Git without fear · Chapter 26 of 28
Lost in detached HEAD
By the end of this chapter you can find commits that no branch points to, give them a branch, and get out of detached HEAD safely. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Detective work
- 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
Yesterday Bunty tried a monsoon menu: he checked out an old commit, made two commits there (a monsoon.txt with bhajji and ginger chai), then went back to main. Meera wants to taste the monsoon menu on Monday, and Bunty cannot find it. He checked out the old commit again to look, and now his prompt shows a hash instead of a branch.
Everything you know so far looks along branches. git log, even with --all, shows only commits a branch (or HEAD) leads to, and no branch leads to Bunty's. Checking out the old commit again shows the commit before his work, not his work. And commits nothing points to are not kept for ever: after about a month, Git's clean-up may delete them for good.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Save the monsoon commits on menu-experiment
Bunty's two monsoon commits are on no branch now. Save them on a new branch.
1. Run git reflog. It lists every place HEAD has been, newest first.
2. Find the line for Bunty's commit "Add ginger chai to the monsoon menu". It is the newest of his two commits.
3. Make a branch called menu-experiment at that commit. Do this before HEAD moves again.
4. Show the history of menu-experiment to check it.
To pass: menu-experiment has exactly these commits, newest first: Add ginger chai to the monsoon menu, Try a monsoon menu, Add bun maska, Add the menu. Its monsoon.txt has the line Ginger chai 45.
- Task 2
Out of detached HEAD, back on an unchanged main
You are in detached HEAD now. Go back to the branch main.
1. Save the monsoon work on menu-experiment first (task 1).
2. Switch to main.
3. Do not make or undo any commits on main.
To pass: you are on the branch main, not in detached HEAD. main has exactly its five commits, newest first: Add samosa, Add poha, Add vada pav, Add bun maska, Add the menu.
- Task 3 · interview question
Interview: find lost commits with the reflog
Interview question: how do you find commits that no branch points to any more?
1. Run git reflog. git log -g also works.
2. Read it from the bottom up. Bunty leaves main, makes two commits, then goes back to main.
To pass: you ran git reflog (or git log -g) in the chapter folder, and it worked. A command with a typing mistake does not count, so type it again.
What you will learn
- Detached HEAD: HEAD pointing at a commit instead of a branch, and why commits made there get left behind.
- The reflog: Git's local diary of every place HEAD has been, which still knows the lost commits.
- A branch as a rescue: once a branch points at the commits, they are safe again.
More in Detective work
Build "Lost in detached HEAD" 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.