Black Friday sale: 30% off Pro and Unlimited, until 22 December

Recover lost commits with git reflog

Find a commit after a reset, rebase, or branch deletion. Save it to a new branch and keep your current checkout in place.

If a commit disappears after a reset, rebase, or branch deletion, look in the reflog before resetting again. Git’s local reflog records where HEAD pointed as you committed, switched branches, and moved branch tips.

Find the old commit

Run this from the worktree where you did the work. It shows commit hashes and reflog actions, newest first.

git reflog --format='%h %gs'

After a reset, find the reset entry. Its hash is where you landed. The entry immediately below it shows where HEAD pointed just before the reset. Here’s a reset in the example repository acme/web:

59bae10 reset: moving to HEAD^
909cc30 commit: Fix login retry
59bae10 commit (initial): Add login page

After a completed rebase, find the entry where that run started, such as rebase (start). The entry just below it shows the old tip. Entries marked rebase (pick) can show the same commit subjects as before with different hashes.

After deleting a local branch, look for the entry where you switched away from it. The hash on that entry points to the branch you switched to. The entry just below it shows the tip you left. Git removes the deleted branch’s own reflog, but the worktree’s HEAD reflog can still have that commit.

If you don’t see the entry, check the worktree you used and search the remaining local reflogs:

git reflog show --all --format='%h %gs'

Verify the commit and save it to a branch

Use your own hash in place of 909cc30. Check the commit message and changed files first. If it’s the commit you want, create a new branch at it:

git show --stat 909cc30
git branch recover/login 909cc30
git log -1 --oneline recover/login

The last command confirms the new branch points to the commit you found. If recover/login already exists, pick another name. Creating the branch keeps your current checkout and working files in place.

If the commit is missing

Reflogs are local and their entries can expire. If you shared the commit, check the remote branch or another clone that had it. The reflog records commits and other changes to references. Edits you never committed may still be in an editor’s local history or a backup.

Before changing history again

Create a branch at the tip you want to keep before rewriting it. If you use push& (Pushand), see which Git actions are enabled by default and how to change them.

More Git how-tos