Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 282
Simon Tewsi Avatar asked Jun 21 '16 23:06

Simon Tewsi


People also ask

How do I stash changes not staged for commit?

If you only want to stash specific changes in those files, add the --patch option. The --include-untracked option lets you stash untracked files. Run git help stash (or man git-stash ) for more info.

Do you need to stage before stash?

If you stash your files you should "unstash" to recover them. Now, if those are new files, yes, you should stage them first.

Does git stash include staged files?

The answer to the question as asked ("does stash convert staged files to unstaged") is both yes and no. If you've applied with git stash apply (vs git stash pop ), you're in great shape because the stash is still present.

How do I add a specific file to stash?

To stash a specific file, use the “git stash push” command and specify the file you want to stash. However, the other tracked files that may be modified in your current working directory are untouched.


1 Answers

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.

like image 198
Jonathan.Brink Avatar answered Sep 22 '22 13:09

Jonathan.Brink