Why can't stash be applied to the working directory?
I cannot apply stash back to the working directory.
Little story:
First I tried to push some committed changes, but it said: "no you can't, pull first"... OK then, I'll pull things from GitHub and then push my changes. When I tried to pull, it said that I had changes that would be overwritten, and that I should stash my changes. OK, I stashed the changes... did the pull, and push the committed changes. But now, I cannot restore the uncommitted changes I was working on.
This is the error:
MyPath/File.cs already exists, no checkout
Could not restore untracked files from stash
For sure I don't yet understand all the concepts of git, they confuse me a bit... maybe I did something wrong.
It would be great if someone could help me solve this... I've been searching google and everything for more than an hour now, and I didn't come to a solution yet.
Help is much appreciated. Thanks!
It sounds like your stash included an untracked file that was subsequently added to the repo. When you try and check it out, git rightly refuses because it would be overwriting an existing file.
To fix, you could do something like deleting that file (it's okay, it's still in the repo), applying your stash, and then replacing the stashed version of the file with the in-repo version as appropriate.
Edit: It's also possible that the file has only been created in the working tree without having been added to the repo. In this case, don't simply delete the local file, rather:
- move it somewhere else
- apply the stash
- manually merge the two file versions (working tree vs. moved).
The safest and easiest way would probably be stashing things again:
git stash -u # This will stash everything, including unstaged files
git stash pop stash@{1} # This will apply your original stash
Afterwards if you're happy with the result you may call
git stash drop
to remove your "safe" stash.
As mentioned by @bentolo, you can manually delete the files it is complaining about, switch branches, and then manually add them back. But I personally prefer to stay "within git".
The best way to do this is to convert the stash to a branch. Once it is a branch you can work normally in git using the normal branch-related techniques/tools you know and love. This is actually a useful general technique for working with stashes even when you don't have the listed error. It works well because a stash really is a commit under the covers (see PS).
Converting a stash to a branch
The following creates a branch based on the HEAD when the stash was created and then applies the stash (it does not commit it).
git stash branch STASHBRANCH
Working with the "stash branch"
What you do next depends on the relationship between the stash and where your target branch (which I will call ORIGINALBRANCH) is now.
Option 1 - Rebase stash branch normally (lots of changes since stash)
If you have done a lot of changes in your ORIGINALBRANCH, then you are probably best treating STASHBRANCH like any local branch. Commit your changes in STASHBRANCH, rebase it on ORIGINALBRANCH, then switch to ORIGINALBRANCH and rebase/merge the STASHBRANCH changes over it. If there are conflicts then handle them normally (one of the advantages of this approach is you can see and resolve conflicts).
Option 2 - Reset original branch to match stash (limited changes since stash)
If you just stashed while keeping some staged changes, then committed, and all you want to do is get the additional changes that where not staged when you stashed you can do the following. It will switch back to your original branch and index without changing your working copy. The end result will be your additional stash changes in your working copy.
git symbolic-ref HEAD refs/heads/ORIGINALBRANCH
git reset
Background
Stashes are commits likes branches/tags (not patches)
PS, It is tempting to think of a stash as a patch (just like it is tempting to think of a commit as a patch), but a stash is actually a commit against the HEAD when it was created. When you apply/pop you are doing something similar to cherry-picking it into your current branch. Keep in mind that branches and tags are really just references to commits, so in many ways stashes, branches, and tags are just different ways of pointing at a commit (and its history).
Sometimes needed even when you haven't made working directory changes
PPS, You may need this technique after just using stash with --patch and/or --include-untracked. Even without changing working directories those options can sometimes create a stash you can't just apply back. I must admit don’t fully understand why. See http://git.661346.n2.nabble.com/stash-refuses-to-pop-td7453780.html for some discussion.
The solution: You need to delete the file in question, then try to stash pop/apply again and it should go through. Don't delete other files, just the ones mentioned by the error.
The problem: Git sucks sometimes. When running git stash -u
it includes untracked files (cool !) but it does not remove those untracked files and does not know how to apply the stashed untracked files on top of the leftovers (not cool !), which really makes the -u
option pretty useless.