Synopsis
git add -p [<path>...]
git add --patch [<path>...]
Description
The git add -p command launches an interactive interface that walks through each "hunk" of changes in your working tree, asking you to decide for each: stage, skip, split, edit, etc. This is the secret weapon for crafting clean, atomic commits when your working tree contains a mixture of related and unrelated changes.
The interactive prompt accepts single-letter commands: y (yes, stage this hunk), n (no, skip), s (split into smaller hunks), e (edit the hunk manually), q (quit), ? (help), and several others. Mastering add -p is one of the highest-leverage skills for producing reviewable history.
In day-to-day use, git add -p integrates closely with shell aliases, editor plugins, and continuous integration. Power users often add aliases that combine flags they always pass, or wrap the command in scripts that enforce team conventions. Output formatting can be customized via Git config — pretty formats, color schemes, and pager behavior are all tunable. When something goes wrong, the first diagnostic step is usually to re-run the command with GIT_TRACE=1 in the environment, which reveals the underlying plumbing calls. For unusual situations, the --help output (git add -p --help) opens the full manual page with details on every option, including those rarely used in casual workflows but essential for debugging or scripting at scale.
Understanding how git add -p interacts with the rest of Git's data model — the object database, the index, refs, and the working tree — pays dividends. Each command operates on some subset of these pieces, and knowing which it touches helps predict outcomes and recover from mistakes. Reading the official Git documentation alongside hands-on practice in a throwaway repository is the fastest way to internalize the nuances. Most production issues with Git stem from one of three causes: surprising default behavior, partial network operations, or rewriting history that was already shared. A working mental model of git add -p's side effects helps avoid all three.
Common Options
| Interactive command | Description |
|---|---|
y | Stage this hunk. |
n | Don't stage this hunk. |
s | Split into smaller hunks. |
e | Manually edit the hunk. |
a | Stage this and all remaining hunks in this file. |
d | Skip this and all remaining hunks in this file. |
q | Quit; don't stage further hunks. |
? | Show help. |
Examples
git add -p
# Walk through every changed file's hunks
git add -p src/auth.go
# Restrict to one file
# Inside the prompt:
# Press 's' to split a large hunk into smaller ones
# Press 'e' to manually edit the hunk text
# Press 'y' or 'n' to accept or skip
git diff --cached
# Review what's now staged
Common Mistakes
Editing a hunk with e requires understanding the unified diff format — adding a line means the new file has it, removing a context line means the new file lacks it. Mistakes here can produce confusing index state. Always verify with git diff --cached after.
Related Commands
git add, git stash -p, git restore -p, git checkout -p