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!
Apply Git stashes In order to apply your Git stash to your current working directory, use the “git stash apply” command and specify the stash you want to apply. If you don't specify any arguments to the apply command, the top of the stack will be applied.
Creating and applying a stash entry To reapply our stashed changes at a later point, we can use git stash apply . We can apply the stash entry to a different branch – it doesn't have to be the branch that we created the stash from.
Basically, it adds all changed files to index, except for folder (or files) you want to stash. Then you stash them using -k ( --keep-index ). And finally, you reset index back to where you started.
Create a new branch to apply your stashed changes to, and then pop your stashed changes onto it: $ git stash branch <branch_name> <stash_id> . This is another way to save your stash before moving on with the project.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With