Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recovering Lost Commits with Reflog
📖 Scenario: You accidentally deleted some commits in your Git repository. You want to recover those lost commits using Git's reflog feature.
🎯 Goal: Learn how to use git reflog to find lost commits and restore your branch to a previous state.
📋 What You'll Learn
Use git reflog to view recent HEAD changes
Identify the commit hash of the lost commit
Use git reset --hard <commit-hash> to restore the branch
Verify the branch points to the recovered commit
💡 Why This Matters
🌍 Real World
Developers often accidentally lose commits by resetting branches or deleting branches. Using reflog helps recover those commits quickly without losing work.
💼 Career
Knowing how to recover lost commits is a valuable skill for software developers and DevOps engineers to maintain code integrity and avoid data loss.
Progress0 / 4 steps
1
Initialize a Git repository and create commits
Run git init to create a new Git repository. Then create a file named file.txt with the content First line. Add and commit this file with the message Initial commit. Next, append Second line to file.txt, add and commit with the message Second commit.
Git
Hint
Use git init to start a repo. Use echo to write to files. Use git add and git commit to save changes.
2
Simulate losing commits by resetting branch
Use git log --oneline to find the commit hash of the Initial commit. Then run git reset --hard <initial-commit-hash> to move the branch back to the first commit, losing the second commit from the branch history.
Git
Hint
Use git log --oneline to get short commit hashes. Use shell commands to extract the hash. Then reset hard to that hash.
3
Use reflog to find lost commit hash
Run git reflog to see recent HEAD changes. Identify the commit hash of the lost Second commit from the reflog output. Assign this hash to a variable named lost_commit using shell commands.
Git
Hint
Use git reflog and grep to find the commit message. Extract the commit hash with cut or awk.
4
Restore the lost commit using reset
Use git reset --hard $lost_commit to restore the branch to the lost commit. Then run git log --oneline -1 to print the latest commit message and verify it is Second commit.
Git
Hint
Use git reset --hard $lost_commit to restore. Use git log --oneline -1 --pretty=%s to print the commit message only.
Practice
(1/5)
1. What is the primary purpose of git reflog in Git?
easy
A. To show a log of where HEAD and branch references have been recently
B. To permanently delete commits from the repository
C. To merge two branches automatically
D. To push commits to a remote repository
Solution
Step 1: Understand what reflog tracks
Git reflog records changes to HEAD and branch tips, showing recent commit movements.
Step 2: Identify reflog's main use
It helps find lost commits by listing recent HEAD positions, not deleting or merging.
Final Answer:
To show a log of where HEAD and branch references have been recently -> Option A
Quick Check:
Reflog = recent HEAD changes [OK]
Hint: Reflog shows recent HEAD moves, not branch merges or deletions [OK]
Common Mistakes:
Confusing reflog with git log
Thinking reflog deletes commits
Assuming reflog pushes commits
2. Which command correctly recovers a lost commit using its reflog hash abc1234 by creating a new branch named recovered?
easy
A. git branch recovered && git checkout abc1234
B. git reset --hard recovered abc1234
C. git reflog checkout abc1234 recovered
D. git checkout -b recovered abc1234
Solution
Step 1: Understand how to create a branch from a commit hash
The command git checkout -b <branch> <commit> creates and switches to a new branch at that commit.
What command will restore the commit with message 'Add feature'?
medium
A. git checkout HEAD@{2}
B. git checkout 789abcd
C. git checkout def5678
D. git checkout abc1234
Solution
Step 1: Identify the commit hash for 'Add feature'
From reflog, 'Add feature' commit hash is def5678 at HEAD@{1}.
Step 2: Use git checkout with the correct hash
Checking out def5678 restores that commit state.
Final Answer:
git checkout def5678 -> Option C
Quick Check:
Checkout commit by hash = def5678 [OK]
Hint: Match commit message to hash, then checkout that hash [OK]
Common Mistakes:
Choosing wrong commit hash
Using HEAD@{2} which is 'Initial commit'
Confusing latest commit with target commit
4. You ran git reflog and found a lost commit hash abc1234. You tried git checkout abc1234 but got a detached HEAD warning. How do you fix this to recover the commit safely?
medium
A. Create a new branch at that commit using git checkout -b recovered abc1234
B. Run git reset --hard abc1234 immediately
C. Delete the reflog entry and try again
D. Push the commit hash to remote
Solution
Step 1: Understand detached HEAD state
Checking out a commit hash puts you in detached HEAD, which is risky for new work.
Step 2: Create a branch to save the commit safely
Use git checkout -b recovered abc1234 to create a branch and avoid losing commits.
Final Answer:
Create a new branch at that commit using git checkout -b recovered abc1234 -> Option A
Quick Check:
Fix detached HEAD by creating branch [OK]
Hint: Always create a branch from lost commit to avoid detached HEAD [OK]
Common Mistakes:
Ignoring detached HEAD and continuing work
Deleting reflog entries mistakenly
Trying to push without branch
5. You accidentally reset your branch to an older commit, losing recent commits. You run git reflog and see:
abc1234 HEAD@{0}: reset: moving to abc1234 def5678 HEAD@{1}: commit: Add new feature 789abcd HEAD@{2}: commit: Fix bug
How do you restore your branch to include the lost 'Add new feature' commit?
hard
A. Delete reflog entries before abc1234
B. Run git reset --hard def5678 to move branch back to lost commit
C. Run git checkout abc1234 to stay at reset point
D. Run git merge def5678 from abc1234
Solution
Step 1: Identify lost commit hash from reflog
The lost commit 'Add new feature' is at def5678 (HEAD@{1}).
Step 2: Use git reset to move branch pointer back
Running git reset --hard def5678 restores branch to that commit, recovering lost work.
Final Answer:
Run git reset --hard def5678 to move branch back to lost commit -> Option B
Quick Check:
Reset branch to lost commit hash [OK]
Hint: Use git reset --hard with reflog hash to recover lost commits [OK]
Common Mistakes:
Checking out reset commit instead of resetting branch