Currently when there are untracked changes in a file "one" and in a file
"two" in the repository and the user uses:

    git stash push -k one

all changes in "two" are wiped out completely.  That is clearly not the
intended result.  Make sure that only the files given in the pathspec
are changed when git stash push -k <pathspec> is used.

Reported-by: Jeff King <p...@peff.net>
Signed-off-by: Thomas Gummerer <t.gumme...@gmail.com>
---
 git-stash.sh     |  4 +++-
 t/t3903-stash.sh | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/git-stash.sh b/git-stash.sh
index 13711764a9..2fb651b2b8 100755
--- a/git-stash.sh
+++ b/git-stash.sh
@@ -314,7 +314,9 @@ push_stash () {
 
                if test "$keep_index" = "t" && test -n "$i_tree"
                then
-                       git read-tree --reset -u $i_tree
+                       git read-tree --reset $i_tree
+                       git ls-files -z --modified -- "$@" |
+                       git checkout-index -z --force --stdin
                fi
        else
                git apply -R < "$TMP-patch" ||
diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh
index ea8e5c7818..6f6e31cc6d 100755
--- a/t/t3903-stash.sh
+++ b/t/t3903-stash.sh
@@ -907,4 +907,18 @@ test_expect_success 'stash without verb with pathspec' '
        test_path_is_file bar
 '
 
+test_expect_success 'stash -k -- <pathspec> leaves unstaged files intact' '
+       git reset &&
+       >foo &&
+       >bar &&
+       git add foo bar &&
+       git commit -m "test" &&
+       echo "foo" >foo &&
+       echo "bar" >bar &&
+       git stash -k -- foo &&
+       test "",bar = $(cat foo),$(cat bar) &&
+       git stash pop &&
+       test foo,bar = $(cat foo),$(cat bar)
+'
+
 test_done
-- 
2.12.0.401.g98d3b1bb99.dirty

Reply via email to