C.W.K.
Stream
Lesson 03 of 05 · published

git add -p: Surgical Staging

~24 min · add, patch, staging

Level 0Untracked Rookie
0 XP0/47 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

git add -p is how professionals stage

git add file.js stages the entire file. git add . stages everything in the current directory. Both work for the simple case where every change in a file belongs in the same commit. The simple case is rare. Most real coding sessions touch unrelated things — a feature plus a typo, a bug fix plus a debug log you forgot to remove, a refactor plus a stray console.log. Lumping them into one commit hurts review and breaks bisect later.

The professional answer is git add -p (also --patch). It walks each "hunk" of changes and asks stage this one? You answer y (yes), n (no), s (split into smaller hunks), or e (manually edit the hunk). The result: one commit contains exactly the bug fix, another contains exactly the typo, another contains exactly the debug-log removal. History reads cleanly and bisect can isolate the failure.

The companion is git restore -p for unstaging or discarding hunks selectively, and git checkout -p for the older equivalent. Modern editors also have GUI hunk staging — VS Code's "Stage Selected Ranges," Tower, GitKraken, GitHub Desktop. Whichever you use, the muscle to build is "review every diff before commit," not "commit everything you happened to type today."

One refinement: git add -A stages all changes including deletions and untracked files in the entire repo. Convenient and dangerous — it is how unrelated test artifacts, secret .env edits, and accidental binaries end up in commits. Many seniors avoid -A entirely and use git add -p + named files for everything else.

Code

Interactive patch staging session·bash
# Walk every hunk in every modified file, choosing per hunk:
git add -p

# Per-prompt keys:
#   y  stage this hunk
#   n  do not stage
#   q  quit
#   a  stage this and all remaining hunks in this file
#   d  do not stage this or any remaining hunks in this file
#   s  split into smaller hunks
#   e  edit the hunk manually before staging
#   ?  print help
Selectively unstage or restore·bash
# Unstage some hunks you accidentally added:
git restore --staged -p src/app.js

# Discard unstaged changes interactively (DESTRUCTIVE — pick carefully):
git restore -p src/style.css

External links

Exercise

Find a file in any project with several unrelated edits (or create the situation deliberately). Run git add -p. For at least two hunks, use s to split, then make different decisions per piece. End with two separate commits, each with a focused message. Run git log --oneline -p -n 2 and confirm the commits read as two clean stories rather than one mixed dump.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.