Git Foundations / Record Your Work

Stage and commit focused changes

Lesson 6 of 33. Open to read. No account needed.

Why One Change per Commit

Suppose you fix a spelling mistake in a file and, while you are there, add a new section. If both go into one commit called "Update notes", three things become harder later:

  • Reviewing: someone reading the commit has to work out that it contains two unrelated changes.
  • Understanding history: a year later, the message says nothing about either change.
  • Undoing: if the new section turns out to be wrong, undoing it also undoes the spelling fix, because the usual way to undo a commit undoes all of it.

A focused commit holds one coherent change: everything that change needs, and nothing else. The staging area from the "How Git records work" lesson is what makes this possible, because you choose exactly what goes into the next commit, even when unrelated edits sit in the same file.

Staging Whole Files or Parts of a File

git add <file> stages the file's whole current content. That is right when every edit in the file belongs to the same change.

When one file contains edits for different changes, git add -p <file> lets you stage part of it. The -p stands for "patch". Git splits the file's edits into hunks, groups of nearby changed lines, and shows them one at a time:

@@ -3,7 +3,7 @@
 ## Setup steps

 1. Install Git.
-2. Configure your idenity.
+2. Configure your identity.
 3. Create a practice folder.

 ## Meeting times
(1/2) Stage this hunk [y,n,q,a,d,k,K,j,J,g,/,e,p,P,?]?

This is illustrative output. Read a hunk like this:

  • Lines starting with - are being removed; lines starting with + are being added. Here, the misspelled line is replaced by the corrected one.
  • Lines starting with a space are unchanged context, shown so you can recognise where you are in the file.
  • The @@ -3,7 +3,7 @@ header gives the line numbers involved. You can ignore it for now; the "Compare changes with diff" lesson explains it.
  • (1/2) means this is the first of two hunks.

The letters in brackets are your possible answers, and the list varies between Git versions. You need four of them:

AnswerEffect
yStage this hunk
nLeave this hunk unstaged
qStop; leave this and all remaining hunks unstaged
?Show help for every answer

Type the letter and press Enter. Git then shows the next hunk.

Checking Before You Commit

After a partial stage, the file is in two states at once, so git status lists it twice: under "Changes to be committed" for the staged hunk, and under "Changes not staged for commit" for the rest.

To see exactly what the next commit will contain, use:

git diff --staged

It shows the difference between the staging area and the last commit: the change you are about to record. Plain git diff, without --staged, shows what you have not staged. The "Compare changes with diff" lesson covers both in detail. Here, use them to check before committing.

Writing the Commit Message

A commit message has a summary line and, optionally, a body:

  • The summary says what the change does, in a few words: Fix typo in setup steps. Many projects write it as a command ("Fix", "Add") and keep it to about 50 characters so it fits in one-line history views. That is a common convention, not a rule Git enforces.
  • The body, after a blank line, explains why: what problem the change solves or what someone reading the history should know. Leave it out when the summary already says everything.

Each -m option adds one paragraph, so two of them give a summary and a body:

git commit -m "Add troubleshooting section" -m "New team members asked where to start when a command fails."

git log then shows the summary, a blank line and the body.

Without -m, git commit opens the editor you configured, with some lines starting with # that explain the commit. Write the summary on the first line, leave a blank line, write the body, then save and close. Git ignores lines starting with #. If you close the editor without writing a message, Git stops with Aborting commit due to empty commit message. and records nothing.

Why git commit -a Can Surprise You

git commit -a stages every modified tracked file before committing. It is convenient when every change you made belongs together, and it causes surprises in two ways:

  • It includes edits you did not mean to include, from any tracked file.
  • It does not include new, untracked files. A file you created but never added stays out of the commit.

Use it only after git status shows that every modified file belongs in the commit and no new file needs adding.

Exercise: Split a Mixed Edit

You will record two unrelated edits to the same file as two separate commits.

notes.md contains edit 1, a spelling fix, and edit 2, a new Troubleshooting section. git add -p stages edit 1 and leaves edit 2 unstaged. The first git commit records only edit 1 as Fix typo in setup steps; git add and a second git commit then record edit 2 as Add troubleshooting section. The history, newest first, is Add troubleshooting section, Fix typo in setup steps, Add team notes.

Follow the diagram from the top. Both edits start in the working tree. git add -p moves only edit 1 into the staging area, and the first commit records it. Edit 2 is still waiting in the working tree; the second round stages and commits it. The two commits sit on top of the starting commit, each holding exactly one change.

Starting state: Git configured as in the configuration lesson. The setup commands below create everything else in ~/git-practice/split-commits.

These setup commands write files with a construction called a here document. Everything between the line cat > notes.md <<'EOF' and the line EOF is written into notes.md, replacing any previous content. The quotes around EOF keep the text exactly as written. Copy each block whole, from the first line to the closing EOF.

  1. Create the repository with one commit:

    mkdir -p ~/git-practice/split-commits
    cd ~/git-practice/split-commits
    git init -b main
    cat > notes.md <<'EOF'
    # Team notes
    
    ## Setup steps
    
    1. Install Git.
    2. Configure your idenity.
    3. Create a practice folder.
    
    ## Meeting times
    
    The team meets on Monday at 10:00.
    Bring one question about Git.
    
    ## Useful links
    
    See the course lessons.
    EOF
    git add notes.md
    git commit -m "Add team notes"
    

    The file deliberately contains a spelling mistake, "idenity".

  2. Make both edits at once: fix the spelling and add a section at the end. Running this block replaces the file with the edited version:

    cat > notes.md <<'EOF'
    # Team notes
    
    ## Setup steps
    
    1. Install Git.
    2. Configure your identity.
    3. Create a practice folder.
    
    ## Meeting times
    
    The team meets on Monday at 10:00.
    Bring one question about Git.
    
    ## Useful links
    
    See the course lessons.
    
    ## Troubleshooting
    
    Read the whole error message before changing anything.
    EOF
    git status
    

    git status lists notes.md as modified and not staged.

  3. Stage only the spelling fix:

    git add -p notes.md
    

    Git shows the first hunk, the spelling fix. Answer y. It then shows the second hunk, the new section. Answer n.

  4. Check what you staged:

    git status
    git diff --staged
    

    git status lists notes.md under both "Changes to be committed" and "Changes not staged for commit". git diff --staged shows only the idenity line being replaced by identity, and no Troubleshooting lines.

  5. Record the first commit:

    git commit -m "Fix typo in setup steps"
    
  6. Stage and record the second change, with a body explaining why:

    git add notes.md
    git commit -m "Add troubleshooting section" -m "New team members asked where to start when a command fails."
    
  7. Check the result:

    git log --oneline
    git log -p -2
    git status
    

    git log -p -2 shows the two newest commits, each followed by its changes: -p adds the changes, and -2 limits the list to two commits. The next lesson explains git log options in detail. Illustrative output from the first command:

    1ae7a05 Add troubleshooting section
    9b67108 Fix typo in setup steps
    e6f9aec Add team notes
    

Success: there are three commits. In git log -p -2, the "Fix typo in setup steps" commit changes only the idenity line. The "Add troubleshooting section" commit adds only the new section and shows the body text under its summary. git status reports a clean working tree.

If something differs:

What you seeLikely causeNext step
git add -p shows one hunk containing both editsThe file differs from the setup text, so the edits are too close togetherRemove the folder and repeat the setup blocks exactly
You answered y to both hunksA mistyped answergit restore --staged notes.md removes the file from the staging area without changing it; run git add -p notes.md again
No changes. after git add -pStep 2 did not run, or ran in another folderRun pwd, return to the exercise folder and repeat step 2
The first commit contains both editsBoth hunks were staged before committingRemove the folder and start again; the undo module shows how to repair commits
An editor opened during a commit-m was missedWrite the message, save and close; or close without a message to abort
Aborting commit due to empty commit messageThe editor was closed without a messageRun the commit again with -m
  1. Clean up:

    cd ~/git-practice
    ls
    rm -rf split-commits
    ls
    

Independent task: set up the starting state below, then record the edits as two commits without looking at the answer. One commit fixes only the spelling mistake. The other adds the export plan: the new note in app-notes.md together with the new item in todo.md, because they describe the same change. Check your result with git log -p -2.

mkdir -p ~/git-practice/split-practice
cd ~/git-practice/split-practice
git init -b main
cat > app-notes.md <<'EOF'
# App notes

The app will recieve notes from the editor.

## Saving

Notes are saved every minute.

## Sharing

Sharing is not planned yet.
EOF
cat > todo.md <<'EOF'
# To do

- Write the user guide
EOF
git add app-notes.md todo.md
git commit -m "Add app notes and to-do list"
cat > app-notes.md <<'EOF'
# App notes

The app will receive notes from the editor.

## Saving

Notes are saved every minute.

## Sharing

Sharing is not planned yet.

## Export

Users can export notes as a text file.
EOF
cat > todo.md <<'EOF'
# To do

- Write the user guide
- Add an export button
EOF

Check Your Understanding

You run git status and see:

Changes to be committed:
	modified:   notes.md

Changes not staged for commit:
	modified:   notes.md
	modified:   todo.md

Untracked files:
	draft.txt
  1. What exactly does git commit -m "Update notes" record now?
  2. What would git commit -a -m "Update notes" record instead?

Answer and Reasoning

Independent task

git add -p app-notes.md
git commit -m "Fix spelling in app notes"
git add app-notes.md todo.md
git commit -m "Plan the export feature"
git log -p -2

At the git add -p prompts, answer y to the hunk that corrects "recieve" and n to the hunk that adds the Export section. The second git add stages the rest of app-notes.md and the whole of todo.md together, because both edits belong to the export plan. In git log -p -2, the older of the two commits changes only the spelling line. The newer one adds the Export section and the to-do item. Your messages can differ, provided each describes its own change. Remove the folder afterwards with cd ~/git-practice and rm -rf split-practice.

Understanding check

  1. Only the staged version of notes.md. The unstaged edits to notes.md, all edits to todo.md and the untracked draft.txt stay out. A commit records the staging area, nothing else.
  2. -a first stages every modified tracked file, so the commit would include all edits to notes.md, staged and unstaged, plus the edits to todo.md. draft.txt is still left out, because -a never adds untracked files. That mix of changes is exactly what a focused commit avoids.

References

Study this course with an account

Course access adds revision notes, the Quick Reference, saved progress, bookmarks and your own notes.

INR 99 plus 18% GST. 90 days of online access. No auto-renewal.

See course access

This website is operated by VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED, operating as Vectametric.

Corporate Identity Number (CIN): U85499KA2026OPC229822. GSTIN: 29AAMCV8281D1ZS. Registered office: RS NO/38/2A/1 BASTAND, ROAD MAHANTESH NAGAR, Ramdurg, Ramdurg, Belgaum- 591123, Karnataka.

Queries and grievances: Prajval Honnungar, Grievance Officer and Data Protection Contact.

Telephone: +91 94800 09310. Support: support@vectametric.com. Grievances: support@vectametric.com.

Copyright 2026 VECTAMETRIC EDTECH (OPC) PRIVATE LIMITED. All rights reserved.

Vectametric home · Roadmaps · Vecta IT Exams · Vecta Government Exams · Vecta Banking & Finance Exams · Vecta Entrance Exams · About us · Contact · Privacy Policy · Terms and Conditions · No-Refund Policy