Git undo local branch delete

后端 未结 8 1797
慢半拍i
慢半拍i 2020-11-29 14:52

I just deleted the wrong branch with some experimental changes I need with git branch -D branchName.

How do I recover the branch?

8条回答
  •  难免孤独
    2020-11-29 15:18

    First: back up your entire directory, including the .git directory.

    Second: You can use git fsck --lost-found to obtain the ID of the lost commits.

    Third: rebase or merge onto the lost commit.

    Fourth: Always think twice before using -D or --force with git :)

    You could also read this good discussion of how to recover from this kind of error.

    EDIT: By the way, don't run git gc (or allow it to run by itself - i.e. don't run git fetch or anything similar) or you may lose your commits for ever.

提交回复
热议问题