Adding and Committing Changes
The Basic Workflow
Every time you want to save your progress in Git, you follow two steps:
# Step 1: Stage your changes
git add <files>Step 2: Commit with a message
git commit -m "Describe what you did"
Step-by-Step Example
# 1. Create a file
echo "Hello, Git!" > hello.txt2. Check status — file is "untracked" (red)
git status3. Stage the file
git add hello.txt4. Check status again — file is "staged" (green)
git status5. Commit
git commit -m "Add hello.txt with greeting"6. Check status — clean!
git status
Staging Multiple Files
# Add specific files
git add file1.txt file2.txtAdd all changed files
git add .Add all .html files
git add *.html
Writing Good Commit Messages
A commit message should explain what you did and why.
✅ Good Messages
Add navigation bar to homepage
Fix login button not responding to clicks
Update README with installation instructions
❌ Bad Messages
update
fix stuff
asdfgh
changes
The Rule of Thumb
Start with a verb in present tense: Add, Fix, Update, Remove, Refactor.Viewing Your Commits
# See commit history
git logCompact one-line format
git log --oneline
Example output:
a1b2c3d Add navigation bar
e4f5g6h Fix login button
i7j8k9l Initial commit
Each commit has a unique hash (like a1b2c3d) — its ID.
Summary
| Command | Purpose |
|---|---|
| <code>git add file</code> | Stage a file |
| <code>git add .</code> | Stage all changes |
| <code>git commit -m "msg"</code> | Create a commit |
| <code>git log</code> | View commit history |
| <code>git log --oneline</code> | Compact history view |