Git without fear · Chapter 11 of 28
The reflog to the rescue
By the end of this chapter you can bring back commits that a reset took off a branch, using the reflog: Git's own diary of where HEAD has been. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Undo
- 3 tasks
- Git
- In the all labs plan
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.
Why you need this
Bunty had a messy edit in orders.js to throw away. He found an answer online and ran git reset --hard HEAD~3, decided it had not worked, and ran git reset --hard HEAD~1 to be sure. Now git log --oneline shows:
1654304 (HEAD -> main) Add the menu
His whole morning (the snacks menu, the snack prices, the order form) has gone from main, and so have Meera's prices. Nothing was pushed, so GitHub has no copy either.
Everything you have used so far (git log, git restore, git revert) works on the commits main can reach, and those four are no longer on it. You cannot name them, so you cannot go back to them, and retyping the work from memory gets small things wrong. Kabir: "The commits are still in the repository. They just have no branch pointing at them."
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Bring main back to before both resets
Bunty ran git reset --hard two times, and main lost four commits. Bring main back to where it was before both resets.
1. Run git log --oneline. The snacks commits are not there.
2. Run git reflog. It lists every place HEAD has been, newest first.
3. Find the last entry before the two resets: the commit "Add snacks to the order form".
4. Move main back to that entry with git reset. Use its name from the reflog, or its hash.
To pass: main has exactly these five commits, newest first, and no others: Add snacks to the order form, Price the snacks, Add the snacks menu, Add the prices, Add the menu. Bring back Bunty's own commits. Do not type them again.
- Task 2
The files back too, and a clean folder
Moving main is not enough. The files in the chapter folder must come back too.
1. Use the reset mode that also changes your files, or bring the files back after the reset.
2. Run git status. It should say the working tree is clean.
3. Stay on main.
To pass: you are on main. snacks.txt (with Samosa 20), prices.csv (with the samosa price) and orders.js (with the snacks) are in the chapter folder. git status shows nothing, except answer.txt.
- Task 3 · interview question
Interview: how long a lost commit is kept
Interview question: is a commit lost to git reset --hard gone forever? Not straight away.
1. Read the last part of the lesson, about how long you have.
2. Find how many days Git keeps reflog entries for commits that no branch can reach.
3. Save that number of days in answer.txt, in the chapter folder.
To pass: answer.txt has the default number of days for commits that no branch can reach.
What you will learn
- git reflog: every place HEAD has been, newest first, with a name for each.
- HEAD@{n}: the name of an old position, usable wherever Git takes a commit.
- Undoing a reset: resetting again, to the position before it, files and all.
- How long the reflog lasts: what it keeps, and when it forgets.
More in Undo
Build The reflog to the rescue yourself
Your first week on Tapri’s team, and every Git situation it throws at you. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Git without fear are free. This one comes with the all labs plan.