DEV Community

Sonia Singla
Sonia Singla

Posted on • Originally published at soniasingla.com

How to restore local branch in GitHub?

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

$ git reflog
Enter fullscreen mode Exit fullscreen mode

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>
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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

Discussion (0)