49

I'm no great expert at git but I've been using it for a couple of years. Lately I've noticed some strange behaviour relating to stashing. It seems to be only partially working for me.

For example today I tried to stash 4 uncommitted changes. 1 file was added to the stash, the remaining 3 stayed as uncommitted changes. Tried it a couple of times and after the first attempt I was just getting "No local changes to save", yet the status still showed the 3 uncommitted changes.

However, by coincidence I found that when I first stage the previously unstashable changes, they will be stashed normally. I've played around and this is repeatable: If all changes are uncommitted, only the one change is stashed. If the "problem" changes are staged then all changes are stashed (doesn't matter whether the "good" change is staged or not, it will always stash).

Is this a bug or am I doing something wrong?

I'm using Git for Windows 2.8.2 and Git Extensions 2.48.05.

It doesn't matter whether I try to stash the changes via the Git Extensions GUI or via the console.

EDIT: If I stage all 4 changes before stashing, when I do a stash pop the 3 problem changes are staged, as before, but the good change, which always stashes, is uncommitted. So the stash pop does not round trip the status to the way it was before the stash. This is repeatable as well.

Chirag Bhatia - chirag64
  • 4,430
  • 3
  • 26
  • 35
Simon Elms
  • 17,832
  • 21
  • 87
  • 103
  • 5
    Perhaps the files that are not being staged are not tracked by Git? Try `git stash -u` – Jonathan.Brink Jun 21 '16 at 23:58
  • 3
    @Jonathan.Brink: That's the answer. The changes that wouldn't stash were new files, not yet tracked. I set the -u flag and they stashed without problems. If you would like to make that an answer I'll accept it. – Simon Elms Jun 22 '16 at 02:27
  • 1
    Does this answer your question? [How do you stash an untracked file?](https://stackoverflow.com/questions/835501/how-do-you-stash-an-untracked-file) – Seth Battin Aug 24 '20 at 17:48

1 Answers1

83

The problem seems to be that by default the stash command will not include untracked files.

To include untracked files, use the -u (--include-untracked) flag:

git stash -u

If the --include-untracked option is used, all untracked files are also stashed and then cleaned up with git clean, leaving the working directory in a very clean state.

Jonathan.Brink
  • 23,757
  • 20
  • 73
  • 115
  • 13
    as mentioned in answers to [this earlier question](http://stackoverflow.com/questions/835501/how-do-you-stash-an-untracked-file), using `git stash -u` **will [permanently delete ignored folders](https://gitlab.com/tortoisegit/tortoisegit/issues/1200)** – phillchill May 05 '17 at 09:56
  • 2
    The command `git stash -u` indeed solves the issue, but using GIT extensions, it would be great having a configuration setting which automatically uses `-u` while adding to the stash. In my version (2.51) this setting seems not to be present. Any idea if and when this might happen (or is it already done?)? – Dominique Mar 23 '18 at 08:59
  • 2
    @Dominique one thing you could do is create an alias so you don't need to worry about adding the `-u` flag: `git config --global alias.stashu "stash -u"`. Then you invoke the alias with: `git stashu` – Jonathan.Brink Mar 23 '18 at 13:58
  • 2
    @Dominique and others wondering, if you talk about Git Extensions, the Windows Git GUI, instead of doing a simple Stash, select Create a stash and check Include untracked files. – PhiLho Oct 30 '19 at 09:04