Skip to content

Instantly share code, notes, and snippets.

@eagafonov
Last active March 20, 2019 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eagafonov/bfc7eaa2fade52dc3570c24a3e39d69b to your computer and use it in GitHub Desktop.
Save eagafonov/bfc7eaa2fade52dc3570c24a3e39d69b to your computer and use it in GitHub Desktop.
How to recover deleted stashes

How to recover deleted stash commits

TL;DR

git fsck --unreachable | grep commit | cut -d" " -f3 | xargs git log --merges --no-walk --grep=WIP

Original SO post.

Explanation

Plan of attack

  • find all unreferenced commits
  • filter commits (drop blobs, etc..)
  • get commit hashes
  • fetch and examine commit message

Find all unreferenced commits

git fsck --unreachable.

Sample output

unreachable blob 862f102361a60d0b5a36db173cbe0d880f49cc69
unreachable blob 983d9095856b818373d0098a997028665046b14f
unreachable blob b35aa00b801b349bccef85d7fe1e602b938378fb
unreachable blob 539310183a288b7db2988dfe16b701cdb45f86cb
unreachable blob ef1911629622fa83d4a6f33990929282521de80c
unreachable commit ca3ff1cb3a8ec458fdecac2ed19d8e8d120fd04c
unreachable commit 4247b10cbcc8ab419d396d439fe9339b9e92b0d6
unreachable blob 9153b161e8b63daa6c333c3cd5740b69783ce033
unreachable commit 7d70e1179dc88a3f8079d0f068d6545e89f42b17
unreachable blob 229da12d5f63ccc10a13d753f4569c776cb2bb66
unreachable blob d0b8818c3fd921ff8c1d426ab518e73899949fdb
unreachable commit 502482fe84dc1fd3ec0c4608442dac39fdf5976b
unreachable blob 6165c2abfc586af3bf54ad88dd7c622180661f1c
unreachable commit c6a322322fdc936c80040fbd3bc6ffc258c16aeb
unreachable commit c7d262cea731131dffb81036821de17817eb1cd8
unreachable blob e2449375c6dd91fafa712dbf5f21e0e25e50bb6a

Filter commit objects

grep commit

Sample output

unreachable commit ca3ff1cb3a8ec458fdecac2ed19d8e8d120fd04c
unreachable commit 4247b10cbcc8ab419d396d439fe9339b9e92b0d6
unreachable commit 7d70e1179dc88a3f8079d0f068d6545e89f42b17
unreachable commit 502482fe84dc1fd3ec0c4608442dac39fdf5976b
unreachable commit c6a322322fdc936c80040fbd3bc6ffc258c16aeb
unreachable commit c7d262cea731131dffb81036821de17817eb1cd8

Extract commit hash

cut -d" " -f3

cut splits each input line into field list using space character (-d" ") and outputs 3rd element of the list.

Sample output

ca3ff1cb3a8ec458fdecac2ed19d8e8d120fd04c
4247b10cbcc8ab419d396d439fe9339b9e92b0d6
7d70e1179dc88a3f8079d0f068d6545e89f42b17
502482fe84dc1fd3ec0c4608442dac39fdf5976b
c6a322322fdc936c80040fbd3bc6ffc258c16aeb
c7d262cea731131dffb81036821de17817eb1cd8

Get commit messages of discovered commit + filter the commits

man xargs
xargs - build and execute command lines from standard input
...

xargs applies command git log --merges --no-walk --grep=WIP for each commit hash read after cut from step 3.

  • --merges prints only merge commits. It expolites the fact all stash commit has two parents i.e. its a merge commit
  • --no-walk it shows the given commit only, not predecessors
  • --grep=WIP filters stash commit message. It include all commit messages prefixed with WIP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment