How to make git stash include new files that have not been staged?

Simon Tewsi picture Simon Tewsi · Jun 22, 2016 · Viewed 19.4k times · Source

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.

Answer

Jonathan.Brink picture Jonathan.Brink · Jun 22, 2016

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.