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:
| Answer | Effect |
|---|---|
y | Stage this hunk |
n | Leave this hunk unstaged |
q | Stop; 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.

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.
-
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".
-
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 statusgit statuslistsnotes.mdas modified and not staged. -
Stage only the spelling fix:
git add -p notes.mdGit shows the first hunk, the spelling fix. Answer
y. It then shows the second hunk, the new section. Answern. -
Check what you staged:
git status git diff --stagedgit statuslistsnotes.mdunder both "Changes to be committed" and "Changes not staged for commit".git diff --stagedshows only theidenityline being replaced byidentity, and no Troubleshooting lines. -
Record the first commit:
git commit -m "Fix typo in setup steps" -
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." -
Check the result:
git log --oneline git log -p -2 git statusgit log -p -2shows the two newest commits, each followed by its changes:-padds the changes, and-2limits the list to two commits. The next lesson explainsgit logoptions 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 see | Likely cause | Next step |
|---|---|---|
git add -p shows one hunk containing both edits | The file differs from the setup text, so the edits are too close together | Remove the folder and repeat the setup blocks exactly |
You answered y to both hunks | A mistyped answer | git 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 -p | Step 2 did not run, or ran in another folder | Run pwd, return to the exercise folder and repeat step 2 |
| The first commit contains both edits | Both hunks were staged before committing | Remove the folder and start again; the undo module shows how to repair commits |
| An editor opened during a commit | -m was missed | Write the message, save and close; or close without a message to abort |
Aborting commit due to empty commit message | The editor was closed without a message | Run the commit again with -m |
-
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
- What exactly does
git commit -m "Update notes"record now? - 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
- Only the staged version of
notes.md. The unstaged edits tonotes.md, all edits totodo.mdand the untrackeddraft.txtstay out. A commit records the staging area, nothing else. -afirst stages every modified tracked file, so the commit would include all edits tonotes.md, staged and unstaged, plus the edits totodo.md.draft.txtis still left out, because-anever adds untracked files. That mix of changes is exactly what a focused commit avoids.