T7 Git and GitHub · Lesson 4
Merging and conflicts
When two lines of work have both moved on, git joins them with a merge commit, line by line from the version they share. When both changed the same lines, git stops and marks a conflict for you to settle: read both sides, write the result, and say what you decided.
- say why a fast-forward is not always possible, and find the merge base
- merge two lines that changed different parts, and read the merge commit
- recognise a conflict and read its markers
- see where a merge stands with git status, during the merge
- resolve a conflict, mark it with git add, and commit the merge
- back out of a merge with git merge --abort
The back cover
Two people improve the same plan on two lines of work, and then the lines must become one. Most of the time git joins them alone, line by line. When both changed the very same lines, it stops and asks you to decide.
These pages show how git joins two lines that have both moved on, using the commit they share, and the merge commit it makes. They show what a conflict looks like, what the repository holds while one is open, and how to settle it. They also show how to back out of a merge safely.
Before you start
The story: Two hands on one line
Both plans have now proved themselves, and both must join the main orders. The Archive joins the summer plan at once. Then it reaches the east gate's plan and stops: both plans changed the same line, the opening time of gate 4, in different ways.
The drawer will not choose between two engineers. It marks the line and waits.
Keeper Hollis is not surprised. "When two people disagree," she says, "the Archive asks you. Read both, decide, and say that you decided."
The last lesson ended with an easy case. main had not moved, so bringing the summer plan in was a fast-forward: git moved a name. Now both plans have proved themselves, and both must join main. After the summer plan went in, main had moved on. A fast-forward cannot bring the east plan in. Git must truly join two lines of work, and sometimes it needs your help to do it.
It builds on these ideas. Each one has a tiny example, so you can check that you remember it.
| What you already know | A tiny example | Where it comes back |
|---|---|---|
| Branches. | git switch -c east | Each plan is a branch. |
| Fast-forward. | Fast-forward | It works only when one side has not moved. |
| git log --all. | git log --oneline --all | With --graph, it draws the lines. |
| Ranges. | main..east | What a merge would bring. |
| A diff. | -old / +new | A merge compares each side with the start. |
| git add. | git add orders.txt | It marks a conflict as settled. |
| git commit -m. | git commit -m "why" | A merge commit says what was decided. |
| git status. | git status -s | It shows what is still in conflict. |
| git restore. | git restore orders.txt | A clean desk before a merge. |
| grep. | grep -n text file | To find marks left in a file. |
By the end of this lesson you will be able to:
- say why a fast-forward is not always possible, and find the merge base;
- merge two lines that changed different parts, and read the merge commit;
- recognise a conflict and read its markers;
- see where a merge stands with git status, during the merge;
- resolve a conflict, mark it with git add, and commit the merge;
- back out of a merge with git merge --abort.
How to follow this lesson
Most blocks start in a repository like lesson 03's, where main already holds the summer plan (gate 4 at ten) and a branch named east holds the east plan. In some, east adds gate 9; in others, east opens gate 4 at eleven. The label above each block says which. Each block starts afresh, so you can run them in any order.
In one line
This lesson is about joining two lines of work that have both moved on: the merge commit, the conflicts that git cannot settle alone, and how you settle them.
When both lines have moved on
At a glance
When main and a branch have both gained commits, a fast-forward is impossible: no single name can be moved to hold both lines.
Git first finds the merge base, the last commit that both lines share. Then it makes a three-way merge, comparing each side with that base. For each part of each file, a change made on one side only is taken from that side, and a part changed on neither side stays as it was. Only parts changed differently on both sides need a person.
Take a guess: What does the word ort mean, in git's message that a merge was made by the 'ort' strategy?
Here is the history now. main took the summer plan by a fast-forward, so main and summer point at d223728. The east plan, 4ea879c, grew from the first orders, aba517b, as summer did. Git can draw the history itself: --graph adds a picture to git log:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git log --oneline --graph --all
* 4ea879c (east) East: open gate 9
| * d223728 (HEAD -> main, summer) Summer: open gate 4 at ten
|/
* aba517b First orders
Read the picture from the top down; the newest commits are at the top. Each * is a commit. Each | is a line of work going down to older commits. The /, under the two tips, is where the two lines meet: both grew from aba517b.
| Mark | Means |
|---|---|
* | a commit |
| | a line of work, going on down |
/ or \ | two lines joining, or splitting apart |
Why a fast-forward is impossible
A fast-forward needs one line to be the other line plus more. Here, main has a commit that east lacks, and east has one that main lacks. Neither line contains the other. The option --ff-only, from lesson 03, makes git say so instead of merging:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge --ff-only east
hint: Diverging branches can't be fast-forwarded, you need to either:
hint:
hint: git merge --no-ff
hint:
hint: or:
hint:
hint: git rebase
hint:
hint: Disable this message with "git config set advice.diverging false"
fatal: Not possible to fast-forward, aborting.
Diverging means that the two lines have gone separate ways. The hints name two ways forward. A merge without --ff-only is the first, and this lesson's subject. A rebase is a way to rewrite commits; this course leaves it for later.
The merge base
To join two lines, git first finds the last commit that both share. It is called the merge base. The command git merge-base names it:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge-base main east
aba517bd0ac20cf6722879cf47522f787e978a68
The merge base is the first orders, where both plans began. Git then has three versions of every file: the merge base's, yours (main's) and theirs (east's). That is why this is called a three-way merge. For each part of a file, git compares the two sides with the merge base:
| The merge base had | Yours | Theirs | Git takes |
|---|---|---|---|
| open gate 4 at noon | open gate 4 at ten | open gate 4 at noon | yours: only you changed it |
| (nothing after check gate 5) | (nothing) | open gate 9 | theirs: only they changed it |
| a line | the same new line | the same new line | that line: both made the same change |
| a line | one new line | another new line | nothing: a conflict, for you to settle |
Notice that git never asks which side is "better". It asks only what changed, on each side, since the merge base. A part changed on one side only is taken from that side.
Worked example 1
Before joining the plans, Keeper Hollis asks which commit both plans started from, and what it holds.
What we have: main and east, which both grew from the first orders
What we need: the merge base, by its short name and its message
The plan: git merge-base names it in full; cut keeps the first 7 characters; git log -1 shows that one commit
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge-base main east | cut -c1-7
git log --oneline -1 aba517b
aba517b
aba517b First orders
- The two plans started from aba517b, the first orders. Every change since then, on either side, is what the merge must combine.
The merge base is aba517b, First orders.
Check: the graph above meets at aba517b, under the two tips.
A path that fails: taking main's newest commit as the starting point. That commit holds the summer plan, which east never had.
When you are stuck: you are not sure what a merge will have to combine
Run git log --oneline --graph --all, and find where the two lines meet.
Run git merge-base with the two branch names; it prints the commit where they meet.
main..east lists what east would bring; east..main lists what main has that east lacks.
Your turn
git merge-base main east | cut -c1-7
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
aba517b
- Both lines grew from the first orders.
- cut -c1-7 keeps the short name.
It prints aba517b.
Why can main not be fast-forwarded to east here?Tap to reveal
- A fast-forward needs one line to contain the other.
- main has the summer commit, which east lacks.
Because both lines have moved on since the merge base; neither contains the other.
The merge base says check gate 5; yours says check gate 5; theirs says check gate 5 twice. What does git take?Tap to reveal
- Only one side changed that line.
check gate 5 twice, theirs: only they changed it.
Did you know?
ort, Ostensibly Recursive's Twin
The ort in git's merge message is a name with a joke inside. The git merge manual says ort stands for Ostensibly Recursive's Twin, because it was written to replace the previous default method, recursive. GitHub's notes on Git 2.34, from 15 November 2021, say ort became the default in that release. They add that in testing ort consistently outperformed recursive, so merges got faster with fewer bugs.
Why it fits here: It answers this topic's question: ort stands for Ostensibly Recursive's Twin, git's default way of merging since Git 2.34.
Sources: git manual: git-merge (merge strategies); GitHub Blog: Highlights from Git 2.34
Two merge bases at once
Sometimes two lines share more than one last commit. The git merge manual says that when ort finds more than one common ancestor, it first merges those ancestors into one tree and uses that as the base of the three-way merge. It adds that tests on real merge commits from the history of the Linux 2.6 kernel showed this gives fewer conflicts without causing wrong merges.
Why it fits here: It extends this topic's merge base: when two lines share several last commits, ort first merges them into one base.
In one line
When both lines have moved on, a fast-forward is impossible. Git finds the merge base, the last commit both share, and makes a three-way merge: for each part of each file, a change made on one side only is taken from that side.
A merge that git finishes alone
At a glance
When the two lines changed different parts of the files, git can merge them without any help from you. Such a merge needs no decisions at all.
It writes the joined files and makes a merge commit: a commit with two parents, one from each line. Its message is Merge branch, then the branch's name. Give --no-edit to accept that message without an editor opening, or -m to write your own. The merge prints a diffstat of what came into the current branch.
Take a guess: Can one merge commit join more than two lines of work at once?
The summer plan changed the first line, and the east plan added a line at the end. They touched different parts of orders.txt, so git can combine them by itself. Here is the merge, run on main:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge --no-edit east
cat orders.txt
Auto-merging orders.txt
Merge made by the 'ort' strategy.
orders.txt | 1 +
1 file changed, 1 insertion(+)
open gate 4 at ten
close gate 2
check gate 5
open gate 9
The file now holds both plans: gate 4 at ten, from summer, and gate 9, from east. Read the output line by line:
| Line | Means |
|---|---|
Auto-merging orders.txt | both sides changed this file, so git combined it line by line |
Merge made by the 'ort' strategy. | the merge is done; ort is the name of git's way of merging |
orders.txt | 1 + | the diffstat: what main gained, here one line |
The merge commit
A three-way merge ends with a new commit, called a merge commit. It is special in one way: it has two parents, the tips of both lines. Here it is in the graph:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge -q --no-edit east
git log --oneline --graph
Auto-merging orders.txt
* 8b2aed0 (HEAD -> main) Merge branch 'east'
|\
| * 4ea879c (east) East: open gate 9
* | d223728 (summer) Summer: open gate 4 at ten
|/
* aba517b First orders
The | and \ under the merge commit are its two parents. The -q keeps the merge quiet, but git still says which file it combined. In the long form of git log, a merge commit has one more line, Merge:, which names its two parents, main's first:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge -q --no-edit east
git log -1
Auto-merging orders.txt
commit 8b2aed08048eb656938b52c44fc263bda8eccea2 (HEAD -> main)
Merge: d223728 4ea879c
Author: Asha <asha@tower.example>
Date: Thu Jan 1 10:00:12 2026 +0000
Merge branch 'east'
The parents have names of their own. HEAD^1 is the first parent, the line you were on; HEAD^2 is the second, the line you merged in. HEAD~1, from lesson 02, always follows the first parent.
The merge commit's message
Git writes the message itself: Merge branch 'east'. On a computer of your own, run in a terminal, git opens a text editor at this point, so that you can add to the message, and finishes the merge when you save and close it. The option --no-edit accepts git's message as it is. The option -m gives your own, as for git commit:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge -q -m 'Join the east plan: gate 9' east
git log --oneline -1
Auto-merging orders.txt
65d249b (HEAD -> main) Join the east plan: gate 9
No editor on this page
The small shell on this page has no text editor, and neither has the machine that checks it. So the blocks here always give --no-edit or -m. Without them, the merge stops and waits for a message; the Mistakes section shows what to type then.
Worked example 2
Bring the east plan into main, with a message that says what it adds, and check the result.
What we have: main with the summer plan; east, from the first orders, adding gate 9
What we need: a merge commit with a clear message, and orders.txt holding both plans
The plan: git merge with -m; then read the file, and the top of the graph
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge -q -m 'Join the east plan: gate 9' east
cat orders.txt
git log --oneline --graph | head -n 2
Auto-merging orders.txt
open gate 4 at ten
close gate 2
check gate 5
open gate 9
* 65d249b Join the east plan: gate 9
|\
- orders.txt holds both plans. The graph's first row shows the merge commit with its message, and the second shows its two parents splitting off. No branch names appear there, as the output goes into a pipe.
main now holds both plans, in the merge commit 65d249b.
Check: git log --oneline main..east prints nothing now: main has every commit of east.
A path that fails: git merge east with neither --no-edit nor -m. On this page, the merge then stops and waits for a message.
When you are stuck: a merge went through, but you are not sure what it did
Run git log --oneline --graph: the merge commit has two lines going down from it.
Run git log -1: the Merge: line names both parents.
Run git diff HEAD^1 HEAD to see what the merge brought into your line.
Your turn
git merge -q --no-edit east
git log --oneline -1
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Auto-merging orders.txt
8b2aed0 (HEAD -> main) Merge branch 'east'
- -q quietens the merge, but git still names the file it combined.
- With --no-edit, the message is git's own.
It prints Auto-merging orders.txt, then 8b2aed0 (HEAD -> main) Merge branch 'east'.
How many parents does a merge commit have, and which comes first?Tap to reveal
- Each line that the merge joins is a parent.
Two; the first is the line you were on when you merged.
Which option takes git's own message for a merge, without an editor?Tap to reveal
- The message would otherwise be edited.
--no-edit.
Did you know?
The octopus merge
A merge commit can have more than two parents. The git merge manual describes a strategy called octopus, which resolves cases with more than two heads and is the default when merging more than one branch at once. It refuses any complex merge that needs a person to resolve it, and is meant mainly for bundling several topic branches together in one commit.
Why it fits here: It answers this topic's question: the octopus strategy joins three or more lines in one merge commit.
A message that lists the work
The plain message Merge branch can say more. The git config manual says the setting merge.log adds, after the branch names, one-line descriptions of the commits being merged. Its value is the most it will list, and true means 20. So after git config merge.log true, a merge message also shows the subjects of the work that came in.
Why it fits here: It adds to this topic's default message: merge.log lists the subjects of the merged commits in the merge message.
In one line
When the two lines changed different parts, git merges by itself and makes a merge commit, with two parents and the message Merge branch 'name'; give --no-edit to accept that message, or -m for your own.
When both changed the same line
At a glance
When both lines changed the same lines in different ways, git cannot choose for you. It stops and marks a conflict.
Git writes both versions into the file, between markers on lines of their own. The marker of seven < signs and HEAD starts your side, seven = signs separate the two sides, and seven > signs with the branch's name end the other side. Changes that touch, on lines next to each other, can conflict too. Everything outside the markers was merged already.
Take a guess: Can a conflict also show what the lines said before either side changed them?
Now take the other east plan. It opens gate 4 at eleven, while main, with the summer plan, opens it at ten. Both sides changed the same line, in different ways. Git will not choose between two engineers:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that opens gate 4 at eleven; main is checked out
git merge east
echo "[$?]"
Auto-merging orders.txt
CONFLICT (content): Merge conflict in orders.txt
Automatic merge failed; fix conflicts and then commit the result.
[1]
This is a merge conflict: two changes to one part of a file, that git cannot combine. The merge stopped, with the status 1, before making any commit. Git has written both versions into orders.txt, between marks called conflict markers:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that opens gate 4 at eleven; main is checked out
git merge east > /dev/null
cat orders.txt
<<<<<<< HEAD
open gate 4 at ten
=======
open gate 4 at eleven
>>>>>>> east
close gate 2
check gate 5
| Marker | Means |
|---|---|
<<<<<<< HEAD | the start: below it is your version, the one on HEAD (main) |
======= | the middle: your version above, theirs below |
>>>>>>> east | the end: above it is their version, from east |
The lines outside the markers, close gate 2 and check gate 5, were the same on all sides, so git kept them as they were. Only the disputed part is marked. Each marker is seven characters long, at the start of its own line.
How close is too close?
Two changes need not be on the very same line to conflict. Git treats changes to lines that touch, with no unchanged line between them, as one area. Here one side changed line 3 of a file of numbers, and the other changed line 4:
In a repository whose n.txt holds the numbers 1 to 6: main changed 3 to three, and the branch b changed 4 to four
git merge b > /dev/null
cat n.txt
1
2
<<<<<<< HEAD
three
4
=======
3
four
>>>>>>> b
5
6
Each side's version covers both lines, and the merge waits for you. With one unchanged line between the two changes, git merges them by itself:
In a repository whose n.txt holds the numbers 1 to 6: main changed 2 to two, and the branch b changed 4 to four
git merge -q --no-edit b
cat n.txt
Auto-merging n.txt
1
two
3
four
5
6
| The two changes are | The merge |
|---|---|
| in different files | clean |
| in one file, with an unchanged line or more between them | clean |
| on lines that touch, or on the same line, and different | a conflict |
| the same change, on both sides | clean: git takes it once |
Worked example 3
Corin is sure that two plans cannot clash: one closes gate 2 at dusk, and the other checks gate 5 twice. Different gates, he says. Merge them and see.
What we have: main checks gate 5 twice (line 3); the branch dusk closes gate 2 at dusk (line 2)
What we need: whether the merge is clean
The plan: merge dusk into main, then read the file
In a repository whose main changed check gate 5 to check gate 5 twice, with a branch dusk that changed close gate 2 to close gate 2 at dusk
git merge dusk
cat orders.txt
Auto-merging orders.txt
CONFLICT (content): Merge conflict in orders.txt
Automatic merge failed; fix conflicts and then commit the result.
open gate 4 at noon
<<<<<<< HEAD
close gate 2
check gate 5 twice
=======
close gate 2 at dusk
check gate 5
>>>>>>> dusk
- Lines 2 and 3 touch, so git saw one area changed in two ways. Each side's version shows both lines: main's with gate 5 changed, dusk's with gate 2 changed.
It is a conflict, because the two changed lines touch.
Check: the markers surround lines 2 and 3 only; line 1, open gate 4 at noon, is outside them.
A path that fails: judging by the gates. Git judges by lines: what matters is whether the changed lines touch.
When you are stuck: a merge stopped with CONFLICT
Nothing is lost: the merge has stopped part way, and git waits for you.
Open the file named in the CONFLICT line, and find the markers.
Above ======= is your branch's version; below it is the other branch's.
Not ready to decide? git merge --abort puts everything back, as the section after next shows.
Your turn
git merge east > /dev/null
head -n 1 orders.txt
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
<<<<<<< HEAD
- The conflict is on the first line.
- So the file starts with the first marker.
It prints <<<<<<< HEAD.
In a conflict, which version is between <<<<<<< HEAD and =======?Tap to reveal
- HEAD is the branch you are on.
Yours: the version on the branch you were on when you merged.
One side changes line 2 and the other changes line 5 of the same file. Will they conflict?Tap to reveal
- Lines 3 and 4 are unchanged between them.
No: unchanged lines separate them, so git merges both.
Did you know?
A third version between the markers
A conflict can show where both sides began. The git config manual says the default style, merge, shows your side and the other side. The style diff3 adds a marker of seven bars and the original text before the = signs. A newer style, zdiff3, works like diff3 but pulls out lines that match on both sides. So git config merge.conflictStyle zdiff3 shows the base in every conflict.
Why it fits here: It answers this topic's question: the diff3 and zdiff3 styles add the original lines to each conflict.
Markers of 32 characters
Seven characters is only the usual length of a marker. The gitattributes manual describes conflict-marker-size, which sets how long the markers are for a file. Its own example gives git's manual page on merging markers of 32 characters, instead of the usual 7. That page shows conflict markers as examples, so longer real markers keep the two apart.
Why it fits here: It adds to this topic's seven-character markers: the length can be changed per file, as git does for its own merge manual.
In one line
When both sides changed the same lines, or lines that touch, in different ways, git stops with a conflict and writes both versions into the file between the markers <<<<<<< HEAD, ======= and >>>>>>> name.
In the middle of a merge
At a glance
A merge that stopped at a conflict is half done. Until you settle it, the repository is in a special state.
git status says that a merge is under way and lists each unmerged file, and git status -s marks such a file UU. The tray keeps three versions of it: stage 1 is the merge base, stage 2 is yours, and stage 3 is the other branch's. Git refuses to make a commit until every conflict is settled. Read git status first.
Take a guess: Can git remember how you settled a conflict, and settle the same one for you next time?
A stopped merge is a state of its own. Until you finish it or abort it, git remembers that a merge is under way. git status says so first:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
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: orders.txt
no changes added to commit (use "git add" and/or "git commit -a")
An unmerged path is a file still in conflict. Both modified means that both sides changed it. The hints give the two ways out: settle the conflict and commit, or abort the merge. The short form puts it in two letters, U for unmerged:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git status -s
UU orders.txt
The three versions, kept on the tray
While a file is in conflict, the tray holds not one version of it but three, in numbered places called stages. Stage 1 is the merge base, stage 2 is yours, and stage 3 is theirs. git show reads each one, written as a colon, the stage, a colon and the file's name:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git show :1:orders.txt | head -n 1
git show :2:orders.txt | head -n 1
git show :3:orders.txt | head -n 1
open gate 4 at noon
open gate 4 at ten
open gate 4 at eleven
| Stage | Holds | Here |
|---|---|---|
:1:orders.txt | the merge base's version | open gate 4 at noon |
:2:orders.txt | yours, from HEAD | open gate 4 at ten |
:3:orders.txt | theirs, from east | open gate 4 at eleven |
git diff during a conflict
git diff shows a conflicted file in a combined diff. It has two columns of marks, one for each side: the first for yours, the second for theirs:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git diff
diff --cc orders.txt
index 1ba9bbf,2a79fd0..0000000
--- a/orders.txt
+++ b/orders.txt
@@@ -1,3 -1,3 +1,7 @@@
++<<<<<<< HEAD
+open gate 4 at ten
++=======
+ open gate 4 at eleven
++>>>>>>> east
close gate 2
check gate 5
A + in a column means that the line on the desk is new compared with that side. The markers have ++, as neither side has them. The line open gate 4 at ten has a + in the second column only: yours already has it, theirs does not. Most people read the file itself rather than this; it is shown here so that it does not surprise you.
No commit until it is settled
Git refuses to commit while any file is unmerged. It lists each one with a U first:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git commit -m 'Too early'
echo "[$?]"
U orders.txt
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
[128]
The work tree, in the hint, is git's name for the desk.
Worked example 4
Before deciding, Keeper Hollis wants the first line of each version side by side: the first orders, the summer plan and the east plan.
What we have: a merge stopped at its conflict in orders.txt
What we need: line 1 of the merge base's, ours and theirs
The plan: the three stages hold the three versions; git show :N:orders.txt reads each, and head keeps its first line
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git show :1:orders.txt | head -n 1
git show :2:orders.txt | head -n 1
git show :3:orders.txt | head -n 1
open gate 4 at noon
open gate 4 at ten
open gate 4 at eleven
- The first orders opened gate 4 at noon; summer moved it to ten; east moved it to eleven. Both sides changed it, in different ways: the conflict in one picture.
noon in the merge base, ten on main, eleven on east.
Check: the markers in orders.txt show ten above ======= and eleven below it.
A path that fails: running git show orders.txt, without the colons and the stage number. That does not read the three versions on the tray at all.
When you are stuck: you have lost track of a merge in progress
Run git status: its first lines say whether a merge is under way, and what is still unmerged.
git status -s marks each conflicted file with two letters: UU for both modified.
git show :2:file and git show :3:file give your version and theirs, untouched.
Your turn
git status -s
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
UU orders.txt
- orders.txt is unmerged.
- Both sides modified it.
It prints UU orders.txt.
Which stage holds the other branch's version of a conflicted file?Tap to reveal
- Stage 1 is the merge base, and stage 2 is yours.
Stage 3: theirs.
Why does git commit refuse during a conflict?Tap to reveal
- A commit records the whole project, and one file is still undecided.
Because a file is still unmerged; git will not record a file with no decision in it.
Did you know?
rerere remembers your answer
Settling the same conflict twice is a waste, and git has a cure with an odd name: rerere, reuse recorded resolution of conflicted merges. Its manual says it records a conflict and the way you resolved it by hand, then applies that answer when the same conflict comes back. It helps with long-lived branches that meet the same conflict again and again. It works once rerere.enabled is set.
Why it fits here: It answers this topic's question: with rerere switched on, git reuses your earlier resolution of the same conflict.
Source: git manual: git-rerere
A tool for each conflict
You need not settle conflicts in a plain editor. The git mergetool manual says it runs one of several merge programs to resolve merge conflicts, and is typically run after git merge stops. Such programs show your side, the other side and often the base next to each other, so a decision can be made by clicking. git mergetool then opens each unmerged file in turn.
Why it fits here: It adds a helper to this topic's unmerged files: git mergetool opens each one in a merge program.
Source: git manual: git-mergetool
In one line
During a conflict, git status says that a merge is under way and lists each unmerged file; git status -s marks it UU. The tray keeps three versions, stage 1 the base, 2 yours and 3 theirs, and git refuses to commit until each conflict is settled.
Resolving the conflict
At a glance
Settling a conflict is an edit like any other: you decide what the lines should say, and write that. No command can know which side is right.
Edit the file into its final form, with every marker gone, then run git add on it to mark it settled. When every file is settled, commit with a message that says what was decided. To take one side whole, git checkout --ours keeps your version of a file and git checkout --theirs takes the other branch's.
Take a guess: Is there a command that warns you about a conflict marker you forgot to remove?
To resolve a conflict is to decide what the file should say, and tell git. There are three steps, and none is new.
- Edit the file into its final form: the agreed text, with every marker gone.
- Run
git addon it. This marks the conflict as resolved. - Run
git commit, which makes the merge commit.
The engineers talked, and agreed on half past ten. Here printf writes the agreed file; in real work you would edit it in your editor. Then git add marks it, and git status confirms:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
printf 'open gate 4 at half past ten\nclose gate 2\ncheck gate 5\n' > orders.txt
git add orders.txt
git status
On branch main
All conflicts fixed but you are still merging.
(use "git commit" to conclude merge)
Changes to be committed:
modified: orders.txt
No file is unmerged now, but the merge is still under way: only a commit finishes it. The commit message should say how the disagreement was settled, so that anyone reading the history later knows why:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
printf 'open gate 4 at half past ten\nclose gate 2\ncheck gate 5\n' > orders.txt
git add orders.txt
git commit -m 'Join east: open gate 4 at half past ten, as both engineers agreed'
git log --oneline --graph
[main 8dd0400] Join east: open gate 4 at half past ten, as both engineers agreed
* 8dd0400 (HEAD -> main) Join east: open gate 4 at half past ten, as both engineers agreed
|\
| * 05735ad (east) East: open gate 4 at eleven
* | d223728 (summer) Summer: open gate 4 at ten
|/
* aba517b First orders
The merge commit has two parents, as before. Git prints no summary of changed lines for a merge commit; git log --stat shows none for one either.
Taking one side whole
Sometimes one side is right as it stands. git checkout --ours puts your version of the file on the desk, and --theirs puts theirs. Either way, the file is still unmerged until you add it:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git checkout --ours orders.txt
head -n 1 orders.txt
git status -s
Updated 1 path from the index
open gate 4 at ten
UU orders.txt
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git checkout --theirs orders.txt
git add orders.txt
git commit -q -m 'Join east: take its gate 4 at eleven'
head -n 1 orders.txt
Updated 1 path from the index
open gate 4 at eleven
Check that no marker is left
A marker left in a file is the classic slip, as git add accepts any text. Before adding, search for lines that start with seven <, = or > characters:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
grep -n '^[<=>]\{7\}' orders.txt
1:<<<<<<< HEAD
3:=======
5:>>>>>>> east
Here the search finds all three markers, as nothing is settled yet. After a good resolution it prints nothing at all.
Worked example 5
The engineers have agreed on half past ten. Settle the conflict, check the file, and record the decision.
What we have: a merge stopped at its conflict in orders.txt
What we need: the agreed file, no markers, a merge commit whose message gives the decision
The plan: write the file; search for markers; git add; git commit -m; then git log -1 to read the record
In the same repository, where git merge east has just stopped at its conflict in orders.txt
printf 'open gate 4 at half past ten\nclose gate 2\ncheck gate 5\n' > orders.txt
grep -c '^[<=>]\{7\}' orders.txt
git add orders.txt
git commit -q -m 'Join east: open gate 4 at half past ten, as both engineers agreed'
git log -1 | head -n 2
0
commit 8dd0400382ba1d5585ef4b506623f643a1335011
Merge: d223728 05735ad
- grep -c counted 0 markers, so the file is clean. The commit's Merge: line shows the two parents: main's d223728 and east's 05735ad.
The merge is recorded as 8dd0400, with the decision in its message.
Check: git status now says nothing to commit, working tree clean.
A path that fails: adding the file without removing the markers. git add accepts it, and the markers go into the record, as the Mistakes section shows.
When you are stuck: a conflict will not go away
git status still says unmerged: run git add on the file once its text is right.
git status says still merging: run git commit to finish.
grep -n '^[<=>]\{7\}' file finds any marker left behind.
To start the file again from one side, use git checkout --ours or --theirs.
Your turn
You have edited the conflicted file into shape. Which two commands finish the merge?Tap to reveal
- One marks the file as resolved.
- One makes the merge commit.
git add file, then git commit.
What does git status say after git add, but before git commit?Tap to reveal
- No file is unmerged now.
- But the merge is not finished.
All conflicts fixed but you are still merging.
git checkout --theirs orders.txt
head -n 1 orders.txt
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
Updated 1 path from the index
open gate 4 at eleven
- --theirs takes east's version.
- East opens gate 4 at eleven.
It prints Updated 1 path from the index, then open gate 4 at eleven.
Did you know?
A check for leftover markers
git add accepts a file that still holds markers, so the slip is easy to make. The git diff manual says the option --check warns if changes introduce conflict markers or whitespace errors. Running git diff --check before git add points at any marker left behind, with its file and line, before it can be committed.
Why it fits here: It answers this topic's question: git diff --check warns about conflict markers left in a file.
Source: git manual: git-diff
ours and theirs can swap
--ours and --theirs do not always mean what they seem. The git checkout manual says they take stage 2, ours, or stage 3, theirs, of an unmerged file. It warns that during git rebase and git pull --rebase the two may appear swapped: --ours gives the branch being rebased onto, and --theirs gives the branch holding your work. Read the conflict before choosing a side.
Why it fits here: It sharpens this topic's --ours and --theirs: in a rebase their meanings swap, so check which side is which.
Source: git manual: git-checkout
In one line
To resolve a conflict, edit the file into its final form with every marker gone, run git add on it, and commit with a message that says what was decided. git checkout --ours or --theirs takes one side whole.
Backing out: git merge --abort
At a glance
Sometimes a merge is not worth finishing now: the conflicts are bigger than expected, or the wrong branch was named.
The command git merge --abort stops a merge that is under way. It puts the tray and the desk back as they were before the merge began, and leaves untracked files alone. With no merge under way there is nothing to abort, and git says so. After an abort, the branch is exactly where it stood. It is the merge's emergency exit.
Take a guess: Can a merge be undone after it has already finished?
Sometimes the right answer to a conflict is: not yet. The engineers may not have agreed, or you merged the wrong branch. git merge --abort stops the merge and puts the tray and the desk back as they were before it began:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git merge --abort
git status
head -n 1 orders.txt
On branch main
nothing to commit, working tree clean
open gate 4 at ten
The markers are gone, orders.txt holds main's version again, and no merge is under way. Nothing was committed, so the history is as it was. The branch east still holds its plan, ready for another try.
What the abort keeps
The abort undoes only what the merge did. A file that git does not track, made during the merge, stays where it is:
In the same repository, where git merge east has just stopped at its conflict in orders.txt
printf 'ask both engineers\n' > notes.txt
git merge --abort
git status -s
?? notes.txt
That is why git refuses to start a merge when files it would change have changes not yet committed: the abort could not tell your changes from the merge's. The Mistakes section shows that refusal.
Only during a merge
With no merge under way, there is nothing to abort, and git says so:
In a repository whose orders.txt (open gate 4 at noon, close gate 2, check gate 5) has one commit, on main
git merge --abort
echo "[$?]"
fatal: There is no merge to abort (MERGE_HEAD missing).
[128]
MERGE_HEAD is a small file in .git that names the branch's commit being merged. It exists only while a merge is under way, and it is how git, and its status, know that one is.
| Situation | Command |
|---|---|
| a conflict you can settle now | edit, git add, git commit |
| a conflict you cannot settle yet | git merge --abort |
| the wrong branch merged, and stopped | git merge --abort |
| no merge under way | nothing to abort |
Worked example 6
Merge east, see the conflict, and decide to wait until the engineers have talked. Leave everything as it was before.
What we have: main with the summer plan; east, which opens gate 4 at eleven
What we need: no merge under way, and main still at its summer commit
The plan: merge, which stops at the conflict; abort; then check the status and the last commit
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that opens gate 4 at eleven; main is checked out
git merge east > /dev/null
git merge --abort
git status -s
git log --oneline -1
d223728 (HEAD -> main, summer) Summer: open gate 4 at ten
- git status -s printed nothing: the desk is clean. main is still at d223728, the summer commit, and no merge commit was made.
Everything is as it was, with main at d223728.
Check: cat orders.txt shows no markers, and open gate 4 at ten.
A path that fails: deleting the markers by hand and carrying on. The merge would still be under way, and the next git commit would make a merge commit.
When you are stuck: git merge --abort did not do what you expected
There is no merge to abort: no merge is under way, so nothing needs undoing.
A file is still there after the abort: if git did not track it, the abort leaves it alone.
To try the merge again later, run git merge with the branch's name once more.
Your turn
git merge --abort
git status -s
echo "[done]"
What does this print? Say your answer aloud first, then press Run or tap to reveal.Tap to reveal
[done]
- The abort puts the tray and the desk back.
- So git status -s finds nothing to report.
It prints only [done].
After git merge --abort, is there a merge commit?Tap to reveal
- The merge stopped before it could commit.
No: the abort undoes the merge, and nothing was committed.
Which file in .git tells git that a merge is under way?Tap to reveal
- Its name says that it points at the commit being merged.
MERGE_HEAD.
Did you know?
Commit or stash before you merge
--abort is not a perfect undo. The git merge manual says that if there were uncommitted changes on the desk when the merge started, git merge --abort will in some cases be unable to put them back. It therefore recommends always committing or stashing your changes before running git merge. A clean desk makes every abort exact.
Why it fits here: It sharpens this topic's --abort: changes left uncommitted before a merge may not survive an abort, so commit or stash first.
Source: git manual: git-merge
ORIG_HEAD, the way back
A merge that has finished can still be undone. Git's manual on naming revisions says commands that move HEAD in a drastic way, git merge among them, record where HEAD was before in ORIG_HEAD. That lets you put the branch's tip back as it was before the command ran. So git reset --hard ORIG_HEAD, right after a merge, takes the branch back to its old tip.
Why it fits here: It answers this topic's question: after a finished merge, ORIG_HEAD still names the old tip, so the merge can be undone.
Source: git manual: gitrevisions
In one line
The command git merge --abort stops a merge that is under way and puts the tray and the desk back as they were, leaving untracked files alone; with no merge under way, there is nothing to abort.
Where it is used
Merges are part of every team's week. Here is where each idea of this lesson does its work.
| What you learned | Where it is used | A real example |
|---|---|---|
| three-way merges | joining a finished feature into main | a teammate's branch merged after review |
| merge commits | keeping a record of when two lines joined | git log --graph showing each feature's join |
| conflicts | two people editing the same part of a file | two fixes to one function |
| git status during a merge | knowing what is left to settle | a merge with conflicts in several files |
| resolving with a clear message | explaining a decision for the future | a message that says why one version won |
git merge --abort | backing out safely | merging the wrong branch by mistake |
On GitHub, which the next two lessons bring in, the same merges happen when a pull request is merged, and conflicts are settled in the same way, on a computer of your own.
Mistakes and debugging
These are the slips that people meet most often with merges. Every output below is the real one, from the code shown.
1. Committing the markers
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git add orders.txt
git commit -q -m 'Join east'
grep -c '^<<<<<<<' orders.txt
1
git add accepts any text, markers included, so the merge commit now records a broken file. The fix: before git add, search with grep -n '^[<=>]\{7\}' file; it must print nothing. A commit that is already made can be corrected by a new commit, or undone with tools that lesson 07 shows.
2. Committing before resolving
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git commit -m 'Join east'
U orders.txt
error: Committing is not possible because you have unmerged files.
hint: Fix them up in the work tree, and then use 'git add/rm <file>'
hint: as appropriate to mark resolution and make a commit.
fatal: Exiting because of an unresolved conflict.
orders.txt is still unmerged. The fix: edit the file, run git add on it, then commit.
3. Switching branches during a merge
In the same repository, where git merge east has just stopped at its conflict in orders.txt
git switch summer
fatal: cannot switch branch while merging
Consider "git merge --quit" or "git worktree add".
A merge is under way on main, so git will not leave it. The fix: finish the merge, or run git merge --abort, then switch.
4. Merging with changes in the way
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that opens gate 4 at eleven; main is checked out
echo 'oops' >> orders.txt
git merge east
echo "[$?]"
error: Your local changes to the following files would be overwritten by merge:
orders.txt
Please commit your changes or stash them before you merge.
Aborting
Merge with strategy ort failed.
[2]
The merge would write into orders.txt, which has a change not yet committed. Git refuses before doing anything. The fix: commit the change, or throw it away with git restore, then merge.
5. A merge with no message given, on this page
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge east
echo "[$?]"
Auto-merging orders.txt
hint: Waiting for your editor to close the file... error: cannot run editor: No such file or directory
error: unable to start editor 'editor'
Not committing merge; use 'git commit' to complete the merge.
[1]
On a computer of your own, an editor would open here with the message Merge branch 'east'. This page has no editor, so git reports that it could not start one. The merge is done but not committed. Git asks for an editor only when its input and its output are both the terminal; with its output going into a file or a pipe, it takes its own message without asking. The fix: finish the merge with git's own message:
In a repository whose main holds the summer plan (gate 4 at ten), with a branch east, made from the first orders, that adds open gate 9; main is checked out
git merge east
git commit --no-edit
git log --oneline -1
Auto-merging orders.txt
hint: Waiting for your editor to close the file... error: cannot run editor: No such file or directory
error: unable to start editor 'editor'
Not committing merge; use 'git commit' to complete the merge.
[main c92ba6d] Merge branch 'east'
c92ba6d (HEAD -> main) Merge branch 'east'
The debugging corner: is a merge under way?
When git refuses something unexpected, run git status first. If its second line says You have unmerged paths, or All conflicts fixed but you are still merging, a merge is under way: finish it with git add and git commit, or leave it with git merge --abort.
Quick card
The whole lesson on one card. Come back to it whenever you need a reminder.
The lesson in code
git log --oneline --graph --all # two lines of work
git merge east # a conflict on gate 4
git status -s # UU: unmerged
printf 'open gate 4 at half past ten\nclose gate 2\ncheck gate 5\n' > orders.txt
git add orders.txt # marked resolved
git commit -q -m 'Join east: gate 4 at half past ten'
git log --oneline --graph | head -n 3
* 05735ad (east) East: open gate 4 at eleven
| * d223728 (HEAD -> main, summer) Summer: open gate 4 at ten
|/
* aba517b First orders
Auto-merging orders.txt
CONFLICT (content): Merge conflict in orders.txt
Automatic merge failed; fix conflicts and then commit the result.
UU orders.txt
* 440ead8 Join east: gate 4 at half past ten
|\
| * 05735ad East: open gate 4 at eleven
| When you see | It means | Do |
|---|---|---|
Merge made by the 'ort' strategy. | a clean merge, with a merge commit | nothing to fix |
Not possible to fast-forward | both lines moved on, and --ff-only was given | merge without --ff-only |
CONFLICT (content) | both sides changed the same lines | open the file, find the markers |
UU in git status -s | unmerged: both modified | settle it, then git add |
All conflicts fixed but you are still merging. | resolved, not committed | git commit |
Committing is not possible | a file is still unmerged | settle it and add it |
cannot switch branch while merging | a merge is under way | finish it, or git merge --abort |
would be overwritten by merge | a change on the desk is in the way | commit it, or git restore |
The story: The Archive asks
You read the marked line: one engineer's version above, the other's below, and the markers that separate them. You talk to both engineers, write the line they agree on, remove the markers, and file the joined sheet with a note that says how the disagreement was settled.
The joined orders carry both plans, and a record of the choice.
Corin says that he would have picked one at random. The Keeper says that she knows, which is why she asked you.
Reagents gathered: merge, merge conflict, conflict markers, three-way merge, merge commit, git merge --abort.
Sources
- Git reference documentation (the manual pages of git 2.53) (GPL-2 (Debian's copyright file for git 2.53, which covers the documentation with the code; read 2026-11-07); facts and ideas only)
- Pro Git (second edition), by Scott Chacon and Ben Straub (CC BY-NC-SA 3.0 (the book's page on git-scm.com, read 2026-11-07); facts and ideas only)