From: Thomas Gummerer <t.gummerer@xxxxxxxxx> 'git stash push -u -- <pathspec>' prints the following errors if <pathspec> only matches untracked files: fatal: pathspec 'untracked' did not match any files error: unrecognized input This is because we first clean up the untracked files using 'git clean <pathspec>', and then use a command chain involving 'git add -u <pathspec>' and 'git apply' to clear the changes to files that are in the index and were stashed. As the <pathspec> only includes untracked files that were already removed by 'git clean', the 'git add' call will barf, and so will 'git apply', as there are no changes that need to be applied. Fix this by making sure to only call this command chain if there are still files that match <pathspec> after the call to 'git clean'. [jc: swapped the order of if/elseif chain to reduce nesting levels] Reported-by: Marc Strapetz <marc.strapetz@xxxxxxxxxxx> Signed-off-by: Thomas Gummerer <t.gummerer@xxxxxxxxx> Signed-off-by: Junio C Hamano <gitster@xxxxxxxxx> --- git-stash.sh | 7 ++++--- t/t3903-stash.sh | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/git-stash.sh b/git-stash.sh index 4c92ec931f..fa61151548 100755 --- a/git-stash.sh +++ b/git-stash.sh @@ -313,13 +313,14 @@ push_stash () { git clean --force --quiet -d $CLEAN_X_OPTION -- "$@" fi - if test $# != 0 + if test $# = 0 + then + git reset --hard -q + elif git ls-files --error-unmatch -- "$@" >/dev/null 2>&1 then git add -u -- "$@" git diff-index -p --cached --binary HEAD -- "$@" | git apply --index -R - else - git reset --hard -q fi if test "$keep_index" = "t" && test -n "$i_tree" diff --git a/t/t3903-stash.sh b/t/t3903-stash.sh index 1cd85e70e9..f5b102a958 100755 --- a/t/t3903-stash.sh +++ b/t/t3903-stash.sh @@ -1003,4 +1003,20 @@ test_expect_success 'stash -- <subdir> works with binary files' ' test_path_is_file subdir/untracked ' +test_expect_success 'stash -u -- <untracked> doesnt print error' ' + >untracked && + git stash push -u -- untracked 2>actual && + test_path_is_missing untracked && + test_line_count = 0 actual +' + +test_expect_success 'stash -u -- <untracked> leaves rest of working tree in place' ' + >tracked && + git add tracked && + >untracked && + git stash push -u -- untracked && + test_path_is_missing untracked && + test_path_is_file tracked +' + test_done -- 2.17.0-rc0