Why does git stash pop say that it could not restore untracked files from stash entry?

steinybot picture steinybot · Jul 11, 2018 · Viewed 44.6k times · Source

I had a bunch of staged and unstaged changes and I wanted to quickly switch to another branch and then switch back.

So I staged my changes using:

$ git stash push -a

(In hindsight I probably could have used --include-untracked instead of --all)

Then when I went to pop the stash I get a whole lot of errors along the lines of:

$ git stash pop
foo.txt already exists, no checkout
bar.txt already exists, no checkout
...
Could not restore untracked files from stash entry

There doesn't seem to be any changes restored from the stash.

I also tried $ git stash branch temp but that shows the same errors.

I did figure out a way around this which was to use:

$ git stash show -p | git apply

Disaster averted for now but this raises some questions.

Why did this error happen in the first place and how do I avoid it next time?

Answer

Daniel Smith picture Daniel Smith · Jul 11, 2018

I managed to recreate your issue. It seems if you stash untracked files and then you create those files (in your example, foo.txt and bar.txt), then you have local changes to untracked files that would be overwritten when you apply git stash pop.

To get around this issue, you can use the following command. This will override any unsaved local changes so be careful.

git checkout stash -- .

Here is some further information I found on the previous command.