Git without fear · Chapter 16 of 28
Merge conflicts
By the end of this chapter you can finish a merge that stops with a conflict, calmly, and back out of one when you need to think first. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Branches
- 4 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
Meera has decided: cutting chai goes to 30 and bun maska to 55. Her prices are on the pricing branch. Meanwhile Bunty committed his own guess straight to main: "cutting chai 28 (i think?)". Kabir wants pricing merged into main, with Meera's price, today.
Both branches changed the same line, so Git cannot pick one for you. The merges you know so far just finish on their own. This one stops halfway, and the quick way out is a trap: last month Bunty met his first conflict, typed git add . and git commit, and the counter printed menus with <<<<<<< HEAD on them for a whole morning. Git let him do it.
What you build: 4 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Merge pricing into main and finish the merge
Merge pricing into main. Git stops with a conflict, and you finish the merge.
1. Make sure you are on main. Merge pricing with git merge.
2. Git stops with a conflict in menu.txt. This is normal.
3. Before you fix anything, do task 4 once: run git merge --abort, then merge pricing again.
4. Fix menu.txt (tasks 2 and 3) and stage it with git add.
5. Run git commit to finish the merge. Git's own merge message is fine.
To pass: main has a merge commit that joins in pricing's last commit, "New prices from Meera". No merge may still be in progress. Do not change the pricing branch.
- Task 2
Keep cutting chai at 30 in menu.txt
Choose Meera's price in the conflict: cutting chai at 30.
1. Run cat menu.txt during the conflict. The cutting chai line is there twice.
2. Keep Meera's line, Cutting chai 30. Delete Bunty's line, Cutting chai 28.
3. Keep every other line as it is. Bun maska 55 comes from pricing and stays.
4. Stage menu.txt and commit to finish the merge.
To pass: pricing is merged into main, and menu.txt as committed on main has exactly these five lines, in this order, and nothing else: Masala chai 40, Cutting chai 30, Adrak chai 35, Elaichi chai 35, Bun maska 55.
- Task 3
Delete every conflict marker
Git adds three marker lines to menu.txt. Delete all of them before you commit.
1. The marker lines are seven < signs, seven = signs and seven > signs.
2. Delete all three lines from menu.txt. Delete the = line between the two versions too.
3. Run cat menu.txt and see that no marker line is left. Then stage and commit.
To pass: pricing is merged into main, no file committed on main has a marker line, and menu.txt in the chapter folder has no marker line either.
- Task 4 · interview question
Interview: cancel a merge that has a conflict
Interview question: how do you cancel a merge that stopped with a conflict?
1. Merge pricing into main. Git stops with the conflict.
2. Before you fix anything, run git merge --abort.
3. Run git status. It says the working tree is clean again.
4. Now merge pricing again and finish it (tasks 1 to 3).
To pass: you ran git merge --abort in the chapter folder while the merge was stopped. It counts only if it worked, so run it before you finish the merge. A command with a typing mistake does not count, so type it again.
What you will learn
- A conflict: both branches changed the same lines, so the merge stops and waits for you.
- git merge --abort: the escape hatch that puts everything back as it was before the merge.
- The markers: how Git writes both versions into the file.
- Resolving: writing the file as it should be, then git add and git commit to finish the merge.
More in Branches
Build "Merge conflicts" 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.