Thanks to haste and some sloppy copy-and-paste, today I deleted the wrong remote Git branch. There’s nothing like
learning in a panic, but that’s what happened. Here’s what I learned on how to fix that.
While watching that all-important branch being deleted erroneously is a heart-stopping moment, as it turns out,
restoring the deleted branch is Super Easy. Barely an Inconvenience. All you need to know the hash of the last commit
to the branch. If you just deleted the branch, it’s even easier.
When you delete branch, you should see something like this:
Deleted branch super_important (was 15060e768).
- [deleted] super_important
In this case, you have your hash: 15060e768
If you don’t still have that message available, you can use reflog:
This is a little tricker, as you’ll need to find the hash of the last commit to the branch you want. In this contrived data, it’s
right there at the top. Once you have that, you checkout out that hash, create a new branch (of the same name or different. Your
call), then push to the remote repository.