Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't stash be applied to the working directory?

Tags:

git

git-stash

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!

like image 294
Miguel Angelo Avatar asked May 09 '12 01:05

Miguel Angelo


People also ask

How do I install the latest stash to the working directory?

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.

Can git stash be applied to any branch?

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.

How do I stash changes to a folder?

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.

What is the best way to stash changes that you will not work on anytime soon?

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.


2 Answers

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:

  1. move it somewhere else
  2. apply the stash
  3. manually merge the two file versions (working tree vs. moved).
like image 168
blahdiblah Avatar answered Oct 23 '22 07:10

blahdiblah


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.

like image 22
Koraktor Avatar answered Oct 23 '22 08:10

Koraktor