602

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

How do I recover the branch?

2
  • 38
    Glad to know I'm not the only lame-o who did this (and forgot to regularly push to remote a copy)
    – Ray
    Commented Aug 24, 2017 at 11:48
  • 2
    This is, hands down, the most important SO question I have ever looked for
    – reformy
    Commented Mar 20 at 11:47

12 Answers 12

1071

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
6
  • 113
    Git tells you what the SHA1 was when the branch is deleted, so if you just delete it it might just be a few lines up in the command line.
    – seagullJS
    Commented Dec 13, 2012 at 1:04
  • 4
    if this branch exists on the server, alternatively, you can use git branch branchName origin/branchName Commented Sep 22, 2017 at 13:13
  • 1
    in addition i ended up using git cherry-pick <sha1> to move the commit to branch Commented May 7, 2018 at 20:28
  • 1
    For those who delete on IntelliJ GUI, go to the Git tool (+9)> Console tab and search for branch -D
    – Ricardo
    Commented Jul 14, 2023 at 23:13
  • In GitKraken I went to Activity Logs and copied the ${sha} from a line that looked like Delete ref refs/heads/my-branch at ${sha}
    – hughjdavey
    Commented Feb 5 at 16:48
118

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--.

2
  • Thank you so much, I deleted my branch accidentally and it existed locally only (which never happens). You saved me 2 days of my work, thank you again
    – Zain Zafar
    Commented Mar 18 at 22:02
  • I deleted both local and remote which caused the related merge request to be get closed as well. And yet with this trick I managed to return my branch. Commented Jul 24 at 5:29
84

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 
66

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
2
  • 7
    Thank you very much. This worked for me. My 2 months of work was lost. This solution helped recover those files.
    – Amit
    Commented Jan 20, 2021 at 13:00
  • 1
    hahahhaha. it was my 10 mins work and I thought I am in trouble
    – om471987
    Commented May 10, 2023 at 5:29
47

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

$ git checkout deletedBranchName
1
  • This answer makes Git Extensions shut up about "the branch you are trying to push seems to be a new branch for this remote." Thanks a lot. Commented Dec 21, 2014 at 8:56
10

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.

2
  • yeah, that is why we use git, to avoid having to carry all that around. Every action you have committed is still available to you.
    – mateor
    Commented May 3, 2013 at 5:26
  • 1
    This works great! I was also able to visualize lost commits in a tree: git fsck --lost-found | awk '/commit/{print $3}' | xargs gitk &
    – Melebius
    Commented Oct 26, 2021 at 12:07
9

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>

8

If you are using IntelliJ IDEA, in Event Log you'll see something like that:

enter image description here

And may simply restore your branch.

6

First of all, don't get panic. You are in the right place. Go on champ, we all make mistakes! That's how we learn! I wish you health, happiness and success!

Oh for the answer! I think you already figured out!

If not! here is the answer.

use git reflog
git checkout branch branch_name commitsha

For more clarification, in the second command branch_name is the name you want to give your branch. commitsha is sha number you want to check out. which you will get from git reflog command.

Once again Happy coding!

5

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

1
  • 2
    This is not a forum, please upvote useful answers instead of reposting them.
    – Melebius
    Commented Oct 26, 2021 at 12:09
4

This worked for me:

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.