Sometimes you end up stashing code, then at some point those stashes get cleaned. And one day, you may encounter the situation I faced this week:
...
For further actions, you may consider blocking this person and/or reporting abuse
Saved my day ❤️
On Windows, in a good old command window (your usual
cmd.exe
), step 1. could be translated to:for /f "tokens=3" %a in ('git fsck --unreachable ^| find "commit"') do @git log --merges --no-walk %a
If your Git speaks français, and you're one to choose the more complicated path, you could use something like:
for /f "tokens=1,2,3,4" %a in ('git fsck --unreachable ^| find "commit"') do @if "%c"=="inatteignable" (@git log --merges --no-walk %d) else (@git log --merges --no-walk %c)
or, really, keep it simple with
alias git='LANG=en_GB git'
instead.Bonus
For PowerShell aficionados, here's a command that should work regardless of your Git's locale (well, as long as a commit is still referred to as
commit
):(git fsck --unreachable | Select-String "commit") -split '\s+' |
Select-String -pattern "^[0-9a-fA-F]{40}$" |
ForEach-Object { git log --merges --no-walk $_ }
(Really useful post btw!)
This command for windows saved my life and job. Thankyou so much!!
Thanks! A link to your comment has been added to the article.
This almost worked for me. I found that the
update-ref
command created the stash ref correctly butgit stash list
still did not show anything.I added the
--create-reflog
parameter on a second try, and then things worked.git version 2.22.0
on Ubuntu 18.04Thanks! I don’t have this issue in Git 2.20.1 (macOS 10.14.16) but added a note in the article.
Thanks a lot. This saved me .
Mehdi bro you have no idea how big of a trouble you saved me from. I accidentally stashed and cleared hard work of my several days and night (My mistake I shouldn't have piled up that big). And your post just saved my life. I was almost crying. Thanks a lot dude. God bless you.
Hey Samar, you know what? I feel there's a lot of gratitude in your message, so I want to say thank you for having taken the time to put it down. It sincerely made my day better (and today is not the easiest one for me 😅).
I hope everything's fine with your work, now. Sharing is caring. ✌️
This may fail if you don't use git in English. To quickly fix it:
alias git='LANG=en_GB git'
:o
What is different when you use Git in another language? I didn't know it existed.
In French (I cannot tell for other languages),
commit
seems to have been translated byobjet commit
.git fsck --unreachable
returnsobjet commit inatteignable 977ee79082f2e1179c3d2156f8f0e6c66682ea2d
instead of
unreachable commit 977ee79082f2e1179c3d2156f8f0e6c66682ea2d
Thus,
cut -d ' ' -f3
returnsinatteignable
instead of the commit tag.Ha oui, carrément. :D
Gonna update the article. Thanks a lot!
I tried both commands (git 2.21.1):
git update-ref refs/stash b68ecd901f90158d7c41edf2d2d3868e3599ca29 -m "My recovered stash"
git update-ref refs/stash b68ecd901f90158d7c41edf2d2d3868e3599ca29 --create-reflog -m "My recovered stash"
both give usage (below, removing the '-d' and '-stdin' stuff:
usage: git update-ref [] []
-m reason of the update
--no-deref update not the one it points to
-z stdin has NUL-terminated arguments
--create-reflog create a reflog
Is refs/stash the refname? Or is the sha the refname?
What's the "new-val" and "old-val" - values of what?
This page seems to help:
gist.github.com/joseluisq/7f0f1402...
git log --graph --oneline --decorate ( git fsck --no-reflog | awk '/dangling commit/ {print $3}' )
and then
git stash apply YOUR_WIP_COMMIT_HASH
Thanks for the hope, and being a stepping stone to most of what I needed. :?)
(4 of my files weren't stashed, though. It turns out NASA's backup utility has been silently failing for months, so I've lost a few days work. Under-staffed projects are a pain. Luckily, the project and my attitude don't really matter...)
this saved my life
Mine too!
Thanks!!
It is good to know, after the fist command, pick your hash and use:
$ git stash apply e3cf5932f4816f5b0022190ce6b871f51cf882de
It worked for me
Thanks, it saved my day !!
Thanks a lot lot lot lot lot lot. You saved my time and I recovered 70 - 80 percent of my work :)
saved my butt! thanks!
This was extremely helpful. Thank you for sharing your knowledge!
It never occurred to me you could do this!
Thanks, saved me some work!
Thanks for the solution. It worked for me.
Thanks, i got my changes back while my rebase autostash failed