Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to undo git stash clear

Tags:

git

I executed git stash save "ABC".
Then by mistake I did git stash clear . How can I retrieve the data that was in stash ABC?

like image 302
Ashish Banker Avatar asked Sep 11 '15 07:09

Ashish Banker


People also ask

Can you undo git stash clear?

All of the above answers end with a git stash apply [commit] which is good, but is not an exact undo of git stash clear . For that you need to re-stash the orphaned stash-commit.

What is git stash clear?

There are two ways to delete a stash: If you no longer need a particular stash, you can delete it with: $ git stash drop <stash_id> . You can delete all of your stashes from the repo with: $ git stash clear .


2 Answers

As it may be found in the documentation of git stash, you may be lucky if this works:

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

If you find the stash you cleared by mistake, then you can do:

git stash apply <stash>

EDIT: Use this command instead git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk --grep=WIP

like image 107
mamuso Avatar answered Oct 16 '22 22:10

mamuso


All of the above answers end with a git stash apply [commit] which is good, but is not an exact undo of git stash clear. For that you need to re-stash the orphaned stash-commit. I found these instructions which almost worked but needed a flag to get all the way there. Summarizing:

  1. Find the orphaned stash commits: git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log --merges --no-walk
  2. Re-stash the commit: git update-ref --create-reflog refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m "My recover stash"
like image 39
studog Avatar answered Oct 16 '22 20:10

studog