Can I recover a branch after its deletion in Git?

后端 未结 20 2117
广开言路
广开言路 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:46

    For recovering a deleted branch, First go through the reflog history,

    git reflog -n 60
    

    Where n refers to the last n commits. Then find the proper head and create a branch with that head.

    git branch testbranch HEAD@{30}
    
    0 讨论(0)
  • 2020-11-22 06:48

    Yes, you should be able to do git reflog --no-abbrev and find the SHA1 for the commit at the tip of your deleted branch, then just git checkout [sha]. And once you're at that commit, you can just git checkout -b [branchname] to recreate the branch from there.


    Credit to @Cascabel for this condensed/one-liner version.

    You can do it in one step:

    git checkout -b <branch> <sha>
    
    0 讨论(0)
提交回复
热议问题