1

Today at work, one of my colleagues noticed that doing a git stash while providing the file path removes the untracked files.

git stash -- src/

This command removes the untracked files and does not recover them after stash pop. However,

git stash

this does not.

I looks like a bug to me. Could you someone please explain if this is the correct behavior ? This works as expected when I try git stash -u

3
  • 1
    The command does not remove untracked files on my system (git 2.16.2 on Linux).
    – lambda
    Commented Mar 21, 2018 at 17:08
  • @lambda: you have the first version of Git in which the bug is fixed :-)
    – torek
    Commented Mar 21, 2018 at 17:23
  • @lambda We use 2.16.1 at work. So close :)
    – Sanketh
    Commented Mar 22, 2018 at 5:50

1 Answer 1

5

This was in fact a bug, fixed in commit bba067d2faf047597bc76f885fb0cf87894b5ed1 by Thomas Gummerer. This means you need Git v2.16.2 or later to have the fix. The bug was introduced in commit df6bba0937209d679a06addd26975593fef744f2, which means Git v2.13.0 or later.

(To avoid the bug, don't stash by pathspec. The stash-by-pathspec feature was new in Git v2.13.0, in the very same commit that has the bug.)

4
  • Thanks a ton ! We use 2.16.1 at work. Time to update I guess
    – Sanketh
    Commented Mar 22, 2018 at 5:49
  • This just happened to me on git 2.17.1. I was able to reproduce it multiple times (edit: formatting fail, see gist): gist.github.com/parkerault/51a1ab6caa295b74f2a44bda8aff829d Commented Jul 11, 2019 at 19:59
  • @ParkerAult: Actually, I had a second look at your gist, and I think what happened here is that you have no tracked files at all. Your git stash made an empty stash, with a non-empty -u commit, and then deleted (because of -u) all untracked files. They'll be in your untracked-files part of the stash. If it doesn't apply correctly, that is yet another git stash bug.
    – torek
    Commented Jul 11, 2019 at 22:17
  • @torek yes, it happens when you try to create a stash that only has untracked files. This bit me really hard when I was trying to stash some rough proof of concept work that I wasn't ready to commit while I did maintenance on the repo. Commented Jul 27, 2019 at 23:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.