How to recover a dropped stash in Git?

前端 未结 19 1752
别跟我提以往
别跟我提以往 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:07

    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
    

提交回复
热议问题