How to restore a deleted branch
Platform Notice: Cloud, Server, and Data Center - This article applies equally to all platforms.
Support for Server* products ended on February 15th 2024. If you are running a Server product, you can visit the Atlassian Server end of support announcement to review your migration options.
*Except Fisheye and Crucible
Problem
You accidentally deleted a branch or you found a missing commit in your local Git repository.
Environment
Any git repository where a branch has been deleted. Or you have a commit that is missing and you found it as a dangling commit. This article makes the following assumptions:
- You are using the working copy of the repository where the branch was deleted. If the deletion happend on another system, the data may not be on your copy or in Bitbucket.
- That you have looked on other clones of the repository in question to see if they have a backup of the branch if the data is not in your local copy. If this is the case, it may be easier to push the branch up to Bitbucket from the other copy of the repository that still has the branch.
- That you have created a backup copy of your local copy of the repository before starting. You can do this by recursively copying the repository directory and its contents to a backup folder.
Diagnosis
This is how to isolate the commit or the branch you need to restore. All branch heads are commit hashes. After finding the hash, we recommend making a new branch. If you know the hash, you can skip to Solution.
Commit
To find the commit, you should look for the dangling commit:
$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (928049/928049), done.
Checking connectivity: 923746, done.
dangling commit <hash>
$
To confirm this is the right commit:
git cat-file -p <hash>
If you find the commit you need, skip to Solution.
Branch
Before we can restore the branch, we need to get the hash of the last commit for that branch, also know as the head. In the examples below, we will always use the hash 773677e, or 773677e7173488c64410af59e0c3287a24c54326, as the one we are looking for. In your case the hash will be different. But once you have the hash you can continue to the solution section.
Just deleted
If you just deleted the branch, you'll see something like this in your terminal:
kb-example-repo$ git branch -d bugfix/CS-1000
Deleted branch bugfix/CS-1000 (was 773677e).
kb-example-repo$
773677e is the head of the feature/one branch. If you do not have this output, you may still be able to find the head of the branch. Git documentation: git-branch
Remote
The branch may still be on the upstream git repository. Here is how to get a list of the branches on the upstream git repository and how to get it's hash.
kb-example-repo$ git branch -r
origin/HEAD -> origin/master
origin/bugfix/CS-1000
origin/feature/one
origin/master
kb-example-repo$ git rev-parse origin/bugfix/CS-1000
773677e7173488c64410af59e0c3287a24c54326
kb-example-repo$
Git documentation: git-rev-parse git-branch
From reflog
The git reflog may have the branch and the last reference. This command assumes you know the name of the branch.
kb-example-repo$ git reflog --no-abbrev | grep bugfix/CS-1000
11bf4fd3d4e58285938e0fce801b2b216a5c0d7f4 HEAD@{0}: checkout: moving from bugfix/CS-1000 to master
773677e7173488c64410af59e0c3287a24c54326 HEAD@{1}: checkout: moving from master to bugfix/CS-1000
kb-example-repo$
In this example, the reference log shows the last two moves for this branch name. The first line is checking out the branch master with the hash 1bf4fd3. The second line is when the repository moved from master to bugfix/CS-1000, so this is the hash we are looking for. Git documentation: git-reflog
From a dangling commit
This is how to look for commits that are not referenced by any branches. Git documentation: git-fsck
kb-example-repo$ git fsck --lost-found
Checking object directories: 100% (256/256), done.
Checking objects: 100% (16/16), done.
dangling commit 773677e7173488c64410af59e0c3287a24c54326
kb-example-repo$
Confirm this is the commit you are looking for
Then you can confirm the commit message and the history using these commands. The git cat-file command will print the commit to validate the commit message. git log will show you the history of that commit. Git documentation: git-cat-file, git-log
kb-example-repo$ git cat-file -p 773677e7173488c64410af59e0c3287a24c54326
tree efe9bf1c6c6428b001bc3548796cec810a4cc981
parent 1bf4fd3d4e58285938e0fce801b2b216a5c0d7f4
author noreply <noreply@atlassian.com> 1668521879 -0600
committer noreply <noreply@atlassian.com> 1668521879 -0600
Fixed output for unknown file format.
kb-example-repo$ git log 773677e7173488c64410af59e0c3287a24c54326
Solution
Once you have the hash, to restore the deleted branch to the latest commit use the checkout command:
git checkout -b <branch-name> <sha>
Example:
kb-example-repo$ git checkout -b bugfix/CS-1000 773677e7173488c64410af59e0c3287a24c54326
Switched to a new branch 'bugfix/CS-1000'
kb-example-repo$
We use the full hash in most of this article. The abbreviated hash should work for all commands. If the abbreviated hash does not work, try to use the full hash.