• Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Data Recovery Pit

The Data Recovery Expert

  • Best
  • Guides
  • Reviews
Home / Guides

June 14, 2022 Pramesh Khanna

How to Recover GitHub Branch

GitHub Branch

In GitHub, a branch or commit is simply a way to separate your code from the main project, and sometimes you need to recover it. When you want to add a new feature or fix a bug in your project, you create a new branch off of the master one. This way, you can work on your code without affecting the main project. Once you’re done, you can merge these branches into the master one. GitHub branches are incredibly useful because they allow you to work on new features or fixes without affecting the primary project.

This way, if something goes wrong with your code, you can simply delete the branch and start over. It also allows multiple developers to work on different parts of the same project at the same time without stepping on each other’s toes. But issues can arise when you accidentally delete or reset a branch. Luckily, there is a way to recover GitHub branch. The only solution is to use “Reflog.” But first, you should familiarize yourself with that term.

Table of Contents

  • What is Reflog?
  • How to Undo Deleting a Branch in GitHub
  • Alternative Method to Recover GitHub Branch

What is Reflog?

Reflog is a “journal” that keeps track of all head pointer movements, such as when a checkout, commit, merge, rebase, reset, and so on. In other words, whenever something significant happens in Git, a new entry is generated in the reflog. This gives you a chance to “go back in time” and find out what happened. Reflog stands for “reference log”. It is a history of your recent commits and branching activities. This allows you to easily check when you last made a change to a file or whether you have already pushed a certain commit.

Reflog is also useful for undoing mistakes, as it lets you see exactly what commands were run and in which order. The Reflog is automatically sorted chronologically by default. This implies that the most recent action will be shown at the top. By looking at it, you can quickly determine whether an operation was a commit, pull, checkout, or something else. This is a perfect solution to undoing mistakes in GitHub commits.

How to Undo Deleting a Branch in GitHub

You must delete outdated branches from your Git repository regularly to maintain it clean and orderly. You may have deleted a branch prematurely because you were in a hurry. This is an excellent illustration of the Reflog coming in handy. Let’s assume the “feature/login” branch is no longer necessary. You’ll not only lose the branch after deleting it in a second but also lose this commit because its most recent addition isn’t anywhere else. But, first, close your eyes and remove the branch; let the disaster unfold naturally. However, because it’s your current HEAD branch, you must first move to another one before we can remove it:

# switch away from the branch, to master
$ git switch master

# delete the unwanted feature branch
$ git branch -D feature/login

Note that you must use the “-D” option when deleting a branch since it included unresolved changes (the most recent commit of which is non-existent elsewhere). Therefore, the branch has been removed. Let’s try to reverse this mistake by opening the Reflog.

$ git reflog
776f8ca (HEAD -> master) HEAD@{0}: checkout: moving from feature/login to master
b1c249b HEAD@{1}: checkout: moving from master to feature/login
776f8ca (HEAD -> master) HEAD@{2}: checkout: moving from feature/login to master
b1c249b HEAD@{3}: Branch: renamed refs/heads/develop to refs/heads/feature/login
b1c249b HEAD@{5}: commit: Change Imprint page title

Take a second to reflect on what happened now: before deleting the branch, you switched to master. This is the most recent item that has been protocoled in the Reflog. So now, you may simply revert to the state before that latest entry by using “git branch” and copying the necessary commit hash from the Reflog:

$ git branch feature/login b1c249b

With this code, you can recover the GitHub branch.

Alternative Method to Recover GitHub Branch

When you need to recover deleted commits, the Reflog can be quite beneficial. If you’re using a desktop graphical user interface like Tower, follow these steps:

Reset the latest version of your current HEAD branch to this older revision and “delete” any subsequent commits. It’s that simple.

$ git reset –hard 2b504bee

What if you realize this was a mistake and need those erased commits back? Let’s take another look at the Reflog to see whether it might be of assistance:

$ git reflog
2b504be (HEAD -> master, origin/staging) HEAD@{0}: reset: moving to 2b504bee
26acef5 HEAD@{1}: commit (amend): Move new product page
83bfa7d HEAD@{2}: commit (amend): Move product page

The “reset” operation you just completed is the latest entry in the Reflog. You can simply return to the prior state if you wish to reverse this action and reclaim your previous commits! Therefore, let’s take note of the commit hash for that second entry and repeat “git reset”:

$ git reset --hard 26acef5

That’s about it; You can compare the two commits for yourself. If you read the “git reflog”, you’ll notice that our seemingly lost commits are recovered and once again available on our HEAD branch.

Next Article
How to Recover Deleted Calendar Events on iPhone
How to Recover GRUB2 When Ubuntu Won’t Boot?

Primary Sidebar

Author

Pramesh Khanna

Pramesh is a tech expert, written more than 500 data recovery articles for different publications. His experience in a computer shop where he was in charge of recovering files from hard drives introduced him to the world of data recovery.


LinkedInTwitter
Related Posts

Footer

data-recovery-pit-logo
Info
  • About
  • Privacy Policy
  • Terms and Conditions
  • Contact Us
Site
  • Sitemap
  • Services
  • Advertise
  • Disclaimer
  • Facebook
  • Twitter
  • Pinterest

Copyright © 2025 · Data Recovery Pit

This site uses cookies to serve you better. By continuing to use this website, you agree to our cookie and Privacy Policy.