Can I recover a branch after its deletion in Git?

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

    If you don't have a reflog, eg. because you're working in a bare repository which does not have the reflog enabled and the commit you want to recover was created recently, another option is to find recently created commit objects and look through them.

    From inside the .git/objects directory run:

    find . -ctime -12h -type f | sed 's/[./]//g' | git cat-file --batch-check | grep commit
    

    This finds all objects (commits, files, tags etc.) created in the last 12 hours and filters them to show only commits. Checking these is then a quick process.

    I'd try the git-ressurect.sh script mentioned in Jakub's answer first though.

提交回复
热议问题