Skip to main content

Sign in

Save your progress and access it from any device.

Or with email

Don't have an account?

Privacy policy

Git reflog: recover lost commits

Lost a commit after a reset? Deleted a branch by mistake? The reflog is Git's safety net. It records everything, even what git log no longer shows.

Key concept

What is git reflog?

git reflog (reference log) records every movement of HEAD in your local repository: commits, checkouts, resets, merges, rebases...

Unlike git log which only shows the reachable commit chain, the reflog sees the complete history, including commits "lost" after a reset or rebase.

It's your safety net: as long as you committed something, the reflog can help you find it.

Git reflog syntax

git reflog

Show the complete history of HEAD

git reflog show <branch>

Reflog for a specific branch

git reset --hard HEAD@{n}

Go back to a previous reflog state

The reflog in action

View the reflog and recover lost commits after an accidental reset.

View the reflog

Recover a lost commit

3 situations where the reflog saves you

Accidental hard reset

You ran git reset --hard and lost commits. The reflog recorded them, you can go back.

git reset --hard HEAD@{1}

Deleted branch

You deleted a branch with git branch -D. Find its last commit in the reflog and recreate it.

git checkout -b my-branch <hash>

Rebase gone wrong

A rebase caused problems? The reflog lets you find the state before the rebase and go back to it.

git reset --hard HEAD@{n}

Best practices with the reflog

1

Commit often

The reflog can only save what has been committed. Uncommitted changes are not recorded.

2

Check the reflog before a reset

Before running reset --hard, note the current hash in case you want to go back.

3

The reflog is local

The reflog is not shared. If you lose your local repository (hard drive failure), the reflog is lost too. Push regularly to the remote.

4

Entries expire

By default, reflog entries expire after 90 days. Don't wait too long to recover a lost commit.

Part of the Undo Changes in Git guide

A

GitQuest is created by Anaïs (nouvelle fenêtre), web developer and head of education, specializing in tech training and digital accessibility.

Questions about git reflog

Practice the reflog without risk

GitQuest puts you in situations where you need to recover lost commits. Practice in a safe environment.

Start practicing now