Can I recover a branch after its deletion in Git?

后端 未结 20 2101
广开言路
广开言路 2020-11-22 05:53

If I run git branch -d XYZ, is there a way to recover the branch? Is there a way to go back as if I didn\'t run the delete branch command?

20条回答
  •  感情败类
    2020-11-22 06:38

    I used the following commands to find and retrieve my deleted branch. The first steps are from gcb's description.

    $ git fsck --full --no-reflogs --unreachable --lost-found > lost
    $ cat lost | cut -d\  -f3 > commits
    $ cat commits | xargs -n 1 git log -n 1 --pretty=oneline
    

    Now look for the git commit id (GIT-SHA) based on the commit comments and use it in the command below. Checkout a new branch called NEW-BRANCH with the previously found GIT-SHA:

    $ git checkout -b NEW-BRANCH GIT-SHA
    

提交回复
热议问题