Baeldung Pro – Ops – NPI EA (cat = Baeldung on Ops)
announcement - icon

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.

Partner – Orkes – NPI EA (cat=Kubernetes)
announcement - icon

Modern software architecture is often broken. Slow delivery leads to missed opportunities, innovation is stalled due to architectural complexities, and engineering resources are exceedingly expensive.

Orkes is the leading workflow orchestration platform built to enable teams to transform the way they develop, connect, and deploy applications, microservices, AI agents, and more.

With Orkes Conductor managed through Orkes Cloud, developers can focus on building mission critical applications without worrying about infrastructure maintenance to meet goals and, simply put, taking new products live faster and reducing total cost of ownership.

Try a 14-Day Free Trial of Orkes Conductor today.

1. Overview

In this tutorial, we’ll learn how to resurrect a git branch after it has been deleted.

2. Scenario

For the sake of the demonstration, let’s imagine a scenario with a simple branch structure:

the existing branches and commits in the initial situation

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

3. Recovering the Hash

Let’s now see alternative ways to find the commit hash of the deleted commit.

3.1. Finding the Commit Hash in the Reflog

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.

3.2. Retrieving the Commit Hash Using git fsck

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:

  • We restricted the git fsck search to dangling objects with unreachable, and excluded the objects still in the reflog with no-reflogs, because we used the previous method first.
  • Then, we used grep to keep commits only (and remove tags, for instance).
  • We extracted the third field from each line thank to cut: it’s the commit hash.
  • xargs passes each hash to git log. We use –pretty-oneline to display the hash and commit message to ease the search.

4. Recreating the Branch

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.

5. Conclusion

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.