Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: October 23, 2024
In this tutorial, we’ll learn how to resurrect a git branch after it has been deleted.
For the sake of the demonstration, let’s imagine a scenario with a simple branch structure:
Given that we’re on the main branch, the following sequence of commands can lead to this situation:
$ git commit --allow-empty -m "original commit"
$ git checkout -b demonstration-branch
$ git commit --allow-empty -m "first commit on demonstration branch"
$ git commit --allow-empty -m "second commit on demonstration branch"
$ git switch main
$ git commit --allow-empty -m "commit only on main branch"
We used empty commits because their content isn’t relevant. Let’s now delete the branch demonstration-branch:
$ git branch -D demonstration-branch
Deleted branch demonstration-branch (was b7fb867).
In Git, a branch is simply a reference to a commit. Deleting a branch doesn’t remove the underlying commit, so we can restore the branch if we have the commit hash. Notably, the abbreviated commit hash of the last commit on demonstration-branch is displayed in the console during the deletion confirmation (in our case, b7fb867).
Let’s now see alternative ways to find the commit hash of the deleted commit.
The reflog records all reference updates in a Git repository, tracking actions like commits and branch changes, even if they aren’t in the current branch history. Let’s have a look at it:
$ git reflog
0eeb0fd (HEAD -> main) HEAD@{0}: commit: commit only on main branch
b2cc5bf HEAD@{1}: checkout: moving from demonstration-branch to main
b7fb867 HEAD@{2}: commit: second commit on demonstration branch
(...)
We can find the abbreviated hash at the beginning of the third line. It’s sufficient for us, but we could also get the full hash with git reflog –no-abbrev. By default, reflog entries are available for 90 days and then become available for Git’s garbage collector.
The git fsck (file system check) command verifies the integrity and validity of the objects and references in a Git repository. It’s convenient for diagnosing problems and ensuring that data remains intact. In particular, if the commits aren’t in the reflog, we can use the git fsck command to retrieve the commits:
$ git fsck --no-reflogs --unreachable | grep commit | cut -d\ -f3 | xargs -n 1 git log -n 1 --pretty=oneline
Checking object directories: 100% (256/256), done.
b7fb8673a10d204267aae6c3bce19981eb417567 second commit on demonstration branch
5990c977f78c9a5b9d6d72853bad0b699078f207 first commit on demonstration branch
We can find the full hash b7fb8673a10d204267aae6c3bce19981eb417567 of the commit in the output. Let’s detail the command:
To recreate the deleted branch, we’ll use the git branch command followed by the desired branch name and the commit hash we found:
$ git branch demonstration-branch b7fb867
We can now check that our main branch and demonstration-branch contain the expected commit:
$ git log -2
commit 0eeb0fdc1297a15383c35f19dd2168682eb2c6ee
(...)
commit only on main branch
commit b2cc5bfa22eb733da78e2f200ffda8c6b39d4b29
(...)
original commit
$ git switch demonstration-branch
$ git log
commit b7fb8673a10d204267aae6c3bce19981eb417567
(...)
second commit on demonstration branch
commit 5990c977f78c9a5b9d6d72853bad0b699078f207
(...)
first commit on demonstration branch
commit b2cc5bfa22eb733da78e2f200ffda8c6b39d4b29
(...)
original commit
We confirm that we came back to the initial situation. Alternatively, git checkout would also work. The difference is that the pointer would directly move to the restored branch, and any unstaged change would be applied to it.
In this article, we saw how to find the commit hash to resurrect a deleted branch. Git objects may eventually be garbage collected, so recreating the branch might only be possible for a limited time.