0
0
Gitdevops~10 mins

Reflog for finding lost commits in Git - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Reflog for finding lost commits
Commit made
HEAD moves to new commit
Commit lost by reset/rebase
Use git reflog
Find lost commit SHA
Recover commit with git checkout or git reset
This flow shows how commits move HEAD, how commits can be lost by reset or rebase, and how reflog helps find and recover them.
Execution Sample
Git
git commit -m "Add feature"
git reset --hard HEAD~1
git reflog
# find lost commit SHA
git checkout <SHA>
This sequence creates a commit, resets HEAD to previous commit losing the new one, then uses reflog to find and recover the lost commit.
Process Table
StepCommandHEAD PositionActionResult
1git commit -m "Add feature"commit1Create new commitHEAD points to commit1
2git reset --hard HEAD~1commit0Move HEAD back one commitcommit1 becomes unreachable (lost)
3git reflogcommit0Show HEAD historyLists commit1 as recent HEAD position
4git checkout <commit1 SHA>commit1Checkout lost commitHEAD points to recovered commit1
💡 Lost commit found in reflog and recovered by checkout
Status Tracker
VariableStartAfter Step 1After Step 2After Step 4
HEADcommit0commit1commit0commit1
commit1does not existexists and pointed by HEADunreachable (lost)checked out and reachable
Key Moments - 3 Insights
Why does the lost commit still appear in reflog after reset?
Because reflog records all HEAD movements including resets, so even unreachable commits are listed (see execution_table step 3).
Can I recover a lost commit if it is not in reflog?
No, reflog only tracks recent HEAD changes. If the commit is too old and reflog expired, recovery is not possible.
What does git reset --hard do to the commit?
It moves HEAD and updates working files, making the newer commit unreachable but still in reflog temporarily (see step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is HEAD pointing to after step 2?
Acommit0
Bcommit1
Ccommit2
DNo commit
💡 Hint
Check the 'HEAD Position' column for step 2 in the execution_table.
At which step does the lost commit become reachable again?
AStep 1
BStep 4
CStep 2
DStep 3
💡 Hint
Look for when HEAD points back to commit1 in the execution_table.
If you did not run git reflog, how would the recovery process change?
AThe commit would be deleted immediately
BYou could still recover the commit easily
CYou would not know the lost commit SHA to recover
DHEAD would automatically point to the lost commit
💡 Hint
Refer to the importance of reflog in finding lost commit SHA in the concept_flow.
Concept Snapshot
git reflog records all HEAD movements including resets.
Lost commits become unreachable but are listed in reflog.
Use 'git reflog' to find lost commit SHA.
Recover lost commits with 'git checkout <SHA>' or 'git reset <SHA>'.
Reflog entries expire after some time, so recover quickly.
Full Transcript
This lesson shows how git commits move HEAD pointer and how commands like reset can make commits unreachable or lost. The git reflog command records all HEAD movements, including resets and rebases, so it helps find lost commits by showing their SHA hashes. By running git reflog, you can see the history of HEAD positions and find the SHA of a lost commit. Then you can recover it by checking it out or resetting HEAD to it. The execution table traces these steps: making a commit, resetting HEAD back, listing reflog, and recovering the commit. Variables like HEAD and commit1 change state accordingly. Key moments clarify why reflog still shows lost commits and the limits of recovery. The quiz tests understanding of HEAD positions and recovery steps. Remember, reflog is a safety net for recent lost commits but entries expire over time.