Quantcast
Channel: How do I recover a dropped stash in Git? - Stack Overflow
Viewing all articles
Browse latest Browse all 55

Answer by Jack G for How do I recover a dropped stash in Git?

$
0
0

Although the parents of unreachable commits are gone, we can difference against the current working tree (assuming you have a rough idea of what changes have happened in between loosing the stash and where the you're at right now):

git fsck --no-reflog --unreachable | grep commit | cut -d" " -f3 | xargs git log --oneline --no-abbrev-commit --no-walk | grep -Ee '^.{40} WIP ' | xargs -roL1 /bin/sh -c 'git log -1 --no-walk "$0";echo;git --no-pager diff --stat --line-prefix=\  $0;echo'
  • Protip: for the best results, ensure you have a clean working tree (possibly stash any unsaved changes), then git checkout to the branch/commit you recall being nearest/closest to the stash you lost. The above command diffs against your current working tree, which will be much more meaningful closer to the lost stash.
  • Protip: if the command isn't working because you're on Windows like @Koen, do yourself a favor and install Linux Mint Cinnamon, open the command prompt, and run sudo apt install -y git. Then, you can open your Windows partition from the file manager and open the terminal in the directory of your git project on the Windows partition. I've only had bad experiences with git on Windows and assure you the 15-30 minutes it takes to install Linux Mint will save you 15-30 hours over the next few weeks.
  • Protip: if the command is taking forever to run like @jthill mentioned, make sure you're using an up-to-date version of git on a well-optimized operating system like Linux or OpenBSD.

For me, this shows a pretty colored list of the form:

commit 71ddcf053e31d3a7203ea5a4f081c62f93b8053cMerge: 744545e 8b46248Author: anonyco <25795277+anonyco@users.noreply.github.com>Date:   Sat Sep 27 21:57:14 2025 -0400    WIP on (no branch): 744545e Fixup formatting with cargo fmt  src/lib.rs                    |  52 ++++++++++++++++++++++++  src/providers/cloudflare.rs   | 110 ++++++++++++++++++++++++++++++++++++++++-----------  src/providers/digitalocean.rs |  54 +++++++++++++++++++++----  src/providers/ovh.rs          | 432 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------------------------------------------------------------------  src/tests/ovh_tests.rs        |  30 +++++---------  5 files changed, 441 insertions(+), 237 deletions(-)commit 398ed235a7900b52b8297d3ec7b7047e7aa046bfMerge: 4303412 dddc52aAuthor: anonyco <25795277+anonyco@users.noreply.github.com>...

Notice: You probably don't want to do git stash apply <commit> because a mess of nasty merge conflicts will ensure. Here's alternatives you can try to selectively recover the content:

  • Use git checkout -b tmp_branch <commit> to create a branch named tmp_branch for the commit and switch both the worktree and index to it. This will make things a lot easier and you can always delete the branch later with git branch -D tmp_branch_name
  • Use git --no-pager show <commit>:/path/to/file to show the contents of /path/to/file
  • Use git checkout <commit> /path/to/file to
  • Use git restore --source=<commit> . to completely override all files in your current branch with the contents of the lost stash.

Viewing all articles
Browse latest Browse all 55

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>