Can I recover a branch after its deletion in Git?

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

    From my understanding if the branch to be deleted can be reached by another branch, you can delete it safely using

    git branch -d [branch]
    

    and your work is not lost. Remember that a branch is not a snapshot, but a pointer to one. So when you delete a branch you delete a pointer.

    You won't even lose work if you delete a branch which cannot be reached by another one. Of course it won't be as easy as checking out the commit hash, but you can still do it. That's why Git is unable to delete a branch which cannot be reached by using -d. Instead you have to use

    git branch -D [branch]
    

    This is part of a must watch video from Scott Chacon about Git. Check minute 58:00 when he talks about branches and how delete them.

    Introduction to Git with Scott Chacon of GitHub

提交回复
热议问题