Two Real, Separate Edits in One File
@@ -1,4 +1,4 @@
-Chocolate Chip Cookie Recipe
+Chocolate Chip Cookie Recipe (Grandma's)
...
@@ -11,3 +11,5 @@ Instructions:
...
+
+Variations: add walnuts, or swap half the chocolate chips for M&Ms.
Two real @@ hunks, far enough apart in the file that git genuinely diffs them as two separate blocks rather than one.
Splitting Them With git add -p
git shows the first hunk and asks, for real:
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?
Answer y to stage the title-credit hunk, then n for the variations hunk — a genuine, interactive choice per hunk, not an all-or-nothing file-level decision.
Two Real, Separate Commits
git commit -m "Credit the recipe source in the cookie title"
git add cookies.md
git commit -m "Add variations note to cookie recipe"
The same single file, edited once, now has two real commits — each containing exactly one logical change.
cookies.md has just been edited in two real, unrelated places: the title now credits its source, and a new "Variations" section was added near the bottom. Both changes are sitting in the working directory together, unstaged — but they genuinely have nothing to do with each other, and deserve two separate commits, not one.
git add -p is the real tool for exactly this: it walks through each diff hunk one at a time and asks, for real, whether to stage it — a genuine interactive y/n/s/q/e prompt per hunk, confirmed directly rather than described.
Hunk
One contiguous block of changed lines in a diff, shown under its own @@ header. git genuinely splits a file's changes into separate hunks whenever they are far enough apart (more than a few lines of unchanged context between them) — confirmed directly in this lab.
git add -p (patch mode)
Walks through a file's real diff hunk by hunk, asking a genuine, interactive y/n/s/q/e/? question for each one, rather than staging the whole file at once. The real prompt (confirmed directly): "Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]?"
Takeaways
- git genuinely splits a file's diff into separate hunks whenever changes are far enough apart in the file
- git add -p walks through each hunk with a real, interactive y/n/s/q/e/? prompt, rather than an all-or-nothing file-level choice
- A file can genuinely appear both staged and modified in git status at once, when only some of its hunks were staged
- Partial staging turns one messy, multi-purpose edit into several clean, honestly-scoped commits
- This is a real, practical power move — most developers only discover git add -p years into using git