Coming Up for Air

Restoring a Deleted Git Branch

Tuesday, February 25, 2020 |

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).
To github.com:yourcompany/super_secret_project.git
 - [deleted]             super_important

In this case, you have your hash: 15060e768

If you don’t still have that message available, you can use reflog:

$ git reflog
15060e768 HEAD@{0}: commit: Fix test
84a524673 HEAD@{1}: commit: Really important work
7a2f75f1f HEAD@{2}: commit: So much I forgot what I did
f2a2afa24 HEAD@{3}: commit: Added dependencies
224260b95 HEAD@{4}: commit: Implement new knob
68ae60023 HEAD@{5}: checkout: moving from foo to dev

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.

$ git checkout 15060e768
$ git checkout -b super_important
$ git push origin super_important

You can also shorthand that a bit:

$ git checkout -b super_important 15060e768
$ git push origin super_important

You should now see your branch in your remote repository. Take a deep breath. Crisis averted. :)

Search

    Quotes

    Sample quote

    Quote source

    About

    My name is Jason Lee. I am a software developer living in the middle of Oklahoma. I’ve been a professional developer since 1997, using a variety of languages, including Java, Javascript, PHP, Python, Delphi, and even a bit of C#. I currently work for Red Hat on the WildFly/EAP team, where, among other things, I maintain integrations for some MicroProfile specs, OpenTelemetry, Micrometer, Jakarta Faces, and Bean Validation. (Full resume here. LinkedIn profile)

    I am the president of the Oklahoma City JUG, and an occasional speaker at the JUG and a variety of technical conferences.

    On the personal side, I’m active in my church, and enjoy bass guitar, running, fishing, and a variety of martial arts. I’m also married to a beautiful woman, and have two boys, who, thankfully, look like their mother.

    My Links

    Publications