Git disasters · Chapter 5 of 14
Commits made on main instead of a branch
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 where main belongs
Bunty made three table booking commits straight on main. He has not pushed them yet.
1. Run git status. It says how many commits main is ahead of GitHub's main.
2. List the commits that are on main but not on origin/main. These are Bunty's three.
3. main should be where GitHub's main is. Find that commit.
4. Write its hash into answer.txt in the case's folder.
To pass: answer.txt has the hash of the commit main should be at, and no other hash. A short hash of 7 characters or more is fine.
- Task 2
Recover: move the work to table-booking
Move Bunty's three commits to their own branch, and put main back where GitHub has it.
1. Make a branch called table-booking where main is now.
2. Switch to table-booking. Bunty's unfinished change comes with you.
3. Move main back to GitHub's main. Do not use a hard reset: it deletes the unfinished line.
4. Push table-booking to GitHub. Do not push main.
To pass: table-booking has Bunty's three commits on top of main. main is exactly at GitHub's main, and GitHub's main does not have his commits. GitHub has table-booking with the three commits. The unfinished SMS line in booking.js is still in the folder, or committed on table-booking.
- Task 3
Guard it: no commits on main
Make Git refuse commits on main, so new work always starts on a branch.
1. Write a hook in .git/hooks/pre-commit. Start it with a line like #!/bin/sh.
2. If the current branch is main, print a reason and exit with 1.
3. On any other branch, exit with 0.
4. Make the hook executable.
To pass: the Check tries it in a copy of your repository. A commit on main must be refused, and main must not move. A commit on a new branch must go through.
What you will learn
- See what happened: find where main belongs
- Recover: move the work to table-booking
- Guard it: no commits on main
More in Lost and found
Build "Commits made on main instead of a branch" 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.