DEV Community

Sonia Singla
Sonia Singla

Posted on β€’ Originally published at soniasingla.com

2

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

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay