Sonia Singla

How to restore local branch in GitHub?

May 12, 2020 • ☕️ 1 min read

To find out the sha of the latest commit, run the command :

$ git reflog

It will provide all the HEADs of all branches. After this, simply run the command given below to recreate a branch using sha :

$ git branch branchName <sha>

Voila ! This is how you can restore local branches in GitHub 💡

WORST CASE :

In my case, I re-installed the OS which resulted in deleting all the local branches of my repository. Even git reflog was not able to save me this time 😓. I lost sha of all the commits and all the local branches of my repository. So, after brainstorming, I landed up with the solution described below :

$ git checkout master
# Switched to branch 'master'

$ git fetch --all
# updates local copies of remote branches

$ git branch -a
# List all your branches

$ git checkout branch-name
# Switched to branch 'branch-name'

$ git log
# View the commit history

Note : This solution is also useful if you are cloning the repository on new/other laptop or you ended fucking up the re-base so badly that you deleted the repository and cloned a new one 😂 , which might result in deleting all the local branches of your repository and worst case, you even lost sha of the commits 🤷🏻‍♀️

Happy Git ! ❤️


Relative Posts:

Week 00 - CNCF Internship with Thanos

May 27, 2020

Outreachy Internship with Mozilla - Final Report

March 17, 2020

Hi 👋🏻, My name is Sonia, 20 yo, studying Information Technology from Panjab University, India. I am passionate about developer tooling and developer communities. Currently, working with Embark Studios on a new platform for gamemaking. I was the CNCF 2020 Intern at Thanos, and spent the winter of 2019 working with web-compatibility team of Mozilla as an Outreachy Intern. I also love to travel, teach, and speak about open source 👩🏻‍💻

RotateLinkImg-iconRotateLinkImg-icon