Git undo local branch delete

GitBranchGit Branch

Git Problem Overview


I just deleted the wrong branch with some experimental changes I need with git branch -D branchName.

How do I recover the branch?

Git Solutions


Solution 1 - Git

You can use git reflog to find the SHA1 of the last commit of the branch. From that point, you can recreate a branch using

git branch branchName <sha1>

Edit: As @seagullJS says, the branch -D command tells you the sha1, so if you haven't closed the terminal yet it becomes real easy. For example this deletes and then immediately restores a branch named master2:

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

Solution 2 - Git

If you know the last SHA1 of the branch, you can try

git branch branchName <SHA1>

You can find the SHA1 using git reflog, described in the solution --defect link--.

Solution 3 - Git

If you haven't push the deletion yet, you can simply do :

$ git checkout deletedBranchName

Solution 4 - Git

If you just deleted the branch, you will see something like this in your terminal:

Deleted branch branch_name(was e562d13)

> - where e562d13 is a unique ID (a.k.a. the "SHA" or "hash"), with this you can restore the deleted branch.

To restore the branch, use:

git checkout -b <branch_name> <sha>

for example:

git checkout -b branch_name e562d13 

Solution 5 - Git

Follow these Steps:

1: Enter:

git reflog show 

This will display all the Commit history, you need to select the sha-1 that has the last commit that you want to get back

2: create a branch name with the Sha-1 ID you selected eg: 8c87714

git branch your-branch-name 8c87714

Solution 6 - Git

First: back up your entire directory, including the .git directory.

Second: You can use git fsck --lost-found to obtain the ID of the lost commits.

Third: rebase or merge onto the lost commit.

Fourth: Always think twice before using -D or --force with git :)

You could also read this good discussion of how to recover from this kind of error.

EDIT: By the way, don't run git gc (or allow it to run by itself - i.e. don't run git fetch or anything similar) or you may lose your commits for ever.

Solution 7 - Git

Thanks, this worked.

> git branch new_branch_name sha1

> git checkout new_branch_name

//can see my old checked in files in my old branch

Solution 8 - Git

If you deleted a branch via Source Tree, you could easily find the SHA1 of the deleted branch by going to View -> Show Command History.

It should have the next format:

Deleting branch ...
...
Deleted branch %NAME% (was %SHA1%)
...

Then just follow the original answer.

git branch branchName <sha1>

Solution 9 - Git

This worked for me:

git fsck --full --no-reflogs --unreachable --lost-found
git show d6e883ff45be514397dcb641c5a914f40b938c86
git branch helpme 15e521b0f716269718bb4e4edc81442a6c11c139

Solution 10 - Git

if you deleted a branch using GUI of a Jetbrains IDE(Goland, phpstorm etc)

go to

git windows(left-down corner of IDE) -> console tab -> now you can see log of executed commands by IDE and find the branch name and SHA1 from this log

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionStefan KendallView Question on Stackoverflow
Solution 1 - GitbobDevilView Answer on Stackoverflow
Solution 2 - GitChetanView Answer on Stackoverflow
Solution 3 - GitamichaudView Answer on Stackoverflow
Solution 4 - GitdeveloperickView Answer on Stackoverflow
Solution 5 - GitgreencheeseView Answer on Stackoverflow
Solution 6 - GitCameron SkinnerView Answer on Stackoverflow
Solution 7 - GitRajeev JayaswalView Answer on Stackoverflow
Solution 8 - GitEvZView Answer on Stackoverflow
Solution 9 - GitAlej privView Answer on Stackoverflow
Solution 10 - GitAmin ShojaeiView Answer on Stackoverflow