Partially stash changes with git stash -p
When one file contains both a bug fix and an unfinished feature, and you want to run only the fix, git stash -p (--patch) lets you temporarily stash just the unfinished part. git stash -p shows the changes one chunk (hunk) at a time and asks whether to stash each one. You can keep the fix, test it, and commit it, then restore the stashed unfinished part.
I tested on Ubuntu 24.04 with Git 2.43.0. For commit authorship, I set the name and email to example values (Neko <neko@example.com>). The y or n at the end of each prompt line is the answer I typed. I did not test Windows or macOS, or using e to edit a hunk by hand.
- 1. An example where a fix and an unfinished feature are in one file
- 2. Stashing only the unfinished hunk with git stash -p
- 3. When changes are left in the same file as the stash, pop stops
- 4. Commit the remaining fix, then pop
- 5. With two files, you are asked file by file
- 6. Selecting nothing, and new files
- 7. Changes you have staged with add stay staged
An example where a fix and an unfinished feature are in one file
The example is tax.sh, which calculates the tax-inclusive price from a price before tax. In the first commit, the consumption tax is still hard-coded at 8%.
$ ./tax.sh 1000
税込み: 1080円
I made two changes to this file. One is a bug fix that changes 8 to 10. The other is a feature that accepts a discount rate as the second argument. I have not yet written the check_rate function that validates the discount rate.
$ git diff
diff --git a/tax.sh b/tax.sh
index dba8d35..17f938f 100755
--- a/tax.sh
+++ b/tax.sh
@@ -1,6 +1,8 @@
#!/bin/sh
-# Usage: ./tax.sh price_before_tax
+# Usage: ./tax.sh price_before_tax [discount_rate]
price=$1
+off=${2:-0}
+check_rate "$off"
if [ -z "$price" ]; then
echo "価格を指定してください" >&2
@@ -8,7 +10,7 @@ if [ -z "$price" ]; then
fi
# Add consumption tax
-tax=$((price * 8 / 100))
+tax=$((price * 10 / 100))
total=$((price + tax))
echo "税込み: ${total}円"
$ ./tax.sh 1000
./tax.sh: 5: check_rate: not found
税込み: 1100円
The fix works and the output is 1100 yen, but the unfinished line also causes an error. As it stands, I cannot test the fix on its own. A plain git stash without arguments stashes every change to tracked files, so it would also stash the fix.
Stashing only the unfinished hunk with git stash -p
When you run git stash -p, each hunk with changes is shown in turn, followed by a prompt that starts with Stash this hunk. Hunks you answer y to are stashed, and hunks you answer n to stay in the working tree. Here I answered y for the first hunk (the unfinished discount code) and n for the second (the line that changes 8% to 10%).
$ git stash -p
diff --git a/tax.sh b/tax.sh
index dba8d35..17f938f 100755
--- a/tax.sh
+++ b/tax.sh
@@ -1,6 +1,8 @@
#!/bin/sh
-# Usage: ./tax.sh price_before_tax
+# Usage: ./tax.sh price_before_tax [discount_rate]
price=$1
+off=${2:-0}
+check_rate "$off"
if [ -z "$price" ]; then
echo "価格を指定してください" >&2
(1/2) Stash this hunk [y,n,q,a,d,j,J,g,/,s,e,?]? y
@@ -8,7 +10,7 @@ if [ -z "$price" ]; then
fi
# Add consumption tax
-tax=$((price * 8 / 100))
+tax=$((price * 10 / 100))
total=$((price + tax))
echo "税込み: ${total}円"
(2/2) Stash this hunk [y,n,q,a,d,K,g,/,e,?]? n
Saved working directory and index state WIP on main: 580ceec 税込み価格を出すスクリプトを追加
The working tree now has only the change that sets the rate to 10%. The unfinished code is stored as stash@{0}, and you can inspect it with git stash show -p.
$ git diff
diff --git a/tax.sh b/tax.sh
index dba8d35..aefd249 100755
--- a/tax.sh
+++ b/tax.sh
@@ -8,7 +8,7 @@ if [ -z "$price" ]; then
fi
# Add consumption tax
-tax=$((price * 8 / 100))
+tax=$((price * 10 / 100))
total=$((price + tax))
echo "税込み: ${total}円"
$ git stash list
stash@{0}: WIP on main: 580ceec 税込み価格を出すスクリプトを追加
$ git stash show -p
diff --git a/tax.sh b/tax.sh
index dba8d35..70a1d46 100755
--- a/tax.sh
+++ b/tax.sh
@@ -1,6 +1,8 @@
#!/bin/sh
-# Usage: ./tax.sh price_before_tax
+# Usage: ./tax.sh price_before_tax [discount_rate]
price=$1
+off=${2:-0}
+check_rate "$off"
if [ -z "$price" ]; then
echo "価格を指定してください" >&2
$ ./tax.sh 1000
税込み: 1100円
The error is gone, and I confirmed that the fix alone produces 1100 yen.
According to the official git stash reference, git stash -p is the same as git stash push -p. The letters inside [ ] in the prompt are the answer options, and they are shared with git add -p. The "INTERACTIVE MODE" section of the official git add reference explains them. For example, s splits a hunk into smaller pieces, and q quits at that point without stashing the remaining hunks.
When changes are left in the same file as the stash, pop stops
After testing, I tried to restore the unfinished code with git stash pop without committing the fix. The pop failed.
$ git stash pop
error: Your local changes to the following files would be overwritten by merge:
tax.sh
Please commit your changes or stash them before you merge.
Aborting
On branch main
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: tax.sh
no changes added to commit (use "git add" and/or "git commit -a")
The stash entry is kept in case you need it again.
$ git stash list
stash@{0}: WIP on main: 580ceec 税込み価格を出すスクリプトを追加
The stashed hunk and the hunk left behind are both in tax.sh. Because tax.sh still has an uncommitted change (the line set to 10%), Git stopped the pop. As the message The stash entry is kept in case you need it again. says, the stash entry is still in the list, and the working tree is unchanged.
Commit the remaining fix, then pop
When pop stops like this, commit the remaining fix first. Once tax.sh has no uncommitted changes, the pop will work.
$ git commit -q -am "消費税を10%にする"
$ git log --oneline
8b151ad 消費税を10%にする
580ceec 税込み価格を出すスクリプトを追加
$ git stash pop
Auto-merging tax.sh
On branch main
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: tax.sh
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (0bbba7bdfa4153054f35d8bf17157db3db600acd)
$ git diff
diff --git a/tax.sh b/tax.sh
index aefd249..17f938f 100755
--- a/tax.sh
+++ b/tax.sh
@@ -1,6 +1,8 @@
#!/bin/sh
-# Usage: ./tax.sh price_before_tax
+# Usage: ./tax.sh price_before_tax [discount_rate]
price=$1
+off=${2:-0}
+check_rate "$off"
if [ -z "$price" ]; then
echo "価格を指定してください" >&2
The fix is now a commit, and only the unfinished discount code is back in the working tree. git stash list is empty. The flow looks like this:

With two files, you are asked file by file
When several files have changes, git stash -p asks about them one file at a time. I tested with a state where README.md has one added line and tax.sh has the unfinished discount code. Using git stash push -p -m lets you attach a message to the stash. I answered n for README.md and y for tax.sh.
$ git status --short
M README.md
M tax.sh
$ git stash push -p -m "割引の書きかけ"
diff --git a/README.md b/README.md
index 5af2723..a5ef266 100644
--- a/README.md
+++ b/README.md
@@ -1 +1,2 @@
# Calculate tax-inclusive price
+割引率は 0〜100 で指定する
(1/1) Stash this hunk [y,n,q,a,d,e,?]? n
diff --git a/tax.sh b/tax.sh
index aefd249..17f938f 100755
--- a/tax.sh
+++ b/tax.sh
@@ -1,6 +1,8 @@
#!/bin/sh
-# Usage: ./tax.sh price_before_tax
+# Usage: ./tax.sh price_before_tax [discount_rate]
price=$1
+off=${2:-0}
+check_rate "$off"
if [ -z "$price" ]; then
echo "価格を指定してください" >&2
(1/1) Stash this hunk [y,n,q,a,d,s,e,?]? y
Saved working directory and index state On main: 割引の書きかけ
$ git status --short
M README.md
$ git stash list
stash@{0}: On main: 割引の書きかけ
This time, pop worked even though README.md still had an uncommitted change. The earlier pop stopped because the stashed change and the uncommitted change were in the same file, tax.sh.
$ git stash pop
On branch main
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: README.md
modified: tax.sh
no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (01aede288efa9e353f341a45f5e6aa9edd90c2de)
Selecting nothing, and new files
If you answer n to every hunk, Git prints No changes selected, and the stash list does not change.
$ git stash -p
(README.md と tax.sh の hunk の表示は省略。どちらにも n と答えた)
No changes selected
$ git stash list
Files that Git does not track yet (files you have never run git add on) do not appear in the git stash -p prompts. I created a new file, memo.txt, and ran git stash -p. It asked only about README.md and tax.sh, and memo.txt stayed where it was. The -u option (--include-untracked), which also stashes untracked files, cannot be combined with -p.
$ git status --short
M README.md
M tax.sh
?? memo.txt
$ git stash -p -u
Can't use --patch and --include-untracked or --all at the same time
Changes you have staged with add stay staged
If you run git stash -p while you have changes staged with git add, the staged changes also appear in the prompts. Here I staged README.md before running the command, answered n for README.md, and answered y for tax.sh.
$ git add README.md
$ git status --short
M README.md
M tax.sh
$ git stash -p
(hunk の表示は省略。README.md に n、tax.sh に y と答えた)
Saved working directory and index state WIP on main: 8b151ad 消費税を10%にする
$ git status --short
M README.md
$ git stash show --stat
tax.sh | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
README.md stayed staged, and only the tax.sh change was stashed. According to the official reference, using --patch also implies --keep-index, which keeps staged changes in place. The reference says that you can turn this off with --no-keep-index. After I ran git stash pop, the tax.sh change came back, and README.md was still staged.




