Git disasters · Chapter 3 of 14
Commits made on a detached HEAD
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Lost and found
- 3 tasks
- Git
- In the all labs plan
The first 2 chapters of Git disasters are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
See what happened: find Anjali's last commit
Anjali checked out the tag v2.3 and made two commits there. Then she switched to main, and the commits seem lost.
1. Run git reflog. It lists every commit HEAD was on, newest first.
2. Find the line where HEAD moved from a bare commit hash to main.
3. The entry just below it is her last commit, "Rounded corners on the menu card".
4. Write its hash into answer.txt in the case's folder.
To pass: answer.txt has the hash of "Rounded corners on the menu card", and no other hash. A short hash of 7 characters or more is fine.
- Task 2
Recover: her two commits on GitHub's main
Put Anjali's work on main and on GitHub, and keep everything main already has.
1. Make a branch at her last commit, so her two commits are safe.
2. On main, merge that branch. Or cherry-pick her two commits, oldest first.
3. Push main to GitHub. Do not force the push.
To pass: GitHub's main has "Bigger photo on the menu card" and "Rounded corners on the menu card", each exactly once. menu-card.css there has her bigger photo and her rounded corners. The tag v2.3 must stay where it is.
- Task 3
Guard it: never leave commits behind
Make Git save the commits that a checkout would leave on no branch.
1. Write a hook in .git/hooks/post-checkout. Start it with a line like #!/bin/sh.
2. When HEAD leaves a commit that no branch contains, make a branch at that commit. Any name is fine, for example rescue- and the short hash.
3. In every other case, make no branch.
4. Make the hook executable.
To pass: the Check tries it in a copy of your repository. It commits on a detached HEAD and switches to main: a branch must then contain that commit. It also switches between two normal branches: that must make no new branch.
What you will learn
- See what happened: find Anjali's last commit
- Recover: her two commits on GitHub's main
- Guard it: never leave commits behind
More in Lost and found
Build "Commits made on a detached HEAD" yourself
Bunty is Tapri's new junior developer, and Kabir keeps handing you the Git incidents left behind. Each is a disaster interviews love to ask about. 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 disasters are free. This one comes with the all labs plan.