Commit 22fc703e authored by Patrick Steinhardt's avatar Patrick Steinhardt Committed by Junio C Hamano

git-stash: fix pushing stash with pathspec from subdir

The `git stash push` command recently gained the ability to get a
pathspec as its argument to only stash matching files. Calling this
command from a subdirectory does not work, though, as one of the first
things we do is changing to the top level directory without keeping
track of the prefix from which the command is being run.

Fix the shortcoming by storing the prefix previous to the call to
`cd_to_toplevel` and then subsequently using `git rev-parse --prefix` to
correctly resolve the pathspec. Add a test to catch future breakage of
this usecase.
Signed-off-by: default avatarPatrick Steinhardt <>
Signed-off-by: default avatarJunio C Hamano <>
parent 3851e448
......@@ -19,6 +19,7 @@ OPTIONS_SPEC=
. git-sh-setup
prefix=$(git rev-parse --show-prefix) || exit 1
......@@ -273,6 +274,8 @@ push_stash () {
eval "set $(git rev-parse --sq --prefix "$prefix" -- "$@")"
if test -n "$patch_mode" && test -n "$untracked"
die "$(gettext "Can't use --patch and --include-untracked or --all at the same time")"
......@@ -812,6 +812,22 @@ test_expect_success 'stash -- <pathspec> stashes and restores the file' '
test_path_is_file bar
test_expect_success 'stash -- <pathspec> stashes in subdirectory' '
mkdir sub &&
>foo &&
>bar &&
git add foo bar &&
cd sub &&
git stash push -- ../foo
) &&
test_path_is_file bar &&
test_path_is_missing foo &&
git stash pop &&
test_path_is_file foo &&
test_path_is_file bar
test_expect_success 'stash with multiple pathspec arguments' '
>foo &&
>bar &&
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment