How to recover a dropped stash in Git?

前端 未结 19 1707
别跟我提以往
别跟我提以往 2020-11-22 01:42

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

19条回答
  •  灰色年华
    2020-11-22 02:06

    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
    

提交回复
热议问题