By admin , 29 April 2026

The case for patch-mode staging

The default git add stages whole files. Real edits rarely come in whole-file units - you almost always have a mix of unrelated tweaks. git add -p walks you through each hunk and lets you decide what to include in the next commit.

Invoking patch mode

git add -p
# or, scoped to a specific file
git add -p src/checkout.js

Git presents each hunk and a prompt:

Stage this hunk [y,n,q,a,d,s,e,?]?

The prompts that matter

  • y - stage this hunk.
  • n - skip this hunk.
  • s - split into smaller hunks. Often the most useful option.
  • e - edit the hunk by hand. Drop lines you do not want by replacing the leading + or - with a space.
  • q - quit; do not stage anything more.
  • a - stage this and all later hunks in the file.
  • d - skip this and all later hunks in the file.

Manual hunk editing

Pressing e opens the hunk in your editor. To exclude an addition, delete the line. To exclude a removal, change the leading - to a space. The header (@@ -10,6 +10,8 @@) updates automatically.

A real workflow

Imagine you fixed a bug, added a quick debug print, and edited some unrelated styling - all in the same file. You want to commit only the bug fix:

git diff src/cart.js          # review what you have
git add -p src/cart.js        # cherry-pick hunks
git diff --cached src/cart.js # verify staged content
git diff src/cart.js          # confirm leftovers
git commit -m "Fix cart total off-by-one"

Patch mode for other commands

The -p flag works on more than add:

git reset -p          # un-stage hunks selectively
git checkout -p      # discard hunks selectively
git stash -p         # stash specific hunks
git restore -p       # modern equivalent of checkout -p

GUIs and patch mode

If the terminal interface frustrates you, GUI tools like git gui, GitKraken, Fork, and the VS Code Source Control panel offer line-level staging. They are functionally equivalent - choose whichever lets you stay in flow.

The discipline payoff

Patch-mode staging is the gateway drug to atomic commits. Once you can carve a working tree into clean, single-purpose commits, code review accelerates, bisect becomes precise, and reverts become safe. Spend an afternoon with git add -p; it will change how you commit forever.