Sub topic 17
Recovering lost commits and branches with the reflog
This chapter assumes you can create, merge, and delete branches, and that you know a branch is a pointer to a commit. Deleting a branch or resetting it removes a pointer. It does not immediately remove the commits.
What the reflog records
The reflog is a local log of where HEAD and each branch tip have pointed. Git adds an entry every time they move: on a commit, a switch, a merge, a rebase, or a reset. The reflog lives only in your repository. It is never pushed or cloned.
Unreachable commits stay in the object database until garbage collection removes them. By default, reflog entries expire after 90 days, or 30 days if the commit is no longer reachable from any branch. Within that window you can usually get the work back.
Recover a deleted branch
Suppose you force-deleted a branch that was not merged:
git branch -D abandoned-experimentTo get it back, find its last commit in the reflog:
git reflogThe output lists entries from newest to oldest. Each line has an abbreviated commit hash, a reference such as HEAD@{3}, and a description:
a1b2c3d (HEAD -> main) HEAD@{0}: checkout: moving from abandoned-experiment to main
9f8e7d6 HEAD@{1}: commit: Try new parser
4c5d6e7 HEAD@{2}: commit: Add parser skeletonThe commit you want is the last one made on the deleted branch, here 9f8e7d6. Create a new branch at that commit:
git branch recovered-work 9f8e7d6The branch recovered-work now points to the same commit the old branch did, along with its history.
Undo a bad reset or rebase
The reflog also helps when a branch moved to the wrong place. After git reset --hard HEAD~3 or a rebase that went wrong, the old tip is still listed. Find the entry from just before the mistake, then move the branch back:
git reflog
git reset --hard HEAD@{2}Use --hard only when you have no uncommitted changes you want to keep, because it overwrites your working tree.
Commits made on a detached HEAD
Commits made on a detached HEAD have no branch pointing at them. If you switched away, the reflog still lists them. Find the hash and run git branch <name> <hash> to keep them.
Limits
- The reflog is per repository. A fresh clone has none of your old entries.
- Uncommitted changes are never in the reflog. If you never committed or staged them, they cannot be recovered this way.
- Entries expire, and
git gc --prune=noworgit reflog expirecan remove them early.
Exercise
You ran git branch -D feature/login and then realized it had unmerged work. Which two commands recover it, assuming the last commit hash is found in the output?
Show answer
Run git reflog to find the hash of the last commit on feature/login, then run git branch feature/login <hash> to recreate the branch at that commit.
Exercise
Why can't you recover a file you edited but never staged or committed by using the reflog?
Show answer
The reflog only records movements of HEAD and branch tips, which point to commits. Changes that were never committed are not part of any commit, so no entry refers to them.