Recovering Data with Git Commands
This lesson explains why lost work can usually be recovered, and which command to use.
Learning outcomes
After completing this lesson, you will be able to:
- Use the reflog to recover commits.
- Restore a single file from history.
- Compare git revert and git reset.
- Revert a merged change safely.
Why recovery is usually possible
When a commit stops being reachable from a branch, for example after a reset, Git does not delete it immediately. The commit remains in the object store until garbage collection removes it, which by default happens only after a grace period. This window allows recovery. Each clone has its own object store, so a commit lost on one machine may also survive on the remote or on a colleague's machine.
The reflog
The reflog is a local log of everywhere HEAD and each branch have pointed. It is the first tool to use after a mistaken reset, rebase or branch deletion. Entries are normally kept for 90 days, or 30 days for commits that are not reachable. The reflog is purely local: it is not pushed, and a fresh clone has an empty one.
# See recent positions of HEAD
git reflog
# Sample output:
# 3f2a1bc HEAD@{0}: reset: moving to HEAD~3
# 9d8e7f6 HEAD@{1}: commit: Add payment retry
# Recover the lost commit on a new branch
git branch recovered-work 9d8e7f6
# Or move the current branch back
git reset --hard 9d8e7f6If a reflog entry is not available, git fsck --lost-found lists dangling commits, which are not referenced by any branch or tag.
What the reflog can and cannot do
Recovering specific files
Recovering a single file is often better than resetting a branch. git restore brings a file back from any commit, and git log finds the right commit.
# Find the commit that deleted a file
git log --diff-filter=D --oneline -- src/config.json
# Restore it from the commit before that one
git restore --source=4e5d6c7^ -- src/config.json
# Recover uncommitted work that was stashed
git stash list
git stash apply stash@{1}
# Find which commit introduced a bug by bisecting
git bisect start
git bisect bad HEAD
git bisect good v2.2.0The ^ means the parent of a commit, which is needed since the deleted file does not exist in the commit that removed it. git bisect performs a binary search through history, and it can run a test script automatically with git bisect run. git cherry-pick copies a commit from one branch onto another, which is useful when work was committed to the wrong branch.
Recovery commands
Undoing changes on shared branches
The right command depends on whether a change has been shared. The following comparison summarizes the options. The key rule is that git revert is safe on shared branches, because it adds history, whereas git reset rewrites it. Teammates who have already pulled a commit that is then removed by a reset will have diverged histories.
git reset has three modes. --soft keeps changes staged, --mixed, the default, keeps them in the working directory, and --hard discards them. Only --hard destroys uncommitted work, and the reflog cannot recover changes that were never committed.
The following diagram shows main after the revert.

Revert or reset?
Undoing a bad merge
A faulty pull request merged into main with a merge commit two hours ago, and several other changes have merged since. Production is deploying from main. What should the team do?
The team should revert the merge commit with git revert -m 1 <merge-commit>, where -m 1 selects the mainline parent, and merge that through a pull request. This undoes the faulty change and retains later work. git reset --hard followed by a force push would remove the later changes and would disrupt everyone. Azure Repos and GitHub also offer a Revert button on a completed pull request, which creates the same kind of change through a new pull request.
Can the reflog recover a commit on a colleague's fresh clone?
Show answer
No. The reflog is local, and a fresh clone has an empty one. The commit would need to be recovered where it was made.
Exam Tips
Remember for the exam
- git reflog finds commits lost through reset, rebase or branch deletion.
- The reflog is local and does not exist in a fresh clone.
- git restore --source recovers a single file from any commit.
- Use git revert on shared branches; use reset only on private work.
- git reset --hard loses uncommitted changes for good.