Sub topic 16
Building Focused Commits with git add -p
Prerequisites
This chapter assumes you can already run git add, git commit, git status, and git diff. It also assumes you know the three areas: working tree, staging area, and repository. Your author name and email must be configured.
Why stage part of a file
Often one file holds two unrelated edits, such as a typo fix and a reworded paragraph. A single commit for both makes history harder to read and harder to undo. git add -p (short for --patch) lets you stage selected pieces of a file, so each commit holds one idea.
The command splits your changes into hunks. A hunk is a contiguous block of changed lines plus a few unchanged lines around it. Git shows each hunk and asks what to do with it.
A worked example
This creates a repository, commits a 12-line file, then edits lines 2 and 11.
mkdir focus-demo
cd focus-demo
git init
seq 1 12 | sed 's/^/line /' > notes.txt
git add notes.txt
git commit -m "Add notes"
seq 1 12 | sed 's/^/line /' | sed -e '2s/.*/line two (typo fixed)/' -e '11s/.*/line eleven (new wording)/' > notes.txt
git add -p notes.txtGit shows the first hunk, which covers lines 1 to 5, and prompts you. Answer y for this hunk. For the second hunk, which covers lines 8 to 12, answer n.
Stage this hunk [y,n,q,a,d,e,?]? y
Stage this hunk [y,n,q,a,d,K,e,?]? nNow check what is staged, commit it, and then handle the rest.
git diff --staged
git commit -m "Fix typo in line two"
git add notes.txt
git commit -m "Reword line eleven"
git log --onelineThe history now has two small commits instead of one mixed commit.
Prompt keys
| Key | Action |
|---|---|
| `y` | Stage this hunk |
| `n` | Do not stage this hunk |
| `q` | Quit. Hunks already chosen stay staged |
| `a` | Stage this hunk and all later hunks in the file |
| `d` | Skip this hunk and all later hunks in the file |
| `s` | Split the hunk into smaller hunks, offered only when it contains unchanged lines between changes |
| `e` | Edit the hunk by hand |
| `?` | Show help |
Use s when two edits sit close together and Git groups them into one hunk. Use e when even a split hunk mixes two changes on adjacent lines.
Common errors
- Pressing
qtoo early. Hunks you already accepted remain staged, but the rest are never offered. - Committing without checking
git status. A file can appear in both "Changes to be committed" and "Changes not staged for commit". That is expected after a partial stage. - Staging a wrong hunk. Undo it with
git restore --staged -p notes.txt, which asks hunk by hunk what to unstage.
Exercise
You edited app.py in two places that are far apart: a bug fix near the top and a debug print near the bottom. How do you commit only the bug fix, and how do you confirm the debug line is not included?
Show answer
Run git add -p app.py, answer y for the bug-fix hunk and n for the debug hunk. Run git diff --staged to check that only the bug fix appears, then git commit -m "Fix bug". The debug line stays in the working tree, and git diff still shows it.