AlgoMaster Logo

Recovering Lost Commits

14 min readUpdated December 6, 2025
Listen to this chapter
Unlock Audio

Losing commits can feel like a nightmare for any developer. One moment, your hard work is encapsulated in pristine commits, and the next, it seems to vanish into thin air. Fortunately, Git provides multiple ways to recover those lost commits, often using tools like reflog and other internal mechanisms. Understanding how to leverage these tools can save you hours of frustration and help you regain confidence in your version control system.

Understanding Lost Commits

Before diving into recovery techniques, it's crucial to understand how commits can be lost. A commit is "lost" when it no longer appears in any branch or tag reference. Common scenarios include:

  • Resetting a Branch: Using commands like git reset --hard can discard commits that were part of the branch's history.
  • Deleting a Branch: If you delete a branch that contains unique commits, those commits may become unreachable.
  • Amending Commits: When you amend a commit, the original commit is replaced, making it seem like it has disappeared.

Understanding these scenarios will help you anticipate potential pitfalls and know where to look when recovery becomes necessary.

Using Reflog for Recovery

The reflog is a powerful tool for recovering lost commits. It acts as a safety net by logging all actions that affect your branches and HEAD. Each entry in the reflog points to a commit, allowing you to navigate back to previous states.

Viewing the Reflog

To view your reflog, you can use the following command:

This command will output a list of entries similar to this:

Each entry includes a reference (like HEAD@{0}) and the commit hash, which can be used to recover lost commits.

Recovering a Lost Commit

Suppose you've run git reset --hard HEAD~1 and lost your most recent commit. You can recover it by identifying the appropriate reflog entry:

  1. Run git reflog to find your commit's hash.
  2. Use git checkout to recover it:

This command creates a new detached HEAD state pointing to the lost commit, allowing you to create a new branch from there if desired:

Understanding the Reflog Context

Reflog entries expire based on your Git configuration, typically after 90 days. If you find yourself frequently needing to recover lost commits, consider adjusting the expiration timeframe in your Git configuration:

This command sets the reflog expiration to 30 days, giving you a larger window for recovery.

Recovering from a Deleted Branch

When a branch is deleted, its commits may become unreachable. However, if those commits were pushed to a remote repository, recovery becomes more straightforward.

Checking Remote Branches

Run the following command to see if the lost commits are still available on the remote:

After fetching, you can check the remote branches:

If the branch exists on the remote, you can simply recreate it locally:

Local Recovery after Deletion

If the branch was never pushed remotely, you can still use the reflog. Follow these steps:

  1. Use git reflog to find the commit hash from the deleted branch.
  2. Create a new branch from that commit:

This way, you can continue working from where you left off.

Amending Commits and Recovery

Amending a commit with git commit --amend can inadvertently make the original commit seem lost. However, this commit still exists in the reflog.

Recovery of Amended Commits

If you have amended a commit and need to recover the original, follow these steps:

  1. View the reflog:
  1. Find the HEAD reference before the amend. It will show an entry like:
  1. Checkout the previous commit:
  1. Optionally create a new branch to preserve the original state:

Understanding Commit States

When you amend a commit, the original commit is technically not deleted. Rather, it's a matter of Git’s object model where the new commit points to the same tree and parent as the original. This is why you can recover it through the reflog.

Advanced Recovery Techniques

Sometimes, reflog might not be sufficient. In these cases, exploring the .git directory and using other Git commands can help.

Recovering Using Git fsck

The git fsck command can be used to find dangling commits, which are commits that are not reachable from any references:

This command will list dangling commits. You can then recover them using a similar approach as with the reflog:

  1. Identify the commit hash from the output.
  2. Create a new branch from that hash:

Directly Inspecting the .git Directory

For those comfortable with diving into Git's internals, you can directly inspect the .git/objects directory. Each commit is stored as a file named by its SHA-1 hash.

To investigate:

  1. List objects:
  1. Inspect a specific commit using:

This method is less common but offers a deep understanding of Git's storage mechanics.

Best Practices for Avoiding Lost Commits

While recovery is possible, implementing strategies to avoid losing commits in the first place is wise.

Utilize Branches Effectively

  • Create feature branches for new work instead of committing directly to main or master.
  • Regularly push branches to remote repositories to ensure a backup exists.

Leverage Tags for Important Commits

Using tags can mark significant points in your project, making them easier to find later:

Tags help maintain a history of essential milestones, making recovery straightforward.

Regular Use of Reflog

Get in the habit of checking your reflog periodically, especially before performing destructive operations like reset or rebase. It can be a lifesaver.

By understanding the mechanisms behind lost commits and the tools available for recovery, you can approach Git with confidence. Whether you're using reflog, checking remote branches, or delving into the internals of Git, you now have a toolkit for recovering what once seemed lost. Embrace these practices, and you'll find that losing commits is less of a nightmare and more of a manageable challenge.