日本語

How to Split Changes in a Single File into Separate Commits with git add -p

​​Git​command line

When you have written a bug fix and a new feature in the same file, you can split them into separate commits with git add -p (--patch). git add -p shows the changes one chunk (hunk) at a time and asks whether to stage each one. Use it when you want to commit only the fix first, or when you do not want debugging lines included in a commit, so you can stage just part of a file.

Note

The test environment was Ubuntu 24.04 with Git 2.43.0. For the commit author name and email, I set example values (Neko <neko@example.com>). I ran git add -p in the same state a person would have at a terminal (a pseudo-terminal) and pasted the output as is. The ?, s, y, and n at the end of the prompt lines are the answers I typed. I did not test Windows or macOS, or the usage that edits a hunk by hand with e.

An example with two changes mixed in one file

The example is shop.sh, a script that calculates a total from a unit price and a quantity. Orders of 3000 yen or more get free shipping; otherwise, 500 yen shipping is added. However, the comparison operator is -gt (greater than), so when the total is exactly 3000 yen, shipping is still added.

console
$ cat shop.sh
#!/bin/sh
# Usage: ./shop.sh unit_price quantity
price=$1
count=$2
sub=$((price * count))

if [ "$sub" -gt 3000 ]; then
  echo "$sub"
else
  echo $((sub + 500))
fi

$ ./shop.sh 1500 2
3500

I made two changes to this file. One is a bug fix that changes -gt to -ge (greater than or equal). The other is a feature addition that lets you specify a discount amount as a third argument.

console
$ git diff
diff --git a/shop.sh b/shop.sh
index cd4f47e..aff891e 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,10 +1,11 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))

$ ./shop.sh 1500 2
3000

$ ./shop.sh 1500 2 200
3300

Both work. However, if you stage the whole file with git add shop.sh, both changes end up in one commit. I want to commit the bug fix first and put the discount feature in a separate commit.

Answering whether to stage each change with git add -p

When you run git add -p, each changed hunk is shown in turn, followed by the prompt Stage this hunk [y,n,q,a,d,s,e,?]?. Type one character and press Enter. Here, I typed ? first to show the list of answers.

console
$ git add -p
diff --git a/shop.sh b/shop.sh
index cd4f47e..aff891e 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,10 +1,11 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))
(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

The meanings of the list are as follows.

Answer What it does
y Stage this hunk
n Do not stage this hunk
q Quit here. Do not stage this hunk or any of the remaining ones
a Stage this hunk and all the remaining hunks in the same file
d Do not stage this hunk or any of the remaining hunks in the same file
s Split this hunk into smaller hunks
e Manually edit this hunk in an editor

The "INTERACTIVE MODE" section of the official reference (git add) also describes j, J, k, and K to move between hunks, g to select by number, and / to search with a regular expression. The letters in the parentheses of the prompt change depending on the situation. After I split a hunk here, s disappeared from the prompt, and j, g, and / appeared. According to the official reference, if you set interactive.singleKey to true, you can answer with one character without pressing Enter.

Splitting one hunk into three with s

In this example, Git showed the three places that changed as one hunk (it displays (1/1)). If I answered y or n to this as it was, I could not separate the bug fix from the discount feature.

So I typed s. The hunk was split into smaller hunks at the unchanged lines. This time it split into three. I answered n to the first (the usage comment) and the second (the two lines that receive the discount and subtract it), and y to the third (the line that changes -gt to -ge).

The following output continues right after the list of answers above. The same hunk and prompt appear again, and there I typed s.

console
@@ -1,10 +1,11 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? s
Split into 3 hunks.
@@ -1,4 +1,4 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
(1/3) Stage this hunk [y,n,q,a,d,j,J,g,/,e,?]? n
@@ -3,4 +3,5 @@
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
(2/3) Stage this hunk [y,n,q,a,d,K,j,J,g,/,e,?]? n
@@ -6,5 +7,5 @@
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))
(3/3) Stage this hunk [y,n,q,a,d,K,g,/,e,?]? y

The split points were at unchanged lines. Between the first and second hunks were the two lines price=$1 and count=$2. Between the second and third hunks was one empty line.

Check what is staged before committing

When you have answered every hunk, git add -p finishes. In git status, the same shop.sh appears under both "Changes to be committed" (staged) and "Changes not staged for commit" (not yet staged).

console
$ git status
On branch main
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
	modified:   shop.sh

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:   shop.sh

You can check what goes into the next commit with git diff --cached, and what remains unstaged with git diff. git diff --cached shows only the line that changes -gt to -ge, and git diff shows only the lines for the discount feature.

console
$ git diff --cached
diff --git a/shop.sh b/shop.sh
index cd4f47e..3ebabf8 100755
--- a/shop.sh
+++ b/shop.sh
@@ -4,7 +4,7 @@ price=$1
 count=$2
 sub=$((price * count))
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))

$ git diff
diff --git a/shop.sh b/shop.sh
index 3ebabf8..aff891e 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,8 +1,9 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"

If this looks right, run git commit without naming a file. Only the staged content becomes the commit.

console
$ git commit -m "3000円ちょうどでも送料無料にする"
[main dcf64b0] 3000円ちょうどでも送料無料にする
 1 file changed, 1 insertion(+), 1 deletion(-)

Making the remaining changes the second commit

When I ran git add -p again, only the remaining discount feature changes appeared. The two places that changed are now grouped into one hunk. This time I answered y to stage it and committed.

console
$ git add -p
diff --git a/shop.sh b/shop.sh
index 3ebabf8..aff891e 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,8 +1,9 @@
 #!/bin/sh
-# Usage: ./shop.sh unit_price quantity
+# Usage: ./shop.sh unit_price quantity [discount]
 price=$1
 count=$2
-sub=$((price * count))
+discount=${3:-0}
+sub=$((price * count - discount))
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"
(1/1) Stage this hunk [y,n,q,a,d,s,e,?]? y

$ git commit -m "値引き額を指定できるようにする"
[main 44923d6] 値引き額を指定できるようにする
 1 file changed, 3 insertions(+), 2 deletions(-)

$ git log --oneline
44923d6 値引き額を指定できるようにする
dcf64b0 3000円ちょうどでも送料無料にする
feeaed8 合計を計算するスクリプトを追加

The changes to one file are now split across two commits. The flow so far is shown in the diagram below.

Diagram of the flow: the first git add -p splits the shop.sh changes into three hunks and stages only the third as commit 1, and the second git add -p stages the rest as commit 2
The first git add -p stages only the free-shipping check, and the second stages the rest

When I view the first commit with git show, it contains only the line that changes -gt to -ge.

console
$ git show --format="%h %s" HEAD~1
dcf64b0 3000円ちょうどでも送料無料にする

diff --git a/shop.sh b/shop.sh
index cd4f47e..3ebabf8 100755
--- a/shop.sh
+++ b/shop.sh
@@ -4,7 +4,7 @@ price=$1
 count=$2
 sub=$((price * count))
 
-if [ "$sub" -gt 3000 ]; then
+if [ "$sub" -ge 3000 ]; then
   echo "$sub"
 else
   echo $((sub + 500))

Undoing part of an over-staged change with git restore –staged -p

Conversely, after staging a whole file with git add, you may want to unstage only part of it. Here, I added one comment line and one debugging output line (echo "debug: ...") to shop.sh to check a value. Then I staged the whole file by mistake with git add shop.sh.

console
$ git diff
diff --git a/shop.sh b/shop.sh
index aff891e..c9273d2 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,9 +1,11 @@
 #!/bin/sh
 # Usage: ./shop.sh unit_price quantity [discount]
+# Discount amount is specified in yen
 price=$1
 count=$2
 discount=${3:-0}
 sub=$((price * count - discount))
+echo "debug: sub=$sub" >&2
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"

$ git add shop.sh

$ git status --short
M  shop.sh

When I ran git restore --staged -p, the staged changes were shown hunk by hunk, with the prompt Unstage this hunk. This time y means "unstage". I split the hunk with s, answered n for the comment line, and y for the debugging output line.

console
$ git restore --staged -p
diff --git a/shop.sh b/shop.sh
index aff891e..c9273d2 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,9 +1,11 @@
 #!/bin/sh
 # Usage: ./shop.sh unit_price quantity [discount]
+# Discount amount is specified in yen
 price=$1
 count=$2
 discount=${3:-0}
 sub=$((price * count - discount))
+echo "debug: sub=$sub" >&2
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"
(1/1) Unstage this hunk [y,n,q,a,d,s,e,?]? s
Split into 2 hunks.
@@ -1,6 +1,7 @@
 #!/bin/sh
 # Usage: ./shop.sh unit_price quantity [discount]
+# Discount amount is specified in yen
 price=$1
 count=$2
 discount=${3:-0}
 sub=$((price * count - discount))
(1/2) Unstage this hunk [y,n,q,a,d,j,J,g,/,e,?]? n
@@ -3,7 +4,8 @@
 price=$1
 count=$2
 discount=${3:-0}
 sub=$((price * count - discount))
+echo "debug: sub=$sub" >&2
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"
(2/2) Unstage this hunk [y,n,q,a,d,K,g,/,e,?]? y

git diff --cached kept only the comment line, and the debugging output line moved to git diff (the unstaged side). Both changes are still in the file; only the staging changed.

console
$ git diff --cached
diff --git a/shop.sh b/shop.sh
index aff891e..4fbb4fb 100755
--- a/shop.sh
+++ b/shop.sh
@@ -1,5 +1,6 @@
 #!/bin/sh
 # Usage: ./shop.sh unit_price quantity [discount]
+# Discount amount is specified in yen
 price=$1
 count=$2
 discount=${3:-0}

$ git diff
diff --git a/shop.sh b/shop.sh
index 4fbb4fb..c9273d2 100755
--- a/shop.sh
+++ b/shop.sh
@@ -5,6 +5,7 @@ price=$1
 count=$2
 discount=${3:-0}
 sub=$((price * count - discount))
+echo "debug: sub=$sub" >&2
 
 if [ "$sub" -ge 3000 ]; then
   echo "$sub"
Warning

If you run git restore -p without --staged, it is the working-tree file that gets reverted, not the staged content. As the official reference (git restore) says, when neither --staged nor --worktree is given, the working-tree file is the target of the restore. Unstaged changes are restored to the staged content. If you only want to unstage, add --staged.

For a new file Git does not yet track, use git add -N first

If you run git add -p on a newly created file, it prints No changes. and stages nothing. According to the official reference (git add), -p can select only differences between the index (the staging area) and the working tree. A file that is not yet tracked is not in the index, so it does not appear as something to choose from.

console
$ printf '送料は3000円以上で無料\n値引きは円で指定\n' > NOTES.txt

$ git status --short
MM shop.sh
?? NOTES.txt

$ git add -p NOTES.txt
No changes.

If you first run git add -N (--intent-to-add), only the fact that this file will be added later is recorded, with no content (as described for -N in the official reference). After that, you can use git add -p to choose the content to stage. For a new file, the prompt changes to Stage addition.

console
$ git add -N NOTES.txt

$ git status --short
 A NOTES.txt
MM shop.sh

$ git add -p NOTES.txt
diff --git a/NOTES.txt b/NOTES.txt
new file mode 100644
index 0000000..c5498b1
--- /dev/null
+++ b/NOTES.txt
@@ -0,0 +1,2 @@
+送料は3000円以上で無料
+値引きは円で指定
(1/1) Stage addition [y,n,q,a,d,e,?]? y

$ git status --short
A  NOTES.txt
MM shop.sh

In git status --short, the NOTES.txt line changed from A right after git add -N to A .

​Posted by 86