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
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
- 1List recent moves:
git reflog - 2Find the state before the mistake:
HEAD@{n}or its SHA - 3Inspect it:
git show <sha> - 4Restore it:
git reset --hard <sha>orgit branch rescue <sha>
| Aspect | git log | git reflog |
|---|---|---|
| Shows | Commits reachable from a branch | Every move of HEAD |
| Includes lost commits | No | Yes, until expiry |
| Shared with remote | Yes | No, 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 --hardor a bad rebase permanently destroyed commits - Waiting too long: unreachable commits are eventually garbage collected
- Looking in
git loginstead ofgit reflog - Expecting a teammate to recover work from their own reflog: it is local to each clone
Git reflog, step by step
Step 1: Read the reflog
Run
git reflog. The newest entry is on top.HEAD@{n}means where HEAD was n moves ago.Terminalgit reflog# 4a1f9c2 HEAD@{0}: reset: moving to HEAD~3# 9d1c3aa HEAD@{1}: commit: add search# 77f0b4d HEAD@{2}: checkout: moving from main to featureTip: Add dates with
git reflog --date=relativeto spot the moment before things went wrong.Step 2: Find the commit you lost
Look for the entry just before the mistake. Inspect it before restoring anything.
Terminalgit show HEAD@{1}git log --oneline HEAD@{1}Step 3: Undo a bad reset
Move the branch back to the commit from before the reset.
Terminalgit reset --hard HEAD@{1}# or by SHAgit reset --hard 9d1c3aaCareful:
--hardoverwrites uncommitted changes in your working directory. Commit or stash them first.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.
Terminalgit reflog# 9d1c3aa HEAD@{5}: checkout: moving from feature/search to maingit branch feature/search 9d1c3aaIn Gitoryx: Open the reflog from the repository menu: a readable list of every HEAD move, labelled RESET, COMMIT, CHECKOUT, and so onHow it works 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).Terminalgit reflog# a1b2c3d HEAD@{4}: rebase (start): checkout main# 9d1c3aa HEAD@{5}: commit: add searchgit reset --hard 9d1c3aaTip: For an ongoing rebase,
git rebase --abortis simpler. Use the reflog when it already finished.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.
Terminalgit reflog show maingit reflog show feature/searchCareful: Entries expire (90 days by default, 30 for unreachable ones), and
git gccan 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 fromgit logbut remain in the object database. - Fix: Open
git reflog, find the SHA from before the mistake, and restore it withgit resetorgit 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-foundcan sometimes help. - Resetting hard over other changes while recovering
- Running
git reset --hardto 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.reflogExpireandgc.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 reflogand rungit branch <name> <sha>. - What is the difference between git log and git reflog?
git logshows the commit history reachable from a ref.git reflogshows the movements of HEAD, including commits that are no longer reachable.
Related reading
- How to Undo a Commit in Git (The Right Way)Tutorial · 10 min
- Interactive Rebase Guide: Clean Up Your Git HistoryTutorial · 13 min
- Recover Lost Commits With git reflogBlog · 10 min
- How to Undo Almost Anything in GitBlog · 14 min