Featured image of post Recovering Deleted Branches and Commits using git reflog Featured image of post Recovering Deleted Branches and Commits using git reflog

Recovering Deleted Branches and Commits using git reflog

Restore panic branch losses and loose commit pointers using the local transaction history tracking of git reflog.

“I accidentally deleted a local branch using git branch -D,” or “I ran git reset --hard HEAD~1 and lost my unpushed commits!” If you have worked with Git for any length of time, you have likely experienced one of these stomach-dropping moments.

While standard git log commands won’t display references to deleted branches or detached commits, you don’t need to panic. Git has a built-in safety net called git reflog.

In this guide, we will explore how to retrieve lost commits and restore deleted branches using the local transaction history tracked by git reflog.


1. What is git reflog?

git reflog (Reference Log) is a log of where your HEAD (the current workspace pointer) has been pointing over time.

While a regular git log maps the permanent public commit structure (the DAG tree), git reflog tracks every private command you execute locally, including:

  • Checkouts (switching branches)
  • Commits & Commit Amends
  • Resets (git reset)
  • Merges & Rebases
  • Pulls & Fetches

Reflog entries are completely local and are not pushed to remotes. By default, Git retains these logs for 30 to 90 days before garbage collection sweeps them away. This means almost any “lost” commit can be recovered if you act before the retention period ends.


2. Recovering an Accidentally Deleted Branch

If you deleted a branch that was not yet merged or pushed to a remote, follow these steps to rescue it.

Step 1: Print the Reflog History

Open your terminal and run:

git reflog

You will see output similar to the following:

7c2a1b3 HEAD@{0}: checkout: moving from feature-auth to main
8f9e0d1 HEAD@{1}: commit: feat: implement login API validation
5a4c3d2 HEAD@{2}: commit: feat: design login interface layout
...

Step 2: Locate the Last Commit of the Deleted Branch

Look down the log entries to identify the moment you were still working on the deleted branch (feature-auth). In this example, the last valid state of that branch was at commit 8f9e0d1 (“feat: implement login API validation”).

Step 3: Recreate the Branch from the Commit Hash

To restore the branch at this exact commit, run:

git branch feature-auth 8f9e0d1

Your deleted branch is now fully restored, complete with all its commits.


3. Recovering from a Dangerous Hard Reset

If you ran a destructive command like git reset --hard and wiped out local commits, you can reverse the clock.

Step 1: Identify the Commit Prior to the Reset

Run git reflog to examine the timeline:

3b2a1c9 HEAD@{0}: reset: moving to HEAD~1
9e8d7c6 HEAD@{1}: commit: feat: complex payment integration

Here, we can see the reset happened at HEAD@{0}, moving away from HEAD@{1} (hash 9e8d7c6), which contained our lost payment integration commits.

Step 2: Spin Up a Rescue Branch

Instead of risking another hard reset on your current branch, the safest approach is to create a new branch from the target hash:

git checkout -b rescue-branch 9e8d7c6

You can now merge this rescue branch back into your development branch or continue working directly from here.


Conclusion

git reflog is your local insurance policy. Remember:

  1. Any movement of the HEAD pointer is tracked locally.
  2. As long as you know the commit hash, you can reconstruct or checkout any historical point.
  3. Act quickly before Git’s garbage collection permanently deletes unreachable objects.

Keep this tool in mind the next time a Git command doesn’t go quite as planned.