Recovering Lost Work With the Reflog: Commits, Branches, Rebases and Stashes
Git rarely loses committed work. Read the reflog, then recover from a hard reset, a deleted branch, a bad rebase or amend, a dropped stash and even staged files that were never committed, using reflog, ORIG_HEAD and git fsck. Also what expires, and when.
kvi5hnuCode Sprout team member8 min readOn this page
Almost every "I lost my work" moment in Git is recoverable, as long as the work was committed, or at least staged. Commands like reset --hard, rebase, commit --amend and branch -D don't delete commits: they move branch names away from them. The commits stay in the repository for weeks, and the reflog records where they were. This article shows how to find them again.
The reflog
Every time HEAD or a branch moves, through a commit, checkout, reset, merge, rebase, pull or amend, Git appends an entry to that reference's reflog (reference log):
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
git switch -q -c maps; git commit -q --allow-empty -m "Start maps"
git switch -q main
git commit -q --amend -m "Add ridge page (with photos)"
git reset -q --hard HEAD~1
git reflog
echo "=== the reflog of one branch"
git reflog show maps
cf8c779 HEAD@{0}: reset: moving to HEAD~1
5c9e05e HEAD@{1}: commit (amend): Add ridge page (with photos)
ad382c1 HEAD@{2}: checkout: moving from maps to main
ce15043 HEAD@{3}: commit: Start maps
ad382c1 HEAD@{4}: checkout: moving from main to maps
ad382c1 HEAD@{5}: commit: Add ridge page
cf8c779 HEAD@{6}: commit (initial): Add lake page
=== the reflog of one branch
ce15043 maps@{0}: commit: Start maps
ad382c1 maps@{1}: branch: Created from HEAD
- Each line shows the commit the reference pointed to, a name like
HEAD@{2}, meaning "where HEAD was two moves ago", and what moved it. git reflogis short forgit reflog show HEAD. Every branch has its own reflog too:git reflog show main.HEAD@{n}andmain@{n}work anywhere a commit is expected:git show HEAD@{3},git diff main@{1} main. Time-based forms work too:main@{yesterday},HEAD@{2.hours.ago}(article 29).git log -gshows the same entries in full log format, andgit reflog --date=isoshows when each move happened.
The reflog is local: it isn't pushed or cloned, and each clone has its own. It records what happened in your repository.
Recovering after reset --hard
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Ridge" > ridge.txt; git add .; git commit -q -m "Add ridge page"
echo "Forest" > forest.txt; git add .; git commit -q -m "Add forest page"
git reset -q --hard HEAD~2 # oops: two commits gone from main
git log --oneline
echo "=== find them"
git reflog -3
echo "=== put main back"
git reset -q --hard HEAD@{1}
git log --oneline
cf8c779 Add lake page
=== find them
cf8c779 HEAD@{0}: reset: moving to HEAD~2
b5eedea HEAD@{1}: commit: Add forest page
ad382c1 HEAD@{2}: commit: Add ridge page
=== put main back
b5eedea Add forest page
ad382c1 Add ridge page
cf8c779 Add lake page
HEAD@{1} is where HEAD was before the last move, the reset. If you're not sure which entry you want, look first with git log --oneline HEAD@{2} or git show. Alternatives to resetting:
git branch rescued HEAD@{1}puts a new branch on the old commit without changing the current one.git cherry-pick <id>copies a single lost commit onto the current branch.- Right after the reset,
git reset --hard ORIG_HEADdoes the same as above.
What reset --hard can't bring back is uncommitted changes it overwrote, because they were never stored. Staged ones are an exception, as shown at the end of this article.
Recovering a deleted branch
git init -q trail-guide && cd trail-guide
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
git switch -q -c experiment
echo "Idea" > idea.txt; git add .; git commit -q -m "Try an idea"
git switch -q main
git branch -D experiment # the message shows the commit ID
echo "=== no ID to hand? search HEAD's reflog for the branch's last commit"
git reflog -2 # leaving experiment, and just before
git branch experiment HEAD@{1}
git log --oneline experiment -1
Deleted branch experiment (was 20cef30).
=== no ID to hand? search HEAD's reflog for the branch's last commit
cf8c779 HEAD@{0}: checkout: moving from experiment to main
20cef30 HEAD@{1}: commit: Try an idea
20cef30 Try an idea
git branch -Dprints(was <id>). If that's still on screen,git branch <name> <id>restores the branch.- Deleting a branch deletes its own reflog too, but
HEAD's reflog still has the moments you were on it, such as "checkout: moving from experiment to main". The commit before that entry is the branch's last commit, as long as you made it there. - For a branch you never checked out, such as one fetched from the remote, the remote usually still has it:
git fetch, thengit switch <name>.
Undoing a rebase or amend
A rebase rewrites a branch's commits, and an amend replaces the last one. The old versions are in the branch's reflog:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
git switch -q -c lake
echo "Lake" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake map" > map.txt; git add .; git commit -q -m "Add lake map"
git switch -q main; echo "News" > news.txt; git add .; git commit -q -m "Add news"
git switch -q lake
git rebase -q main
echo "=== lake's reflog"
git reflog show lake
git reset -q --hard lake@{1} # the branch before the rebase
echo "=== back to the original commits"
git log --oneline --graph --all
=== lake's reflog
2fd3cfe lake@{0}: rebase (finish): refs/heads/lake onto 18ebb35f66e72b036fbf996eebc707f58e82f2b3
8197d43 lake@{1}: commit: Add lake map
340af72 lake@{2}: commit: Add lake page
7fb62a0 lake@{3}: branch: Created from HEAD
=== back to the original commits
* 8197d43 Add lake map
* 340af72 Add lake page
| * 18ebb35 Add news
|/
* 7fb62a0 Start project
- The branch's reflog has one entry per operation, so
lake@{1}is the whole branch before the rebase.HEAD's reflog has an entry for every commit the rebase replayed, so the rightHEAD@{n}is further back: look for the entry just beforerebase (start). - Right after a rebase,
ORIG_HEADpoints to the branch's old tip too. - An amended commit is recovered the same way:
git reset --soft HEAD@{1}returns to the commit before the amend, keeping the amended changes staged.
Recovering a dropped stash
Stashes are commits, stored in the reflog of a special reference, refs/stash. git stash drop and git stash clear remove entries from it, but the commits remain until garbage collection:
git init -q trail-guide && cd trail-guide
echo "Lake loop: 5 km" > lake.txt; git add .; git commit -q -m "Add lake page"
echo "Lake loop: 5.4 km, new bridge" > lake.txt
git stash -q
git stash drop -q
git stash list | wc -l
echo "=== commits nothing points to; stashes have messages like 'WIP on ...'"
for c in $(git fsck --no-reflogs 2>/dev/null | awk '/dangling commit/ {print $3}'); do
git log -1 --format="%h %s" "$c"
done
git stash apply -q "$(git fsck --no-reflogs 2>/dev/null | awk '/dangling commit/ {print $3}' | head -1)"
cat lake.txt
0
=== commits nothing points to; stashes have messages like 'WIP on ...'
4b18d1b WIP on main: 65a5265 Add lake page
Lake loop: 5.4 km, new bridge
git stash dropprintsDropped refs/stash@{0} (<id>). With that ID,git stash apply <id>is all you need.- Without it,
git fsck --no-reflogslists dangling commits: commits that no branch or tag reaches, ignoring the reflogs, so commits lost by a reset, rebase or amend appear too. Pick out stashes by their subjects,WIP on main: ...orOn main: <message>. git log --oneline --no-walk $(git fsck --no-reflogs | awk '/dangling commit/ {print $3}')lists them all at once, so you can pick by message.
Staged but never committed
git add stores a file's content in the repository straight away, as a blob. If that content is later lost, for example by reset --hard, the blob remains, though with no file name:
git init -q trail-guide && cd trail-guide
echo "# Trail guide" > README.md; git add .; git commit -q -m "Start project"
printf 'Ridge loop: 8 km\nTrailhead: north car park\n' > ridge.txt
git add ridge.txt # staged...
git reset -q --hard # ...then wiped before committing
ls
git fsck --lost-found 2>/dev/null
echo "=== the blob's content"
git show "$(git fsck --lost-found 2>/dev/null | awk '/dangling blob/ {print $3}')"
README.md
dangling blob f1600541714629c716b2e864642aef5a41e5aadd
=== the blob's content
Ridge loop: 8 km
Trailhead: north car park
git fsck --lost-found also writes each dangling blob to .git/lost-found/other/, named by its ID. With many blobs, search them with grep -l "Trailhead" .git/lost-found/other/*. Edits that were never staged were never in the repository, so Git can't help: try your editor's local history, or an IDE's.
How long things last
Unreachable commits aren't kept forever. Garbage collection, run automatically by git gc --auto and git maintenance, removes them, after their reflog entries expire:
| Setting | Default | Meaning |
|---|---|---|
gc.reflogExpire | 90 days | reflog entries for commits still on a branch |
gc.reflogExpireUnreachable | 30 days | reflog entries for commits no longer on any branch, such as those removed by reset |
gc.pruneExpire | 2 weeks | unreachable objects with no reflog entry, such as dropped stashes and lost blobs |
So a commit lost by reset --hard is safe for about 30 days, and a dropped stash or staged blob for at least about two weeks. Don't run git gc --prune=now or git reflog expire --expire=now --all while you're trying to recover something.
Other reflog subcommands: git reflog list shows which references have reflogs, git reflog delete removes specific entries, and git reflog drop removes a reference's whole reflog.
What's current
- Git 2.50 and later include
git reflog drop, andgit reflog list(2.45) shows which references have reflogs. git maintenancecan expire reflogs on a schedule, using the same expiry settings.- Hosting services keep their own logs: GitHub shows force pushes in a pull request's timeline and in the repository's activity view, which can help recover a branch that was overwritten on the server.
Common questions
I did git reset --hard and my commits are gone. Are they lost?
No. Run git reflog, find the entry before the reset, and git reset --hard HEAD@{n}, or git branch rescued <id> to keep both. Only uncommitted, unstaged edits are lost.
Can I recover a branch someone else deleted on the server? If anyone still has it, in their local branch or as a remote-tracking branch they haven't pruned, they can push it again. Your own reflog only knows about branches you had locally.
Does the reflog get pushed or cloned? No. It's per repository and never transferred. A fresh clone starts with an empty reflog.
Why doesn't git log show my lost commit?
git log follows parents from branches and HEAD. A commit that no branch leads to doesn't appear. git log -g (the reflog) or git log --all --reflog does show it.
Summary
- The reflog records every move of
HEADand each branch.HEAD@{n}andbranch@{n}name earlier positions. - Recover with
git reset --hard <entry>,git branch <name> <entry>orgit cherry-pick <id>.ORIG_HEADcovers the last reset, merge or rebase. - Deleted branches: use the ID from
-D's message, or find the branch's last commit inHEAD's reflog. - Rebases and amends: the branch's reflog,
branch@{1}, holds the version before. - Dropped stashes and lost staged files:
git fsck --no-reflogsandgit fsck --lost-foundfind dangling commits and blobs. - Lost commits last about 30 days by default, and dangling objects about two weeks.
Practice questions
You ran git rebase -i and messed up, then made two more commits on top. How do you get back to the branch before the rebase?
Answer
Look at the branch's reflog: git reflog show <branch>. The branch's reflog has one rebase (finish) entry for the rebase. The entry just below it is the branch as it was, say <branch>@{3} after two more commits. Then git reset --hard <branch>@{3}. The two new commits can be cherry-picked back onto it afterwards, if needed: note their IDs first.
You deleted feature/maps with -D an hour ago and the message is gone from your terminal. How do you find its last commit?
Answer
Search HEAD's reflog for when you left the branch: git reflog | grep "moving from feature/maps". The entry's commit is where you went, and the entry just below it is the branch's last commit, if you committed there. Then git branch feature/maps <id>. If you never had it checked out, it may still be on the remote: git fetch and git switch feature/maps.
Which kind of lost work can't Git recover, and why?
Answer
Changes that were never staged or committed. Git stores content only when you git add or commit it, so edits that only ever existed in the working tree aren't in the repository. Once overwritten, for example by git restore or git reset --hard, only your editor's or operating system's own history might have them.
Previous: Undoing Merges: Aborting, Resetting and Reverting a Merge Commit
Next: Stashing Work in Progress: push, pop, apply, Partial Stashes and Branches

More in Git Course
Quiz: Git Delicate Details
Twenty-five questions on Git's subtle corners: what commit -a and stash leave out, empty folders, ignore patterns and negation, HEAD~ versus HEAD^, ours and theirs in a rebase, tags that don't move or don't push, describe, merge reverts, diff exit codes, stash conflicts and more.
Quiz: Git Interview Questions
Thirty Git questions of the kind asked in developer interviews: Git versus GitHub, the staging area, merge versus rebase, fetch versus pull, reset, revert and restore, detached HEAD, conflicts, force pushing, stash, cherry-pick, reflog, bisect, submodules, LFS and how commits are identified.
Quiz: Git Course Final Exam (All Parts)
The final exam for the Git course: thirty questions across all twelve parts, from the three areas and staging through branching, remotes, undoing, history investigation, configuration, tags, larger projects, patches, rewriting, internals and troubleshooting.
Quiz: Professional Git Practice (Git Part 12)
Sixteen questions on migrating to Git from Subversion, TFVC and Mercurial, team best practices for commits, branches and reviews, repository hygiene, and diagnosing and fixing the most common Git errors, with explanations.
Discussion
Sign in to ask a question or share what you learned.