Sub topic 17
Recovering lost commits and branches with the reflog
This chapter assumes you can create, switch, merge, rebase, and delete branches, and that you know HEAD is a reference to your current position. Here you learn how to get work back after a deleted branch, a bad rebase, or a reset.
Why "lost" commits are usually still there
Git never removes a commit just because no branch points to it. A commit that nothing references is called unreachable. It stays in the object database until garbage collection prunes it. By default, unreachable objects are kept for at least two weeks, and reflog-referenced ones for longer.
So recovery is a search problem: find the commit's SHA (its object ID), then point a branch at it.
What the reflog records
The reflog is a local log of where references used to point. Git appends an entry each time HEAD or a branch tip moves, for example on commit, checkout, switch, merge, rebase, reset, and pull.
git reflogTypical output:
a1b2c3d (HEAD -> main) HEAD@{0}: merge feature/login: Fast-forward
9f8e7d6 HEAD@{1}: checkout: moving from feature/login to main
4c5d6e7 HEAD@{2}: commit: Add login form validation
e1f2a3b HEAD@{3}: commit: Add login formEach line has the SHA, the position HEAD@{n} (0 is the most recent), and a description of the action. You can also read the reflog of a single branch with git reflog show feature/login.
Three properties matter in practice:
- The reflog is local. Cloning does not copy it, and pushing does not share it.
- Entries expire. By default, reachable entries last 90 days and unreachable ones last 30 days.
- It records reference movements, not file changes. Uncommitted work is never in it.
Recovering a deleted branch
Suppose you force-deleted a branch that was not merged:
git branch -D abandoned-experimentGit prints the tip SHA when it deletes a branch, for example Deleted branch abandoned-experiment (was 4c5d6e7). If you still see that line in your terminal, you can skip the search.
Otherwise, find the commit in the reflog. Look for the last commit you made on that branch or the checkout that moved away from it. Then recreate the branch:
git reflog
git branch recovered-work 4c5d6e7Check that the history looks right before you continue:
git log --oneline --graph recovered-workUndoing a bad rebase, merge, or reset
The same technique works when a branch still exists but moved to the wrong place. Rebasing rewrites commits, and git reset --hard moves the branch tip. In both cases the old tip is still in the reflog.
Branch reflogs make this precise. After a rebase of feature/login, the entry just before the rebase began is the original tip:
git reflog show feature/login7a8b9c0 feature/login@{0}: rebase (finish): refs/heads/feature/login onto 9f8e7d6
4c5d6e7 feature/login@{1}: commit: Add login form validationHere feature/login@{1} is the pre-rebase tip. To move the branch back:
git switch feature/login
git reset --hard feature/login@{1}git reset --hard discards uncommitted changes in your working tree, so commit or stash them first.
A related shortcut is ORIG_HEAD. Git sets it before risky operations such as merge, rebase, and reset. Right after a bad merge, git reset --hard ORIG_HEAD returns you to where you started. Later operations overwrite it, so use it immediately or not at all.
Recovering from a detached HEAD
When you commit on a detached HEAD and then switch away, no branch holds those commits. Git warns you, but the warning is easy to miss. The commits appear in the reflog as commit: entries made while HEAD was detached.
git reflog
git branch rescued-commits 3d4e5f6Always create the branch before doing anything else that moves HEAD, so the reflog entry is easy to find.
Searching when the reflog is crowded
On a busy repository the reflog can be long. Narrow it down:
git reflog --date=isoshows timestamps, so you can match entries to when you did the work.git reflog | grep "commit:"shows only commits made locally.git show <sha>andgit log -1 <sha>let you inspect a candidate before you recover it.git log -g --grep="login"searches reflog entries by message.
If a commit does not appear in the reflog at all, for example because the repository was recloned, git fsck --lost-found lists unreachable commits that still exist in the object database. It can be slow on large repositories.
git fsck --lost-foundEach line looks like dangling commit 4c5d6e7.... Inspect with git show and recover with git branch.
Limits and failure modes
| Situation | Recoverable? | Why |
|---|---|---|
| Deleted a branch with `-d` or `-D` | Yes, within the expiry window | The reflog and objects remain |
| Bad rebase or `reset --hard` | Yes | Old tip is in the branch reflog |
| Uncommitted changes lost to `reset --hard` | No, usually | Never committed, so no reflog entry |
| Staged but uncommitted files | Sometimes | `git fsck --lost-found` may find the blob |
| Work in a different clone | No | The reflog is per repository |
| After `git gc --prune=now` or `git reflog expire --expire=now --all` | No | Objects and entries are gone |
Two further cautions apply. Deleting a repository directory removes its reflog with it. Also, a remote branch deleted on the server is not covered by your local reflog, though your remote-tracking branch may still hold the commits until the next git fetch --prune.
Worked example
This sequence reproduces a loss and a recovery in a scratch repository:
git init reflog-demo
cd reflog-demo
git commit --allow-empty -m "Initial commit"
git switch -c feature/demo
git commit --allow-empty -m "Work on demo"
git switch -
git branch -D feature/demo
git reflogThe reflog lists the "Work on demo" commit. Copy its SHA, then run git branch feature/demo <sha>. Running git log --oneline feature/demo now shows the commit again.
Exercise
You ran git rebase main on feature/login and the result is wrong. The branch reflog shows feature/login@{0} as the rebase finish and feature/login@{1} as your last commit before it. Which command restores the branch, and what must you check first?
Show answer
Switch to the branch and run git reset --hard feature/login@{1}. First commit or stash any uncommitted changes, because reset --hard discards them and the reflog cannot bring them back.
Exercise
A teammate says they deleted a branch three months ago and want it back using the reflog on a fresh clone of the repository. Why will this likely fail?
Show answer
The reflog is local and is not copied by cloning, so the fresh clone has no history of the deleted branch. Reflog entries for unreachable commits also expire after 30 days by default. They would need the original clone, the server's copy of the commits, or a known SHA that still exists on the remote.