Can I recover a branch after its deletion in Git?

Viewed 538827

If I run git branch -d XYZ, is there a way to recover the branch? Is there a way to go back as if I didn't run the delete branch command?

23 Answers

If you removed the branch and forgot its commit id, you can do this command:

git log --graph --decorate $(git rev-list -g --all)

After this, you'll be able to see all commits. Then, you can do git checkout to this id and under this commit create a new branch.

Make sure to perform all of this locally, and confirm your repo is in the state you desire before pushing to Bitbucket Cloud. It may also be a good idea to clone your current repo, and test these solutions out first.

  1. If you just deleted the branch, you'll see something like this in your terminal:
    Deleted branch <your-branch> (was <sha>)

2.To restore the branch, use:

    git checkout -b <branch> <sha>

If you don't know the 'sha' off the top of your head, you can:

  1. Find the 'sha' for the commit at the tip of your deleted branch using:
    git reflog
  1. To restore the branch, use:
    git checkout -b <branch> <sha>

If your commits are not in your reflog:

  1. You can try recovering a branch by reseting your branch to the sha of the commit found using a command like:
    git fsck --full --no-reflogs --unreachable --lost-found | grep commit | cut -d\  -f3 | xargs -n 1 git log -n 1 --pretty=oneline > .git/lost-found.txt

2.You can then display each commit using one of these:

    git log -p <commit>
    git cat-file -p <commit>

Thanks for all of you.

My problem was that i deleted my branch on GitLab (remotely) as well as on GIT (locally).

but somehow i got my commits back like so:

  1. first i got the last commit (SHA) with
git log --graph --decorate $(git rev-list -g --all)

  1. i checkout last SHA simply
git checkout <SHA>
  1. then i created a brand new branch (named as you prefer) by typing
git switch -c <branch-name> 

Then i got my commits back i pushed the branch all over again

Thank god.

ps: Somehow (SHA) is a commit code

A very common sub problem of the general problem of recovering deleted branches is recovering a feature branch after merging and then deleting it - as is common practice.

As this SO post covers you can always recover a deleted branch if it has been merged successfully. This is because a branch is just a fancy pointer to a commit and because you've merged, the commit is still there. A merge commit will list the hash of the head commits of the two (or more) branches merged. For example:

        git show master
        commit 849675951d41497e7b07d34096ebf36dc713221 (HEAD -> master)
        Merge: fc1c9ce 97f8a60
        Author: Me
        Date:   Sun Jan 9 16:14:24 2022 +0100

            Merge branch 'feature'

So you can recover the delete 'feature' branch by doing git checkout -b feature 97f8a60 - no need for any reflog stuff.

I did this on the computer which i delete the branch:

git reflog

response:

74b2383 (develope) HEAD@{1}: checkout: moving from master to develope
40ef328 (HEAD -> master, origin/master, origin/HEAD) HEAD@{2}: checkout: moving from develope to master
74b2383 (develope) HEAD@{3}: checkout: moving from master to develope
40ef328 (HEAD -> master, origin/master, origin/HEAD) HEAD@{4}: reset: moving to HEAD
40ef328 (HEAD -> master, origin/master, origin/HEAD) HEAD@{5}: clone: from http://LOCALGITSERVER/myBigProject/Android.git

and i retrieve the branch with this command:

git checkout -b newBranchName 74b2383

If you are using Git Extensions

Although old, this thread is the top of the list when you google recovering deleted branches. I use git extensions rather than the command line and so am unfamiliar with the commands but the reflog command gave me a clue so I'm posting my git extensions solution here for others that are using git extensions who may read this.

  1. Go to the view dropdown on the tool bar
  2. Select Show reflog references

Your deleted branch should now be viewable and selectable, simply click on it and check it out.

Just using git reflog did not return the sha for me. Only the commit id (which is 8 chars long and a sha is way longer)

So I used git reflog --no-abbrev

And then do the same as mentioned above: git checkout -b <branch> <sha>

IF you are using VSCode... and you synced your branch with the server at some point before deleting it...

Note that git branch delete only deletes the local copy, not the copy on the server. First, in the Git panel (git icon on left toolbar), look through the branches and see if your branch is still there under "origin/your_branch_name". If so, just select that and you should get your code back (suggest that you immediately copy/paste/save it locally somewhere else).

If you didn't see an "origin/your_branch_name", Install the GitLens extension. This allows you to visually poke around in the server repositories and locate the copy you synced to the server. If you have multiple repositories, note that it might be necessary to have at least one file opened from the desired repository in order to make the repository appear in GitLens. Then:

  1. Open the GitLens panel

  2. Expand the repository

  3. You should see a list of categories: Branches / Contributors / Remotes / Stashes / etc

You should find YourLostTreasure under "Branches" or possibly under "Remotes -> Origins". Hopefully, you will see a branch with the desired name - if you expand it, you should see the files you changed in that branch. Double-click the file names to open them, and immediately back up that code.

If you don't immediately see your lost branch, poke around and if you find something promising, immediately open it and grab the code. I had to poke around quite a bit until I found TheGoldenBranch, and even then the code was missing the last one or two saves (possibly because I failed to sync to server before attempting-a-Branch-Merge-but-accidentally-clicking-Branch-Delete). My search was unnecessarily lengthened because when I first found the branch I wasn't completely sure the name was correct so kept looking, and it took some time to re-find that first branch. (Thus, Carpe Carpum and then keep looking.)

If you already push branch to remote server then try to use git checkout <branch> and git will try to clone from you're last origin mirror in your local machine.

Related