The short answer
#Run git reflog, find the commit you lost, and create a branch that points at it. As long as the work was committed in your local repository, it is almost always still there.
_10git reflog_10# find the hash, for example a81b4de_10git branch feature/login a81b4de
Why it happens
#Deleting a branch, resetting or rebasing does not delete commits straight away. It only moves or removes the labels that point at them. A commit with no branch or tag pointing at it is called unreachable: it no longer shows in git log, but it stays in the repository until garbage collection removes it.
The reflog (reference log) is Git's private diary of where HEAD and each branch have pointed. Every commit, checkout, reset, merge, rebase and amend adds an entry. So even when no branch points at a commit any more, the reflog usually remembers it.
A few things to know:
- The reflog is local. It is never pushed, and a fresh clone starts with an empty one.
- Entries expire. By default Git keeps reachable entries for 90 days and unreachable ones for 30 days (
gc.reflogExpireandgc.reflogExpireUnreachable), after which garbage collection can remove the commits. - It only knows about commits. Uncommitted changes in your working folder are not recorded.
How to fix it
#Recover a deleted branch
When you delete a branch, Git prints the commit it pointed at. Check your terminal first:
_10git branch -D feature/login_10# Deleted branch feature/login (was a81b4de)._10_10git branch feature/login a81b4de
If that output is gone, look in the HEAD reflog for the last time you were on the branch. A branch's own reflog is deleted with the branch, but the HEAD reflog keeps the history of what you checked out and committed.
_10git reflog_10# 3f2a1c9 (HEAD -> main) HEAD@{0}: checkout: moving from feature/login to main_10# a81b4de HEAD@{1}: commit: Add login form validation_10# 7c0e5b2 HEAD@{2}: commit: Add login form_10_10git switch -c feature/login a81b4de
The line just before "moving from feature/login" is the last commit you made on that branch.
Undo a bad reset
If git reset --hard moved your branch too far back, the reflog shows where it was before.
_10git reflog_10# 7c0e5b2 HEAD@{0}: reset: moving to HEAD~2_10# a81b4de HEAD@{1}: commit: Add login form validation_10_10git reset --hard a81b4de
If you have uncommitted changes you care about, commit or stash them before running another hard reset.
Undo a rebase or merge that went wrong
Rebase, merge and reset save the previous position in ORIG_HEAD, which is the quickest way back straight after the operation:
_10git reset --hard ORIG_HEAD
If you have done other things since, find the entry from before the rebase started in git reflog show feature/login instead, and reset to that hash.
Look before you restore
Not sure which entry is the right one? Inspect it without changing anything.
_10git show a81b4de --stat_10git log --oneline a81b4de -n 5
Creating a new branch at a hash is always safe, so when in doubt do that and compare.
When the reflog does not have it
For commits that were never on HEAD, or a dropped stash, ask Git to list objects that nothing points at:
_10git fsck --lost-found_10# dangling commit 9d1e4f0...
Files you staged with git add but never committed may appear as dangling blobs, which you can read with git show and the blob hash.
Common pitfalls
#- Running
git gc --prune=nowor similar clean-up commands while trying to recover. They can delete the very commits you are looking for. - Expecting to recover work from a remote branch someone else deleted. Your reflog only covers your own repository; ask the person who pushed it, or check whether your Git host offers a restore option.
- Waiting too long. After the expiry period, garbage collection may remove unreachable commits for good.
- Recovering with
git checkoutand a hash, then committing. That puts you in detached HEAD state; create a branch withgit switch -cso the work has a name.
Related reading
#Gitquick question
Was this resource helpful?