Partial Staging with Hunks
Edit the same real file in two separate places. Stage only one hunk. This is a real power move most developers learn years too late.
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.\n\ngit 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.
Two Real, Separate Edits in One File
git diff cookies.md
@@ -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 add -p cookies.md
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.
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,?]?"
🔍 See the Two Real Hunks
View the real diff on cookies.md and confirm it genuinely splits into two separate hunks.
cd ~/recipes && git diff cookies.mdstudent@lab:~$ cd ~/recipes && git diff cookies.md diff --git a/cookies.md b/cookies.md index ed8c8e6..b573082 100644 --- a/cookies.md +++ b/cookies.md @@ -1,4 +1,4 @@ -Chocolate Chip Cookie Recipe +Chocolate Chip Cookie Recipe (Grandma's) Ingredients: flour, sugar, butter, chocolate chips Serves: 24 cookies @@ -11,3 +11,5 @@ Instructions: 3. Mix in the flour and chocolate chips. 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. + +Variations: add walnuts, or swap half the chocolate chips for M&Ms.
🎛️ Stage Only the First Hunk
Run git add -p and answer the real, interactive prompt: yes to the title credit, no to the variations line.
cd ~/recipes && printf 'y\nn\n' | git add -p cookies.mdstudent@lab:~$ cd ~/recipes && printf 'y\nn\n' | git add -p cookies.md diff --git a/cookies.md b/cookies.md index ed8c8e6..b573082 100644 --- a/cookies.md +++ b/cookies.md @@ -1,4 +1,4 @@ -Chocolate Chip Cookie Recipe +Chocolate Chip Cookie Recipe (Grandma's) Ingredients: flour, sugar, butter, chocolate chips Serves: 24 cookies (1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? @@ -11,3 +11,5 @@ Instructions: 3. Mix in the flour and chocolate chips. 4. Drop spoonfuls onto a baking sheet. 5. Bake at 190C for 10-12 minutes. + +Variations: add walnuts, or swap half the chocolate chips for M&Ms. (2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
📸 Commit Just That One Hunk
Confirm the split with git status, then commit only what was staged.
cd ~/recipes && git statuscd ~/recipes && git commit -m 'Credit the recipe source in the cookie title'student@lab:~$ cd ~/recipes && git status On branch master Changes to be committed: (use "git restore --staged <file>..." to unstage) modified: cookies.md Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: cookies.md student@lab:~$ cd ~/recipes && git commit -m 'Credit the recipe source in the cookie title' [master 280a6ba] Credit the recipe source in the cookie title 1 file changed, 1 insertion(+), 1 deletion(-)
➕ Stage and Commit What Remains
Stage the rest of cookies.md and commit the variations hunk on its own.
cd ~/recipes && git add cookies.mdcd ~/recipes && git commit -m 'Add variations note to cookie recipe'cd ~/recipes && git log --onelinestudent@lab:~$ cd ~/recipes && git add cookies.md student@lab:~$ cd ~/recipes && git commit -m 'Add variations note to cookie recipe' [master 9329755] Add variations note to cookie recipe 1 file changed, 2 insertions(+) student@lab:~$ cd ~/recipes && git log --oneline 9329755 (HEAD -> master) Add variations note to cookie recipe 280a6ba Credit the recipe source in the cookie title c7a2617 Add step-by-step instructions to cookie recipe 8c8aee6 Add Belgian waffles recipe 5856011 Add basic garden salad recipe fd509ba asdf c764cd9 Add prep time estimates to all three recipes 1aa939c Add salt to taste note in soup recipe 56c4ee4 fixed stuff b5dba00 Add chicken noodle soup recipe cd7e65b Add ingredients list and pasta recipe 7fb33b3 Add chocolate chip cookie recipe
Lab 1.2.3 complete. One real file, one edit, genuinely split into two clean commits:\n\n\n Two real hunks seen : ✅ confirmed via git diff\n git add -p interactive split : ✅ real y/n prompts, captured\n First hunk committed alone : ✅ confirmed via git status + log\n Second hunk committed separately : ✅ two clean, real commits\n
Enable JavaScript to run the live terminal and track your progress.