Back
GitoryxGitoryx

Git Reflog: Recover Lost Commits and Branches

git reflog is Git's safety net. This tutorial shows how to read it, find commits that disappeared after a reset or rebase, restore deleted branches, and why you should recover before entries expire.

Intermediate7 min readUpdated

On this page

What is git reflog?

git reflog lists every place HEAD and your branches have pointed recently, in the order you moved through them. It is a local, temporary log: use it to find a commit lost to a reset, rebase, or deleted branch, then restore it with git reset or git branch.

The short version

  1. 1List recent moves: git reflog
  2. 2Find the state before the mistake: HEAD@{n} or its SHA
  3. 3Inspect it: git show <sha>
  4. 4Restore it: git reset --hard <sha> or git branch rescue <sha>
Aspectgit loggit reflog
ShowsCommits reachable from a branchEvery move of HEAD
Includes lost commitsNoYes, until expiry
Shared with remoteYesNo, local only

Why it matters

You ran git reset --hard, deleted a branch, or botched a rebase, and commits you cared about vanished from git log. You assume they are gone for good.

Git records every position HEAD has been at in the reflog. Commits that no longer appear in the log usually still exist for weeks, and the reflog is how you find them. It is the safety net behind almost every 'I lost my work' moment.

Where it usually goes wrong

  • Assuming git reset --hard or a bad rebase permanently destroyed commits
  • Waiting too long: unreachable commits are eventually garbage collected
  • Looking in git log instead of git reflog
  • Expecting a teammate to recover work from their own reflog: it is local to each clone

Git reflog, step by step

  1. Step 1: Read the reflog

    Run git reflog. The newest entry is on top. HEAD@{n} means where HEAD was n moves ago.

    Terminal
    git reflog# 4a1f9c2 HEAD@{0}: reset: moving to HEAD~3# 9d1c3aa HEAD@{1}: commit: add search# 77f0b4d HEAD@{2}: checkout: moving from main to feature

    Tip: Add dates with git reflog --date=relative to spot the moment before things went wrong.

  2. Step 2: Find the commit you lost

    Look for the entry just before the mistake. Inspect it before restoring anything.

    Terminal
    git show HEAD@{1}git log --oneline HEAD@{1}
  3. Step 3: Undo a bad reset

    Move the branch back to the commit from before the reset.

    Terminal
    git reset --hard HEAD@{1}# or by SHAgit reset --hard 9d1c3aa

    Careful: --hard overwrites uncommitted changes in your working directory. Commit or stash them first.

  4. Step 4: Recover a deleted branch

    A deleted branch's commits are still reachable through the reflog. Find the last commit and recreate the branch there.

    Terminal
    git reflog# 9d1c3aa HEAD@{5}: checkout: moving from feature/search to maingit branch feature/search 9d1c3aa
    In Gitoryx: Open the reflog from the repository menu: a readable list of every HEAD move, labelled RESET, COMMIT, CHECKOUT, and so onHow it works
  5. Step 5: Escape a failed rebase or merge

    Find the entry from before the rebase started and go back to it. The entry is labelled with the operation, e.g. rebase (start).

    Terminal
    git reflog# a1b2c3d HEAD@{4}: rebase (start): checkout main# 9d1c3aa HEAD@{5}: commit: add searchgit reset --hard 9d1c3aa

    Tip: For an ongoing rebase, git rebase --abort is simpler. Use the reflog when it already finished.

  6. Step 6: Check a single branch's history

    Every branch has its own reflog, which is useful to see what happened to just that branch.

    Terminal
    git reflog show maingit reflog show feature/search

    Careful: Entries expire (90 days by default, 30 for unreachable ones), and git gc can then delete the commits. Recover early.

Mistakes to avoid with git reflog

Giving up on lost commits
After reset --hard, a rebase, or a branch deletion, commits vanish from git log but remain in the object database.
Fix: Open git reflog, find the SHA from before the mistake, and restore it with git reset or git branch.
Recovering uncommitted work
The reflog only tracks committed states. Changes that were never committed or stashed are not in it.
Fix: Commit or stash often. For deleted staged files, git fsck --lost-found can sometimes help.
Resetting hard over other changes while recovering
Running git reset --hard to recover overwrites any uncommitted work you did since.
Fix: Stash first, or create a branch at the lost SHA instead: git branch rescue <sha>.

Git reflog in Gitoryx

Gitoryx shows the reflog as a browsable history: pick any previous state of HEAD and restore it or create a branch from it, no SHA copying needed.

  • Open the reflog from the repository menu: a readable list of every HEAD move, labelled RESET, COMMIT, CHECKOUT, and so on
  • Click 'Branch here' on any entry to create a branch from it, with no reset needed
  • Use 'Reset here' to put HEAD back where it was after a bad reset or rebase
  • See recovered commits reappear on the graph immediately

Free plan · no account required · macOS, Windows and Linux

Git reflog: frequently asked questions

What is git reflog?
A local log of where HEAD and each branch pointed over time. It records resets, checkouts, commits, merges, and rebases, so you can return to an earlier state.
How long does the reflog keep entries?
By default 90 days for reachable entries and 30 days for unreachable ones. Both can be changed with gc.reflogExpire and gc.reflogExpireUnreachable.
Does git reflog work on a remote?
No. The reflog is local to each clone and is never pushed or fetched.
How do I recover a deleted branch?
Find its last commit in git reflog and run git branch <name> <sha>.
What is the difference between git log and git reflog?
git log shows the commit history reachable from a ref. git reflog shows the movements of HEAD, including commits that are no longer reachable.
All Git tutorials →
Gitoryx

Stop fighting Git. Start shipping.

Download the fast, native Git client that developers actually enjoy using.

v1.1.0 · macOS 12+ (Apple Silicon or Intel) · Windows 10+ · Linux x86_64