git: Undo a local branch delete

Wajeeh Ahsan
Feb 25, 2021

--

When you delete a git branch and want it back, simplest way is to fetch the branch again.

git fetch origin branchNmae

Wrongly, I just deleted my master branch. Being master parent of all branches, I wasn’t able to just fetch it. I was left with only one option. Clone the project again and set it up all :(

After making queries on google, I cam to know a workaround. The following command worked for me

git branch branchName <sha1> 

git branch -D tells you the sha1

Look at the following commands

user@MY-PC /C/MyRepo (master)
$ git branch -D master2
Deleted branch master2 (was 130d7ba). <-- This is the SHA1 we need to restore it!

user@MY-PC /C/MyRepo (master)
$ git branch master2 130d7ba

BOOM!

--

--

Wajeeh Ahsan
Wajeeh Ahsan

No responses yet