SONIA SINGLA
Sonia Singla πŸͺ²πŸ‘©πŸ»β€πŸ’»

Sonia Singla πŸͺ²πŸ‘©πŸ»β€πŸ’»

How to restore local branch in GitHub?

How to restore local branch in GitHub?

SONIA SINGLA's photo
SONIA SINGLA

Published on May 12, 2020

2 min read

Subscribe to my newsletter and never miss my upcoming articles

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 ! ❀️

Β 
Share this
Proudly part of