2420

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?

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
  • 101
    Note for the future: If you don't want to lose your stashes each time you `git stash pop`, you can do `git stash apply` instead. It does the same thing, except it doesn't remove the reference to the applied stash. – Kevin Jul 10 '13 at 17:12
  • 15
    Tried everything here, couldn't find a stash that had been popped already. So glad for IntelliJ's https://www.jetbrains.com/help/idea/local-history.html – Ruan Mendes May 29 '18 at 12:59
  • Also see [How to recover stashed uncommitted changes](https://stackoverflow.com/questions/19003009/how-to-recover-stashed-uncommitted-changes/19003191#19003191) – mfaani Mar 21 '19 at 14:55
  • 5
    Suggestion: [Avoid using `git stash` for anything you aren't willing to lose](https://stackoverflow.com/a/32434045/86967). If it's worth saving, then it's worth making a full commit (possibly on a separate temp branch). With `git commit`, your "stashes" are much easier to keep track of. For one thing, you can include a commit message. But more relevant to this question, your changes will be accessible in the local reflog -- [even if you reset/delete the branch](https://stackoverflow.com/questions/3640764/can-i-recover-a-branch-after-its-deletion-in-git/30598953#30598953). – Brent Bradburn Apr 27 '22 at 23:00
  • I agree with Brent, however you can add a message to your git stash entries using `git stash push -m "my stash message..."` to better organise them. – James Hooper Sep 08 '22 at 14:10
  • Thank a ton for asking this question and for the people who have answered it. This doesn't happen for me but it happened twice in 2 weeks for me that my muscle memory typed in `git stash clear` to clear cluttered stash. This thread helped me recover the stash commit. – Aman Sep 22 '22 at 11:23

24 Answers24

3902

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.

Pang
  • 9,564
  • 146
  • 81
  • 122
Aristotle Pagaltzis
  • 112,955
  • 23
  • 98
  • 97
  • 107
    Jaydel took the words out of my mouth. This post saved my job :) I would just like to add - remembering the date you worked on whatever you lost makes it easier to browse gitk for what you are looking for. – Sridhar Sarnobat Sep 04 '15 at 17:52
  • Any reason why I would get the following message when trying to run that command?: awk : The term 'awk' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:24 – Codey Sep 30 '15 at 18:51
  • 4
    @Codey: Because PowerShell. I don’t know if MsysGit ships an AWK binary. Googling tells me that something like `%{ $_.Split(' ')[2]; }` should do the equivalent of the `{print $3}` in that `awk` command in PowerShell, but I don’t have a Windows system to test that, and you still need an equivalent for the `/dangling commit/` part. Anyway, just run `git fsck --no-reflog` and look at the output. You want the hashes from the “dangling commit ” lines. – Aristotle Pagaltzis Oct 02 '15 at 03:55
  • 8
    It's worth mentioning that the commit message will only have the string "WIP" if you didn't provide your own message when stashing (i.e. by doing `git stash save ""`). – Samir Aguiar Jul 29 '16 at 01:19
  • 1
    This is a modified version of the Windows PowerShell script that will show the summaries also: git fsck --no-reflog | select-string 'dangling commit' | foreach { $bits = $_ -split ' '; $bits[2]; } | foreach { git show --summary $_ } – pwhe23 Jan 25 '17 at 18:43
  • 1
    If you get a message saying something like `argument list is too long`, just add another pipe to `head -n 100` to restrict the amount of commits. – Samir Aguiar Feb 20 '17 at 19:03
  • 39
    If you know when the drop happened, you can use this one-liner to get the list of dangling commits by increasing time: `git fsck --no-reflog | awk '/dangling commit/ {print $3}' | xargs -L 1 git --no-pager show -s --format="%ci %H" | sort` the last entry is probably the one you want to `stash apply`. – ris8_allo_zen0 May 15 '17 at 09:31
  • 23
    `git stash apply {ref}` restored a dropped stash! `git` is so great it should be illegal! – Tom Russell Nov 04 '17 at 07:32
  • `git stash pop` does not (or no longer) prints a hash value. At least not if there are conflicts. No idea if it prints it in other cases. – gman Feb 05 '18 at 09:36
  • 1
    @AristotlePagaltzis FYI, I reordered your answer to be magical and shiny!(TM) Often enough, finding the hash is going to be the obvious/easy part, so no need to put that first. – jpaugh Feb 14 '19 at 19:17
  • I don't even know what this magic spell does but I confirm that after running it, the matrix appeared before me with my lost stash. I want to cry of happiness :`) – Alejandro Moreno Jan 10 '20 at 19:13
  • For PowerShell, rather than piping through `select-string 'dangling commit' | foreach { $bits = $_ -split ' '; echo $bits[2];}`, it would be slightly clearer to say `Select-String "dangling commit (.*)" | ForEach {$_.Matches.Groups[1].Value}` (given that matching groups are numbered from 1). And I can confirm that this works on PowerShell 6. – Andrew Spencer Feb 25 '20 at 09:59
  • 1
    You can use "git stash show " to see the change before apply. – Felipe May 28 '20 at 04:13
  • 3
    You just saved hours of my work, I just popped a stash and accidentally wrote `git checkout .` instead of `git reset .` lol After that I looked on `git reflog` and discovered that stashing does not create a reference there... I was desperate until I found this answer, really thanks!!! – Iuji Ujisato May 28 '20 at 07:14
  • 1
    That worked like a charm, but is giving only hashes, which is difficult to identify which one you want. Below will help you to get details like Author, Date, Stash/Commit message. git fsck --no-reflog | awk '/dangling commit/ {print $3 "^!"}' | xargs git log – Viswa Teja Kuncham Jun 25 '20 at 06:46
  • 1
    Just FYI I think if you stashed using Github Desktop, it'd say $$GitHub_Desktop instead of WIP – user1114 Jun 27 '20 at 19:06
  • Use `LANG=C` if you're using a different locale than English – Treviño Feb 17 '21 at 19:51
  • You can go one step further, and filter summaries by the text stash adds to the beggining of the message. This cut the results from 25 to 3 for me. In English & Powershell, this is: `git show --summary $(git fsck --no-reflog | select-string 'dangling commit' | foreach { $_.ToString().Split(" ")[2] }) | select-string -CaseSensitive "On [\w/-]+:"` – LeBleu Feb 02 '23 at 16:53
  • As of 2023, this still happens and `git fsck --dangling` is an actual command ':-) – Pipetus May 11 '23 at 18:59
822

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.

outis
  • 75,655
  • 22
  • 151
  • 221
Dolda2000
  • 25,216
  • 4
  • 51
  • 92
303

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.

Wade
  • 3,585
  • 2
  • 22
  • 27
  • 4
    Do note that this applies (duh!) the stash to the current working tree. If the tree is dirty, you might want to either use a temporary branch or stash first, apply the stash from the SHA-1, stash again and then pop the second to last stash (called stash@{1}). – musiKk Mar 24 '14 at 10:28
254

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>

blub
  • 8,757
  • 4
  • 27
  • 38
Senthil A Kumar
  • 10,306
  • 15
  • 44
  • 55
  • 2
    echo 'git fsck --unreachable | grep commit | cut -d" " -f3 | xargs git log --merges --no-walk --grep=WIP' >/usr/local/bin/git-stashlog; chmod a+rx /usr/local/bin/git-stashlog # git stashlog – Erik Martino Jun 24 '14 at 06:48
  • 1
    Or you can add this to your .gitconfig as an alias (precede the command with a `!`). – asmeurer Dec 01 '15 at 22:04
  • 2
    This worked for finding the lost commit instead of the accepted answer! And when you find the commit, apply it with `git stash apply ` – blub Apr 26 '22 at 09:11
  • 2
    If OS is in another language, this might fail. In French for instance we have to use `cut -d" " -f4` to match the sha1 in "objet commit inatteignable 12f1d8d92b703b220c37403d0eb83dba5e273551" – JM Lord Jul 19 '22 at 17:42
  • 1
    This rescued me from losing a whole morning's work. I am never popping stashes again – Xiang Wei Huang Aug 14 '23 at 06:57
99

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
Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
94

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.

emragins
  • 4,607
  • 2
  • 33
  • 48
58

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.

Nathan Jones
  • 5,854
  • 1
  • 18
  • 7
49

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
ericbn
  • 10,163
  • 3
  • 47
  • 55
Colin Hebert
  • 91,525
  • 15
  • 160
  • 151
25

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

Adrian W
  • 4,563
  • 11
  • 38
  • 52
  • This worked well for me on Mac OSX, git version 2.31.1 whereas `gitk` produced an error when trying to open a window. I also tried the credited link but at time of writing this comment it contained a syntax error in the command, missing a $ before the parenthesis. It's also perhaps worth mentioning that if you can remember at least part of the stash name you can pipe the output through `grep` to find it. – Glenn Lawrence Feb 23 '22 at 11:31
24

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
Shaheen Ghiassy
  • 7,397
  • 3
  • 40
  • 40
24

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
Vivek Kumar
  • 2,625
  • 2
  • 25
  • 33
20

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

Can Tecim
  • 577
  • 8
  • 18
19

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.

RobbyD
  • 513
  • 3
  • 10
  • 2
    Hi Robby. This is relevant if you were working, got side-tracked, and need to pick back up where you left off a couple weeks ago only to learn you can't find your stashed work -- it probably got lost somewhere in that other stuff you were doing. reflog is great if it's recent history, but not for long time gaps. – emragins Jun 20 '17 at 20:30
  • 1
    Hey emragins, I agree, but this was exactly the OP's use case. I don't know for sure how the other commands being posted here would behave, but my geuss would be that they'd also stop working once the ref to his stashed commit gets cleaned up. – RobbyD Jul 20 '17 at 08:50
  • 1
    Hmm... the scenario above was what brought me to this question, and I know it was at least a couple weeks if not even closer to a month between when I (unknowingly) lost my stash and when I was able to recover it. – emragins Jul 21 '17 at 23:44
  • 1
    this looks neat, but... it didn't list the commit for my (recently) deleted stash - I found it instead with one of the "cursed other" solutions; sure I don't understand/remember everything in their commands, but it solved my problem, and I can always come back to this page (just like any SO)! Learning about another command that "might work" isn't necessary if another solution was found. I'm not 100%, but this RobbyD solution [doesn't seem to cover stashes](https://stackoverflow.com/questions/89332/how-to-recover-a-dropped-stash-in-git#comment109760261_91795). – cellepo Aug 19 '21 at 00:52
  • @cellepo you probably need to add --all - as in __git reflog --all __ -- to your reflog command. AFAIK by default reflog filters out stash events. – James Moore Sep 03 '21 at 14:45
  • Upvoted because getting yourself into this situation should be a learning experience. Don’t get yourself into situations that require strange incantations just to _undo_. – Guildenstern Apr 20 '23 at 08:16
18

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 8506d235f935b92df65d58e7d75e9441220537a4 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

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Koen
  • 381
  • 2
  • 5
  • You can use git bash even on Windows. In git bash you have all the (unixoid) command line tools you need. – Adrian W Aug 26 '19 at 07:55
14

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.

Phil
  • 3,282
  • 1
  • 20
  • 16
12

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
Brad Feehan
  • 121
  • 1
  • 3
9

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]

minTwin
  • 1,181
  • 2
  • 21
  • 35
6

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.

Treviño
  • 2,999
  • 3
  • 28
  • 23
5

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.
Ben
  • 9,184
  • 1
  • 43
  • 56
  • is this an answer, or the continuation of the question? – Alex Brown Jan 15 '14 at 06:52
  • A bit of both. I found this page because I lost a stash and was trying to get it back. The use case for me is doing a bisect where I want to apply a change before testing at each step. I learned the hard way that you can't just pop, test, stash, bisect because that can leave a different commit on the stash, hence `stash apply`. – Ben Jan 15 '14 at 12:50
5

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.
miva2
  • 2,111
  • 25
  • 34
Abhijeet
  • 8,561
  • 5
  • 70
  • 76
  • @miva2 your edit removed the link to the most correct answer in this question. Adding the link back in the comment http://stackoverflow.com/questions/89332/how-to-recover-a-dropped-stash-in-git/91795#91795 – Abhijeet Feb 19 '16 at 07:47
4

To see only stash commits, where they attached, and what their contents are

result sample

Checking object directories: 100% (256/256), done.
2022-08-31 10:20:46 +0900 8d02f61 WIP on master: 243b594 add css
A       favicon.ico

command

git fsck --dangling | awk '/dangling commit/ {print $3}' | xargs -L 1 git --no-pager show -s --format="%ct %h" | sort | awk '{print $2}' | { while read hash; do status=$(git stash show $hash --name-status 2>/dev/null); if (( $? == 0 )); then git show $hash -s --format="%C(green)%ci %C(yellow)%h %C(blue)%B"; echo "$status"; fi; done; }
  • To see full hash, change %h to %H
  • To reduce time, tail fsck like git fsck --dangling | tail -100 | awk ...

recover sample enter image description here

Changdae Park
  • 911
  • 1
  • 9
  • 22
1

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
anapsix
  • 1,847
  • 20
  • 18
0

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]

Lovekush Vishwakarma
  • 3,035
  • 23
  • 25
0

Not exactly an answer to get a stash but if the goal is to get uncommited changes that were first stashed and then popped in another branch but meant for both and done in the following way:

  1. Make changes in branch_a
  2. git stash
  3. Make branch_b from branch_a
  4. git stash apply

Then to restore the uncommited changes to branch_a:

  1. git checkout branch_a
  2. git merge branch_b
  3. git reset HEAD~1
askepott
  • 250
  • 3
  • 13