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.


_10
git reflog
_10
# find the hash, for example a81b4de
_10
git 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.reflogExpire and gc.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:


_10
git branch -D feature/login
_10
# Deleted branch feature/login (was a81b4de).
_10
_10
git 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.


_10
git 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
_10
git 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.


_10
git reflog
_10
# 7c0e5b2 HEAD@{0}: reset: moving to HEAD~2
_10
# a81b4de HEAD@{1}: commit: Add login form validation
_10
_10
git 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:


_10
git 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.


_10
git show a81b4de --stat
_10
git 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:


_10
git 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=now or 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 checkout and a hash, then committing. That puts you in detached HEAD state; create a branch with git switch -c so the work has a name.
#
file under:
Git

quick question

Was this resource helpful?