Git Reflog: Recovering Lost Commits


In Git, it’s common to lose track of a commit after a reset, rebase, or accidental deletion. However, Git provides a safety net called Reflog (Reference Log), which allows you to recover lost commits by tracking every change made to the tip of your branches.

1. What Is Git Reflog?

Git Reflog records all changes made to the references (such as branches and HEAD) in your repository. Every time you make a commit, checkout a branch, or reset, Git stores this action in the reflog. This means even if a commit is detached from a branch, it’s still traceable through the reflog.

2. How to View the Reflog

To see the reflog, use the following command:

git reflog

This command displays a list of actions, including commits, checkouts, resets, and rebases. Each action is associated with a reference, making it easier to track down commits.

Here’s an example output:

c0a1b2f HEAD@{0}: commit: Fixed bug in login
d1e2f3a HEAD@{1}: checkout: moving from feature-branch to master
e2f3g4b HEAD@{2}: commit: Added new feature

Each entry shows the commit hash, action, and a description of what occurred.

3. Recovering Lost Commits Using Reflog

A. Recovering After a Reset

Let’s say you accidentally reset your branch to a previous commit and lost some changes. You can use reflog to find the commit hash before the reset:

git reflog

Once you find the commit you want to restore, you can reset your branch to that commit:

git reset --hard c0a1b2f
B. Recovering After a Rebase

Rebasing can sometimes lead to losing commits, especially if there’s a conflict or error during the process. You can use the reflog to find the commit before the rebase and restore it:

git reset --hard HEAD@{1}
C. Recovering Detached HEAD Commits

If you make a commit in a detached HEAD state (not attached to any branch), the commit can seem lost after switching branches. The reflog will track the detached commit, allowing you to recover it:

git reflog
git checkout c0a1b2f

Then, create a new branch to preserve the commit:

git checkout -b recovered-branch

4. How Long Does Reflog Keep Data?

By default, Git keeps reflog data for 90 days. This is more than enough time to recover lost commits, but you should still aim to resolve issues as quickly as possible.

5. Cleaning Up the Reflog

Over time, the reflog can become cluttered with old references. To clean up outdated entries, you can run:

git reflog expire --all --expire=30.days.ago

This command removes reflog entries older than 30 days.

6. Best Practices for Using Git Reflog

  • Check Reflog Regularly: If you’ve made drastic changes, such as a reset or rebase, check the reflog to ensure all commits are in the desired state.
  • Don’t Panic if Commits Seem Lost: Reflog is your safety net. Before assuming a commit is lost, review the reflog and recover it if necessary.
  • Use Branches to Preserve Important Work: When working in a detached HEAD state, always create a branch to save any important commits and avoid losing them.

7. Conclusion

Git Reflog is an essential tool for recovering lost commits and tracking your branch’s history. Whether you’ve accidentally reset, rebased, or committed in a detached HEAD state, the reflog provides a reliable way to retrieve lost work. By understanding how to navigate and use the reflog, you can ensure that no commit is truly lost.


Vijeesh TP

Proactive and result oriented professional with proven ability to work as a good team player towards organizational goals and having 20+ years of experience in design and development of complex systems and business solutions for domains such as ecommerce, hospitality BFSI, ITIL and other web based information systems.  Linkedin Profile

Leave a Reply