Git stash pop on master branch

1.2k views Asked by At

I have two branches master and development. I stashed on master e.g.

git stash
git checkout development

Now, I was on development branch but by mistake I popped the stash

git stash pop

And now it shows conflicts. I reset the git using this:

git reset HEAD

But it is still showing conflicts. I need my stash back in master branch also. How can i fix this issue?

2

There are 2 answers

4
Petr Skocik On

git reset HEAD won't touch the working tree, just the index. Effectively, it'll just unstage staged files because HEAD is where your current branch is already anyway (that's the definition of HEAD).

git reset --hard HEAD will fix the working tree to reflect HEAD.

If a git pop doesn't apply cleanly, the stash should have been kept.

From man git-stash:

Applying the state can fail with conflicts; in this case, it is not removed from the stash list. You need to resolve the conflicts by hand and call git stash drop manually afterwards.

So just do:

git checkout master
git stash pop 
#or `git stash apply` if you don't want to drop the stash even if it does apply cleanly
0
Rahul Gupta On

Try looking in the git stash manual using:

git stash --help

It has a section on what to do in case you clear/drop stashes by accident. This will help in understanding what is to be done in that case.

Recovering stashes that were cleared/dropped erroneously
   If you mistakenly drop or clear stashes, they cannot be recovered through the normal safety mechanisms. However, you can try the
   following incantation to get a list of stashes that are still in your repository, but not reachable any more:

       git fsck --unreachable |
       grep commit | cut -d\  -f3 |
       xargs git log --merges --no-walk --grep=WIP