How to recover a dropped stash in Git?

GitRecoveryGit Stash

Git Problem Overview


I frequently use git stash and git stash pop to save and restore changes in my working tree. Yesterday I had some changes in my working tree that I had stashed and popped, and then I made more changes to my working tree. I'd like to go back and review yesterday's stashed changes, but git stash pop appears to remove all references to the associated commit.

I know that if I use git stash then .git/refs/stash contains the reference of the commit used to create the stash. And .git/logs/refs/stash contains the whole stash. But those references are gone after git stash pop. I know that the commit is still in my repository somewhere, but I don't know what it was.

Is there an easy way to recover yesterday's stash commit reference?

Note that this isn't critical for me today because I have daily backups and can go back to yesterday's working tree to get my changes. I'm asking because there must be an easier way!

Git Solutions


Solution 1 - Git

Once you know the hash of the stash commit you dropped, you can apply it as a stash:

git stash apply $stash_hash

Or, you can create a separate branch for it with

git branch recovered $stash_hash

After that, you can do whatever you want with all the normal tools. When you’re done, just blow the branch away.

Finding the hash

If you have only just popped it and the terminal is still open, you will still have the hash value printed by git stash pop on screen (thanks, Dolda).

Otherwise, you can find it using this for Linux, Unix or Git Bash for Windows:

git fsck --no-reflog | awk '/dangling commit/ {print $3}'

...or using Powershell for Windows:

git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[2] }

This will show you all the commits at the tips of your commit graph which are no longer referenced from any branch or tag – every lost commit, including every stash commit you’ve ever created, will be somewhere in that graph.

The easiest way to find the stash commit you want is probably to pass that list to gitk:

gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

...or see the answer from emragins if using Powershell for Windows.

This will launch a repository browser showing you every single commit in the repository ever, regardless of whether it is reachable or not.

You can replace gitk there with something like git log --graph --oneline --decorate if you prefer a nice graph on the console over a separate GUI app.

To spot stash commits, look for commit messages of this form:

        WIP on somebranch: commithash Some old commit message

Note: The commit message will only be in this form (starting with "WIP on") if you did not supply a message when you did git stash.

Solution 2 - Git

If you didn't close the terminal, just look at the output from git stash pop and you'll have the object ID of the dropped stash. It normally looks like this:

$ git stash pop
[...]
Dropped refs/stash@{0} (2ca03e22256be97f9e40f08e6d6773c7d41dbfd1)

(Note that git stash drop also produces the same line.)

To get that stash back, just run git branch tmp 2cae03e, and you'll get it as a branch. To convert this to a stash, run:

git stash apply tmp
git stash

Having it as a branch also allows you to manipulate it freely; for example, to cherry-pick it or merge it.

Solution 3 - Git

Just wanted to mention this addition to the accepted solution. It wasn't immediately obvious to me the first time I tried this method (maybe it should have been), but to apply the stash from the hash value, just use "git stash apply ":

$ git stash apply ad38abbf76e26c803b27a6079348192d32f52219

When I was new to git, this wasn't clear to me, and I was trying different combinations of "git show", "git apply", "patch", etc.

Solution 4 - Git

To get the 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 gave a title to your stash, replace "WIP" in -grep=WIP at the end of the command with a part of your message, e.g. -grep=Tesselation.

The command is grepping for "WIP" because the default commit message for a stash is in the form WIP on mybranch: [previous-commit-hash] Message of the previous commit.

When you have found the commit, apply it with git stash apply <commit_hash>

Solution 5 - Git

I just constructed a command that helped me find my lost stash commit:

for ref in `find .git/objects | sed -e 's#.git/objects/##' | grep / | tr -d /`; do if [ `git cat-file -t $ref` = "commit" ]; then git show --summary $ref; fi; done | less

This lists all the objects in the .git/objects tree, locates the ones that are of type commit, then shows a summary of each one. From this point it was just a matter of looking through the commits to find an appropriate "WIP on work: 6a9bb2" ("work" is my branch, 619bb2 is a recent commit).

I note that if I use "git stash apply" instead of "git stash pop" I wouldn't have this problem, and if I use "git stash save message" then the commit might have been easier to find.

Update: With Nathan's idea, this becomes shorter:

for ref in `git fsck --unreachable | grep commit | cut -d' ' -f3`; do git show --summary $ref; done | less

Solution 6 - Git

Windows PowerShell equivalent using gitk:

gitk --all $(git fsck --no-reflog | Select-String "(dangling commit )(.*)" | %{ $_.Line.Split(' ')[2] })

There is probably a more efficient way to do this in one pipe, but this does the job.

Solution 7 - Git

git fsck --unreachable | grep commit should show the sha1, although the list it returns might be quite large. git show <sha1> will show if it is the commit you want.

git cherry-pick -m 1 <sha1> will merge the commit onto the current branch.

Solution 8 - Git

If you want to restash a lost stash, you need to find the hash of your lost stash first.

As Aristotle Pagaltzis suggested a git fsck should help you.

Personally I use my log-all alias which show me every commit (recoverable commits) to have a better view of the situation :

git log --graph --decorate --pretty=oneline --abbrev-commit --all $(git fsck --no-reflogs | grep commit | cut -d' ' -f3)

You can do an even faster search if you're looking only for "WIP on" messages.

Once you know your sha1, you simply change your stash reflog to add the old stash :

git update-ref refs/stash ed6721d

You'll probably prefer to have an associated message so a -m

git update-ref -m "$(git log -1 --pretty=format:'%s' ed6721d)" refs/stash ed6721d

And you'll even want to use this as an alias :

restash = !git update-ref -m $(git log -1 --pretty=format:'%s' $1) refs/stash $1

Solution 9 - Git

I liked Aristotle's approach, but didn't like using GITK... as I'm used to using GIT from the command line.

Instead, I took the dangling commits and output the code to a DIFF file for review in my code editor.

git show $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' ) > ~/stash_recovery.diff

Now you can load up the resulting diff/txt file (its in your home folder) into your txt editor and see the actual code and resulting SHA.

Then just use

git stash apply ad38abbf76e26c803b27a6079348192d32f52219

Solution 10 - Git

You can list all unreachable commits by writing this command in terminal -

git fsck --unreachable

Check unreachable commit hash -

git show hash

Finally apply if you find the stashed item -

git stash apply hash

Solution 11 - Git

My favorite is this one-liner:

git log --oneline  $( git fsck --no-reflogs | awk '/dangling commit/ {print $3}' )

This is basically the same idea as this answer but much shorter. Of course, you can still add --graph to get a tree-like display.

When you have found the commit in the list, apply with

git stash apply THE_COMMIT_HASH_FOUND

For me, using --no-reflogs did reveal the lost stash entry, but --unreachable (as found in many other answers) did not.

Run it on git bash when you are under Windows.

Credits: The details of the above commands are taken from https://gist.github.com/joseluisq/7f0f1402f05c45bac10814a9e38f81bf

Solution 12 - Git

In OSX with git v2.6.4, I just run git stash drop accidentally, then I found it by going trough below steps

If you know name of the stash then use:

$ git fsck --unreachable | grep commit | cut -c 20- | xargs git show | grep -B 6 -A 2 <name of the stash>

otherwise you will find ID from the result by manually with:

$ git fsck --unreachable | grep commit | cut -c 20- | xargs git show

Then when you find the commit-id just hit the git stash apply {commit-id}

Hope this helps someone quickly

Solution 13 - Git

Why do people ask this question? Because they don't yet know about or understand the reflog.

Most answers to this question give long commands with options almost nobody will remember. So people come into this question and copy paste whatever they think they need and forget it almost immediately after.

I would advise everyone with this question to just check the reflog (git reflog), not much more than that. Once you see that list of all commits there are a hundred ways to find out what commit you're looking for and to cherry-pick it or create a branch from it. In the process you'll have learned about the reflog and useful options to various basic git commands.

Solution 14 - Git

I couldn't get any of the answers to work on Windows in a simple command window (Windows 7 in my case). awk, grep and Select-string weren't recognized as commands. So I tried a different approach:

  • first run: git fsck --unreachable | findstr "commit"
  • copy the output to notepad
  • find replace "unreachable commit" with start cmd /k git show

will look something like this:

start cmd /k git show 44078733e1b36962571019126243782421fcd8ae
start cmd /k git show ec09069ec893db4ec1901f94eefc8dc606b1dbf1
start cmd /k git show d00aab9198e8b81d052d90720165e48b287c302e```

 - save as a .bat file and run it
 - the script will open a bunch of command windows, showing each commit
 - if you found the one you're looking for, run: `git stash apply (your hash)` 

may not be the best solution, but worked for me

Solution 15 - Git

I want to add to the accepted solution another good way to go through all the changes, when you either don't have gitk available or no X for output.

git fsck --no-reflog | awk '/dangling commit/ {print $3}' > tmp_commits

for h in `cat tmp_commits`; do git show $h | less; done

Then you get all the diffs for those hashes displayed one after another. Press 'q' to get to the next diff.

Solution 16 - Git

You can achieve this in 2 easy steps

  1. List lost stashes --> run this command for a project where all stashes were trashed:

    > git fsck --unreachable | grep commit | cut -d ' ' -f3 | xargs git log > --merges --no-walk

  2. Send a lost stash back where it comes from --> Let’s use the commit hash of the second stash:

    > git update-ref refs/stash 4b3fc45c94caadcc87d783064624585c194f4be8 -m > "My recovered stash"

Solution 17 - Git

The accepted answer by Aristotle will show all reachable commits, including non-stash-like commits. To filter out the noise:

git fsck --no-reflog | \
awk '/dangling commit/ {print $3}' | \
xargs git log --no-walk --format="%H" \
  --grep="WIP on" --min-parents=3 --max-parents=3

This will only include commits which have exactly 3 parent commits (which a stash will have), and whose message includes "WIP on".

Keep in mind, that if you saved your stash with a message (e.g. git stash save "My newly created stash"), this will override the default "WIP on..." message.

You can display more information about each commit, e.g. display the commit message, or pass it to git stash show:

git fsck --no-reflog | \
awk '/dangling commit/ {print $3}' | \
xargs git log --no-walk --format="%H" \
  --grep="WIP on" --min-parents=3 --max-parents=3 | \
xargs -n1 -I '{}' bash -c "\
  git log -1 --format=medium --color=always '{}'; echo; \
  git stash show --color=always '{}'; echo; echo" | \
less -R

Solution 18 - Git

Recovered it by using following steps:

  1. Identify the deleted stash hash code:

    gitk --all $( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

  2. Cherry Pick the Stash:

    git cherry-pick -m 1 $stash_hash_code

  3. Resolve Conflicts if any using:

    git mergetool

Additionally you might be having issues with commit message if you are using gerrit. Please Stash your changes before following next alternatives:

  1. Use hard reset to previous commit and then recommit this change.
  2. You may also stash the change, rebase and recommit.

Solution 19 - Git

To see the commits in terminal, only filtering the ones we care about we can use:

git log --oneline --all --grep="^WIP on .*: [a-f0-9]\+" --grep="^On [^ ]*:" --grep="^index on [^ ]*:" $( env LANG=C git fsck --no-reflog | awk '/dangling commit/ {print $3}' )

This is based on Aristotle Pagaltzis answer.

Solution 20 - Git

What I came here looking for is how to actually get the stash back, regardless of what I have checked out. In particular, I had stashed something, then checked out an older version, then poped it, but the stash was a no-op at that earlier time point, so the stash disappeared; I couldn't just do git stash to push it back on the stack. This worked for me:

$ git checkout somethingOld
$ git stash pop
...
nothing added to commit but untracked files present (use "git add" to track)
Dropped refs/stash@{0} (27f6bd8ba3c4a34f134e12fe69bf69c192f71179)
$ git checkout 27f6bd8ba3c
$ git reset HEAD^    # Make the working tree differ from the parent.
$ git stash # Put the stash back in the stack.
Saved working directory and index state WIP on (no branch): c2be516 Some message.
HEAD is now at c2be516 Some message.
$ git checkout somethingOld # Now we are back where we were.

In retrospect, I should have been using git stash apply not git stash pop. I was doing a bisect and had a little patch that I wanted to apply at every bisect step. Now I'm doing this:

$ git reset --hard; git bisect good; git stash apply
$ # Run tests
$ git reset --hard; git bisect bad; git stash apply
etc.

Solution 21 - Git

This worked for me (in 2022) with recovering my accidently deleted stash in git from a windows environment.

These steps outline how to recover any deleted git stashes or branches (assuming it has not been permanently delete by garbage collection).

  1. Navigate to the directory where your project located.

  2. Enter the command: git fsck --no-reflogs | find "dangling commit" enter image description here

  3. A list of hashes for dangling commits will appear. These will consist of branches and stashes that were deleted. Start with copy and pasting the hashes near the end of the list to find your stash or branch. For example, use the command: git log -1 [hash]

  4. If the corresponding hash matches what you are trying to recover, use the following command to restore it" git stash apply [hash]

Solution 22 - Git

Knowing the approximate file name and it's location, and was able to find dropped stash files grepping dangling commits for path

for i in $(git fsck --no-reflogs | awk '/dangling commit/ {print $3}'); do
  if git log -5 --name-only -u $i | grep -q "<path-to-files>/.*<partial-file-name>.*"; then
    echo "found something in commit $i";
  fi;
done

Solution 23 - Git

You can follow the below process step by step:

1- use below to list all unreachable commits git fsck --unreachable

2- to show unreachable commit hash by git show hash

3- copy all log, you can see log like, unreachable blob, commit, tree.

4- apply git stash with log having commit hash git stash apply [replace hash]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGreg HewgillView Question on Stackoverflow
Solution 1 - GitAristotle PagaltzisView Answer on Stackoverflow
Solution 2 - GitDolda2000View Answer on Stackoverflow
Solution 3 - GitWadeView Answer on Stackoverflow
Solution 4 - GitSenthil A KumarView Answer on Stackoverflow
Solution 5 - GitGreg HewgillView Answer on Stackoverflow
Solution 6 - GitemraginsView Answer on Stackoverflow
Solution 7 - GitNathan JonesView Answer on Stackoverflow
Solution 8 - GitColin HebertView Answer on Stackoverflow
Solution 9 - GitShaheen GhiassyView Answer on Stackoverflow
Solution 10 - GitVivek KumarView Answer on Stackoverflow
Solution 11 - GitAdrian WView Answer on Stackoverflow
Solution 12 - GitCan TecimView Answer on Stackoverflow
Solution 13 - GitRobbyDView Answer on Stackoverflow
Solution 14 - GitKoenView Answer on Stackoverflow
Solution 15 - GitPhilView Answer on Stackoverflow
Solution 16 - GitAbhiView Answer on Stackoverflow
Solution 17 - GitBrad FeehanView Answer on Stackoverflow
Solution 18 - GitAbhijeetView Answer on Stackoverflow
Solution 19 - GitTreviñoView Answer on Stackoverflow
Solution 20 - GitBenView Answer on Stackoverflow
Solution 21 - GitminTwinView Answer on Stackoverflow
Solution 22 - GitanapsixView Answer on Stackoverflow
Solution 23 - GitLovekush VishwakarmaView Answer on Stackoverflow