Unit 6: Merging
Putting two ideas back together.
Unit 6 of 8 in Git and version control for kids. Its 4 lessons are When Both Sides Moved, When git Cannot Decide, Fixing a Conflict and Conflict Boss — below is everything each one explains, and a question or two from it to try.
Every terminal session on this page was replayed before it shipped, on a git whose every message is tested against the real one.
This unit opens with a fortnight’s trial of everything — no card needed — or with a family plan, bought in the iPhone app. The first two units of every track are free for ever. Try it in the app.
🤝 When Both Sides Moved
A merge with actual work to do
Last unit, main sat still while you worked. This time it moved too — someone added a file to main while your branch added another.
Now git cannot just slide a label. It makes a merge commit: a commit with two parents, joining both lines together.
Terminal
$ git merge hats -m "Merge the hats" Merge made by the 'ort' strategy. hats.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 hats.txt $ ls hats.txt shoes.txt shop.txt
Seeing the join
git log --oneline --graph draws the shape. The lines split apart where the branch began and come back together at the merge commit.
Terminal
$ git log --oneline --graph * da0f4bd (HEAD -> main) Merge the hats |\ | * 21ee6a9 (hats) Sell hats * | 1628a1d Sell shoes |/ * 432f8ac Open the shop
Try it yourself
What is special about a merge commit?
- It has two parents
- It has no message
- It cannot be undone
- It deletes a branch
Two branches changed different files. What does git do?
- Refuses to merge
- Merges them quietly — there is nothing to argue about
- Asks you which file to keep
- Deletes one of them
Answer them in the app
💥 When git Cannot Decide
The same line, two ways
Git is good at combining edits — as long as they are in different places.
If you changed line 2 and your friend changed line 2, git has no way to know which is right. It stops, and asks you. That is a conflict. It is not an error, and nothing is broken.
Different lines: no problem at all
Watch this. Two branches both edit poem.txt — one changes the first line, one changes the last. Git merges them without a word.
Terminal
$ git merge top -m "Merge top" Auto-merging poem.txt Merge made by the 'ort' strategy. poem.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) $ cat poem.txt ONE two THREE
The same line: a conflict
Now both branches change the *same* line. Git stops and writes both versions into the file, fenced off with markers.
Between <<<<<<< and ======= is yours. Between ======= and >>>>>>> is theirs.
Terminal
$ git merge sam Auto-merging list.txt CONFLICT (content): Merge conflict in list.txt Automatic merge failed; fix conflicts and then commit the result. $ cat list.txt apples <<<<<<< HEAD cherries ======= bananas >>>>>>> sam
Try it yourself
When does git stop and ask for help?
- Whenever two branches change the same file
- Whenever two branches change the same lines of a file
- Every single merge
- Only when a file is deleted
In a conflict, which part is your own version?
- Between <<<<<<< HEAD and =======
- Between ======= and >>>>>>>
- The last line of the file
- Git never shows your version
Answer them in the app
🛠️ Fixing a Conflict
Three steps, every time
1. Edit the file so it says what you actually want, and delete all three marker lines.
2. Run git add on it — that is how you tell git "sorted".
3. Run git commit to finish the merge.
You are the one who decides. Git only refused to guess.
Terminal
$ printf "apples\nbananas\ncherries\n" > list.txt $ git add list.txt $ git commit -m "Keep both fruits" [main 4964701] Keep both fruits $ cat list.txt apples bananas cherries
Status walks you through it
Stuck in the middle of a conflict? git status names the files it needs you to fix, under Unmerged paths, and tells you the next command.
Terminal
$ git status On branch main You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Unmerged paths: (use "git add <file>..." to mark resolution) both modified: list.txt no changes added to commit (use "git add" and/or "git commit -a")
The escape hatch
Panicking? git merge --abort puts everything back exactly as it was before you typed merge. Nothing is lost, and you can try again when you are ready.
🏆 Conflict Boss
Try it yourself
A conflict appears. Whose fault is it?
- Yours, for using branches
- Git's, for being broken
- Nobody’s — two people edited the same lines, and git is asking you to choose
- The computer needs restarting
Answer it in the app