Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undo git reset --hard after git stash pop

I had some changes in the stash that I attempted to recover using git stash pop. There were some merge conflicts, and rather than resolving them, I decided to just reset it. Unfortunately, in a moment of stupidity, I did a git reset --hard, and now all of the previously stashed changes are gone.

Is there any way to recover these changes? I've tried git fsck --cache --no-reflogs --lost-found --unreachable HEAD, but none of the commit hashes listed refer to the changes I need. What else can I do? or did I just lose all of that work?

like image 560
eirikir Avatar asked Dec 11 '14 20:12

eirikir


People also ask

Can I revert git stash pop?

After that just fire git stash pop again and you get the same stash, that conflicted before. Keep in mind: The stash is safe, however, uncommitted changes in the working directory are of course not. They can get messed up. What I understand is that you can simply cleanup and pop again, but you can't undo it.

Can you undo a reset hard git?

In most cases, yes. Depending on the state your repository was in when you ran the command, the effects of git reset --hard can range from trivial to undo, to basically impossible.

How do I recover my git code after stash?

Retrieve Stashed Changes To retrieve changes out of the stash and apply them to the current branch you're on, you have two options: git stash apply STASH-NAME applies the changes and leaves a copy in the stash. git stash pop STASH-NAME applies the changes and removes the files from the stash.


2 Answers

Just after posting this, I thought to check .git/refs/stash, which I thought would be blank after the pop. However, likely due to the merge conflict, it still had the hash from the stash I had tried to pop!

I did a git stash apply with the hash and a git reset to resolve the merge conflicts the lazy way.

Sorry if this was a git-noob question and answer. Hopefully this helps someone else.

like image 194
eirikir Avatar answered Oct 04 '22 12:10

eirikir


git help 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 the stash you though has gone is still in your stash list, just git stash list to find it.

like image 42
leo108 Avatar answered Oct 01 '22 12:10

leo108