Search code examples
gitgit-stash

What option did I miss when I stashed untracked file in Git, but it didn't restore with git stash pop?


I was testing two things at once and had working tree status like:

Changes not staged for commit:
    modified:   component/file.html
    modified:   component/file.scss
    modified:   component/file.ts
    modified:   component2/file.scss

Untracked files:
    common/file.scss

I decided to temporarily stash all scss-files to focus on others with git stash push -m "scss files for later" -- *.scss So my working tree now looks like this:

Changes not staged for commit:
    modified:   component/file.html
    modified:   component/file.ts

Later I popped the same stash back, but the untracked scss (which was apparently stashed) file didn't pop:

Changes not staged for commit:
    modified:   component/file.html
    modified:   component/file.scss
    modified:   component/file.ts
    modified:   component2/file.scss

As I popped (vs. applied) the stash, the stash ref is now gone.

Should I have applied some sort of flag on pop?
Or was the file gone already when I pushed the stash (meaning: should I have applied some sort of flag on push)?

(git version 2.15.0)


Steps to reproduce on gist: https://gist.github.com/keinajar/c52d90cded491f82ffbbf5939fc1b044

Referring to gist: if I try to git stash push -- common/d.scss instead of *.scss I get the error which (based on current answers) would be the expected outcome:

error: pathspec 'common/d.scss' did not match any file(s) known to git.
Did you forget to 'git add'?

So instead of me missing options, this is a bug?


Solution

  • To stash untracked files you need to use --include-untracked flag (or -u) according to documentation for push command.

    And about this odd behaviour. Yes, it is a bug. You can find more info in this mailing list.

    As it says:

    Currently when 'git stash push -- ' is used, untracked files that match the pathspec will be deleted, even though they do not end up in a stash anywhere.