git add -p: how to stage only part of a file
What git add -p does, every prompt option (y, n, s, e...) and how to edit a hunk by hand without breaking the patch. With real Git output.
git add -p (long form: git add --patch) walks you through your changes one block at a time and, for each block, asks whether you want to move it to the staging area. It is how you do partial staging: staging only part of a file for the next commit and leaving the rest where it is, without copying or deleting anything by hand.
To follow this post you need to know what the staging area is (the place where you prepare what goes into the commit); if you don’t, start with what the Git staging area is and what it’s for. Why you should split changes into small commits is covered in atomic commits: what they are and how to make them. Here we go straight to the command.
What a hunk is and why it matters
A hunk is a block of changed lines inside a file, with a few unchanged lines around it as context. It is the same thing you see under each @@ ... @@ when you run git diff. If reading that output is still hard for you, see git diff explained with examples.
Plain git add puts the whole file into the staging area. git add -p goes one level down: it compares your folder (the working directory) with the index (or staging area), splits the difference into hunks and lets you decide on them one by one[1]. The unit of decision is the hunk, and that explains almost everything you are about to see in the prompt.
A real git add -p session
The output blocks in this post come from a single session in a test repo with git 2.43.0. I start from a precio.js file with one commit, add two debugging console.log calls and make the real fix: formatting the price with two decimals. I only want to commit the fix.
$ git diff --stat
precio.js | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
I run git add -p and Git shows me the first (and only) hunk:
$ git add -p
diff --git a/precio.js b/precio.js
index fc92be9..aa15bb9 100644
--- a/precio.js
+++ b/precio.js
@@ -1,11 +1,13 @@
const IVA = 0.21;
function precioFinal(base) {
+ console.log("base", base);
return base * (1 + IVA);
}
function formatear(precio) {
- return precio + " €";
+ console.log("formateando", precio);
+ return precio.toFixed(2) + " €";
}
module.exports = { precioFinal, formatear };
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]?
The changes are only a few lines apart, so Git has merged them into a single hunk, and y would also take the debugging along with it. I press ?:
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? ?
y - stage this hunk
n - do not stage this hunk
q - quit; do not stage this hunk or any of the remaining ones
a - stage this hunk and all later hunks in the file
d - do not stage this hunk or any of the later hunks in the file
s - split the current hunk into smaller hunks
e - manually edit the current hunk
? - print help
After the help, Git prints the hunk and the same prompt again. The option I need is s, which splits the hunk at the unchanged lines in between:
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? s
Split into 2 hunks.
@@ -1,7 +1,8 @@
const IVA = 0.21;
function precioFinal(base) {
+ console.log("base", base);
return base * (1 + IVA);
}
function formatear(precio) {
(1/2) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? n
@@ -4,8 +5,9 @@
return base * (1 + IVA);
}
function formatear(precio) {
- return precio + " €";
+ console.log("formateando", precio);
+ return precio.toFixed(2) + " €";
}
module.exports = { precioFinal, formatear };
(2/2) Stage this hunk [y,n,q,a,d,K,g,/,e,?]?
The first piece is only debugging, so n. The second one has a problem: the console.log and the toFixed are on adjacent lines, with no context between them, and s can’t split it any further. Notice that s has disappeared from the list. That is what e is for, which we’ll get to in a moment.
Every option in the git add -p prompt
The prompt only shows the letters that make sense for the current hunk; ? shows the help for those same letters[1][2]. The full list in git 2.43:
| Key | What it does | When it appears |
|---|---|---|
y | Stages this hunk | Always |
n | Doesn’t stage it | Always |
q | Quits; doesn’t stage this one or the remaining ones (whatever you already marked with y does get applied) | Always |
a | Stages this hunk and all later hunks in the file | Always |
d | Doesn’t stage this one or any later hunk in the file | Always |
s | Splits the hunk into smaller ones | Only if there are unchanged lines between two changes |
e | Opens the hunk in your editor | Almost always; not for a mode change or when deleting the whole file |
j / J | Leaves this one undecided; j goes to the next undecided hunk and J to the next hunk, decided or not | When there is a hunk after it |
k / K | The same, going backwards | When there is a hunk before it |
g | Jumps to a hunk by number | With more than one hunk |
/ | Searches for a hunk with a regular expression | With more than one hunk |
? | Shows the help | Always |
For j/k, the lowercase letter skips the hunks you have already decided and the uppercase one doesn’t. That is why hunk 2 of 2 shows K but not k: hunk 1 already had an answer. Git 2.45 added p to print the hunk again and Git 2.52 added P to view it in the pager[3]; 2.43 has neither.
Git writes to the index file by file: when you finish with the hunks of one file, it applies what you marked in it and moves on to the next. I checked it with two files: y in the first one and Ctrl+C at the prompt of the second, and git status --short showed the first as staged (M ) and the second as not ( M). And if pressing Enter after every letter gets tiresome, setting the interactive.singleKey config option to true avoids it[2].
Editing a hunk by hand with the e option
The e option opens the hunk in your editor as a piece of diff you can modify (a patch: the list of lines to remove and to add, with their context); when you save and close, Git applies whatever is left to the index[2]. This is what the editor showed me when I pressed e on hunk 2 of 2:
# Manual hunk edit mode -- see bottom for a quick guide.
@@ -4,8 +5,9 @@
return base * (1 + IVA);
}
function formatear(precio) {
- return precio + " €";
+ console.log("formateando", precio);
+ return precio.toFixed(2) + " €";
}
module.exports = { precioFinal, formatear };
# ---
# To remove '-' lines, make them ' ' lines (context).
# To remove '+' lines, delete them.
# Lines starting with # will be removed.
# If the patch applies cleanly, the edited hunk will immediately be marked for staging.
# If it does not apply cleanly, you will be given an opportunity to
# edit again. If all lines of the hunk are removed, then the edit is
# aborted and the hunk is left unchanged.
The guide at the bottom sums it up well, but in plain terms there are two rules:
- To not stage a
+line, delete it entirely. That’s what I did: I removed the+ console.log("formateando", precio);line and kept thetoFixedone. - To not stage a
-line (so it isn’t deleted in the commit), change the-to a space. That turns it into context.
I saved, closed, and the staging area ended up with exactly the fix:
$ git diff --staged
diff --git a/precio.js b/precio.js
index fc92be9..dd5c889 100644
--- a/precio.js
+++ b/precio.js
@@ -5,7 +5,7 @@ function precioFinal(base) {
}
function formatear(precio) {
- return precio + " €";
+ return precio.toFixed(2) + " €";
}
module.exports = { precioFinal, formatear };
Meanwhile, git diff still shows both console.log calls and git status --short marks the file as MM: one part staged and another not. The file on disk hasn’t changed. This is where a normal git commit would go, saving only the fix and leaving the console.log calls in the file, outside the commit. I didn’t do it in this session, because further down I use this same state to show git reset -p.
What breaks the patch
The documentation is blunt about context and removal lines: don’t add new ones, don’t delete them, don’t change their text[2]. Those lines describe what the file currently looks like in the index; if you change their text, or add or delete one, Git can’t find that text and the patch doesn’t apply. Changing only the leading sign, like the - to a space earlier, is fine. When it fails, Git offers to let you edit again, and if you delete every line of the hunk, it cancels the edit.
The sneakiest mistake is splitting a -/+ pair halfway: turning the - into a space but keeping the +, which the documentation warns leaves the index in a confusing state[2]. I tried it in a separate repo with a line old changed to new, and the index ended up with both, old and new, one under the other. It applies without an error, which is exactly what makes it dangerous. Before committing after an e, always check git diff --staged.
Cases that trip people up
New files. An untracked file has no version in the index to compare against, so git add -p doesn’t see it. In my repo, with a freshly created descuentos.js:
$ git add -p descuentos.js
No changes.
$ git add -N descuentos.js
$ git add -p descuentos.js
diff --git a/descuentos.js b/descuentos.js
new file mode 100644
...
(1/1) Stage addition [y,n,q,a,d,e,?]? y
git add -N (--intent-to-add) puts an empty entry for that file into the index[1], and from then on its whole content shows up as a hunk of added lines. Notice that the prompt changes to “Stage addition”.
Binary files. They have no lines, so there are no hunks. With a modified logo.png, Git replies Only binary files changed. and asks nothing. For those, use a normal git add logo.png.
Narrowing the scope. git add -p precio.js only reviews that file (or that folder, if you pass a directory path). With no paths, it goes through every tracked file with changes, one after another.
Undoing a choice. If you staged a hunk you didn’t want, git reset -p is the same prompt in the opposite direction: it takes hunks out of the index without touching your files[4]. In the session I used it to give back the toFixed fix:
$ git reset -p precio.js
...
(1/1) Unstage this hunk [y,n,q,a,d,e,?]? y
Afterwards, git diff --staged came out empty and the change was still in the file. git restore --staged -p does the same[5].
The -p family: the same prompt in other directions
Several commands accept -p with this same interface; what changes is between which two places they move the change (checked with -h on git 2.43.0):
| Command | What it does with the hunks you pick |
|---|---|
git add -p | Copies them from your folder to the index |
git reset -p | Removes them from the index (your folder doesn’t change) |
git restore --staged -p | Same as the previous one |
git restore -p | Discards them from your folder. Whatever you discard is lost |
git checkout -p | Discards from the folder, like restore -p, but with the older command |
git stash -p | Saves only the chosen hunks to a stash and removes them from your folder |
git commit -p | Opens the prompt and commits what you chose plus whatever you had already staged |
The two scary ones are restore -p and checkout -p, because a y there deletes work that wasn’t saved in any commit[5][6]. Read the question before answering: it says Discard this hunk from worktree instead of Stage this hunk.
If you prefer the mouse, VS Code has the Stage Selected Ranges command, which stages only the selected lines[7]. Even so, knowing how to read the prompt saves the day when you’re working over SSH or when an AI agent has left you mixed changes in a file.
Splitting changes with -p is learned by doing more than by reading. The Git from zero to professional course has interactive exercises where you stage commits piece by piece with feedback at every step.
Sources
- git-add, official Git documentation (source in the git/git repository): the
--patchand--intent-to-addoptions, and the list of keys for thepatchsubcommand. - git-add for version 2.43.0 (git/git repository): the “Interactive mode” and “Editing patches” sections, with the rules for editing a hunk, the edits that keep the patch from applying, and
interactive.singleKey. - git-add on the master branch of git/git: the current list of keys, which adds
p(since 2.45) andP(since 2.52) compared to 2.43. - git-reset, official Git documentation:
git reset -pas the opposite ofgit add -p. - git-restore, official Git documentation:
--patchand--staged. - git-checkout and git-stash, official Git documentation:
checkout -papplies the chosen hunks in reverse to your folder; the behavior ofstash -pis in git-stash. - Strings of the VS Code Git extension (microsoft/vscode): the “Stage Selected Ranges” and “Unstage Selected Ranges” commands.
Frequently Asked Questions
What does the p in git add -p mean?
It stands for patch: -p is the short form of --patch, because what you pick are pieces of a patch.
Why don’t I see the s option in git add -p?
Because that hunk can’t be split any further. s only appears when there is at least one unchanged line between two changes in the hunk; if the changed lines are adjacent, the only way to separate them is e.
If I press q in git add -p, do I lose what I already chose?
No. q leaves the current hunk and the remaining ones unstaged, but the ones you already marked with y do go to the index. I checked it in a test repo: y on the first hunk, q on the second, and git diff --staged showed the first one.
Does git add -p modify my files?
No, not even when you edit a hunk with e. It only changes what’s in the index; your folder stays as it was. That’s why, after using it, it’s normal to see the same file in both git diff and git diff --staged at the same time, each with its own part, and git status --short marks it as MM. The commands in the family that do touch your files are different ones: git restore -p, git checkout -p and git stash -p.