Recover commits lost after an accidental git reset --hard

Backend Live Coding November 13, 2026 bash

The reflog keeps every position of HEAD for about 90 days locally, so almost nothing that was committed is lost.

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# ======================================================================
# Q: You accidentally ran git reset --hard and lost commits. What do you do?
#
# The reflog keeps every position of HEAD for about 90 days locally, so almost
# nothing that was committed is lost. On public history I use revert, not reset.
# ======================================================================

git reflog                       # history of HEAD, including the "lost" commits
# a1b2c3d HEAD@{0}: reset: moving to HEAD~3
# 9f8e7d6 HEAD@{1}: commit: Add VAT rounding per line
git branch rescue 9f8e7d6        # or: git reset --hard HEAD@{1}

# bad commit already pushed to main: I do not rewrite history
git revert <sha>

# commit on the wrong branch
git switch correct-branch && git cherry-pick <sha>
git switch wrong-branch   && git reset --hard HEAD~1