Lesson08
intermediateRecover Lost Commits with git reflog
Goal
Use the reflog to find commits that no branch points to any more, and bring them back.
Ouch. While cleaning up, someone ran git reset --hard HEAD~2 on main. The last two commits, the payment form and its validation, vanished from git log and from the graph.
They are not really gone: Git still has them, and the reflog remembers where HEAD has been. Get them back onto main.
Objectives
0/3maincontainsAdd payment formagain. (Current objective)maincontainsValidate card numbersagain. (Not started)- The working tree is clean. (Not started)
Your task nowStep 1 of 3
main contains Add payment form again.
Need help?
Reference
What a hard reset leaves behind
Step 1 of 2
main has four commits. The last two are the payment work.
Step 1 of 2. `main` has four commits. The last two are the payment work.
Where HEAD has been
$ git reflog
1f0e6d2 (HEAD -> main) HEAD@{0}: reset: moving to HEAD~2 a83b7c4 HEAD@{1}: commit: Validate card numbers 52c9e10 HEAD@{2}: commit: Add payment form 1f0e6d2 (HEAD -> main) HEAD@{3}: commit: Add cart
Newest first. Every commit, switch, merge and reset is listed with where HEAD pointed afterwards. HEAD@{1} is where HEAD was one move ago, right before the reset.
Two ways back
Any reflog entry can be used wherever Git expects a commit: HEAD@{1}, or the short id shown on the left.
- Move the branch back: git reset --hard HEAD@{1} puts main (and your files) where they were just before the reset. Right when nothing else has happened since. - Rescue onto a new branch: git branch rescue HEAD@{1} gives the lost commits a name without moving main at all. Safer when main has new commits you want to keep; merge or cherry-pick from rescue afterwards.
Preparing your workspace…
Repository Graph
Click a commit to inspect it
Working Tree
Not a Git repository yet
No unstaged changes
Your working tree matches the staging area.
Staging Area
Files prepared for the next commit
No staged changes
Use git add to prepare files for your next commit.
Repository
Committed history
No commits yet
Use git commit to record staged changes.
