Don't Panic
Last Friday, I accidentally deleted the wrong git branch. That’s right, I’m a silly, silly programmer.
Fortunately for me, git is the most well-thought piece of software ever and Linus Torval knew that this is the kind of mistakes I make, so he create git reflog.
git reflog is a command to manage reference logs, which keep track of the reason why the ref was updated.
The git reflog show
subcommand shows the ref log. It is then possible to recover the deleted branch by checking out the desired ref by using git checkout <sha>
. This checkouts the ref, and git checkout -b <branch>
will bring the branch back.
Thank you git!
##tl;dr: How to recover a deleted branch:
git reflog
git checkout -b <branch> <sha>