Advanced

What a conflict actually is (and why it isn't a disaster)

A merge conflict feels alarming the first time, but it's one of the most honest things git does. It's git stopping and saying "two people changed the same spot, I can't guess what you wanted." You're the one who gets to decide, and that's a good position to be in.

What you will see

[main 7890abc] Merge branch 'feature/login' 1 file changed, 1 insertion(+), 1 deletion(-)

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

A merge conflict felt like the whole thing breaking the first time, but it was just two of us editing the same line. Let me force that exact situation, watch git stop and leave the markers, then resolve it so I can see that it is nothing to be alarmed about.

1

Start with a file both of us will edit, here that is two of you, one working copy and one branch. I make one commit so there is a clean shared starting line that later both branches will change.

echo "button_color = blue" > theme.py git add theme.py git commit -m "Set button color"

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[main abc1234] Set button color 1 file changed, 1 insertion(+)
Both branches must start from this exact commit, or git can merge automatically and there is no conflict to look at. The commit above is the shared line.
2

On the feature branch, change that one line to a different color. I stay on my new branch while main moves on too, which is what makes the two lines of history actually disagree on the same spot.

git switch -c feature/login echo "button_color = red" > theme.py git add theme.py git commit -m "Make button red"

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[feature/login def4567] Make button red 1 file changed, 1 insertion(+)
The branch name in brackets confirms this commit landed on feature/login, not main. If it did not, switch back before continuing.
3

Go back to main and change that same line to something else, then try to bring the branch in. Because both sides edited the identical line, git cannot pick a winner on its own. It stops and leaves the markers in the file, that is the honest part, git refusing to guess.

git switch main echo "button_color = green" > theme.py git add theme.py git commit -m "Make button green" git merge feature/login

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Auto-merging theme.py Conflict: merge conflict in theme.py Merge made by the 'ort' strategy. theme.py: needs merge
The merge did not finish. Git left the markers in place and will not commit until I delete them and stage a clean version.
4

Read the markers instead of panicking at them. Everything above ======= is my side (main), everything below is theirs (feature/login), and the split line is what they wrote. Two colors, one line, I keep whichever is right or combine them, then delete every marker.

type theme.py

Before you run it, say out loud what it will print. Then open the answer.

What it prints
<<<<<<< HEAD button_color = green ======= button_color = red >>>>>>> feature/login
Leaving a single marker in the file means the commit will be refused. Delete the <<<<, =======, and >>>>> lines, leaving only the final version.
5

Write the version I actually want, stage it, and commit to close the merge. The conflict disappears from history the moment I commit, and this becomes just another entry in the timeline.

echo "button_color = red" > theme.py git add theme.py git commit

Before you run it, say out loud what it will print. Then open the answer.

What it prints
[main 7890abc] Merge branch 'feature/login' 1 file changed, 1 insertion(+), 1 deletion(-)
Accept the merge message git offers, or edit it to say which color won and why. The commit is the one thing that actually finishes the merge.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

GitHub flags the conflicting lines, you choose the winner, and commit to resolve.
1 See the conflict in the diff
↓
2 Read both sides
↓
3 Pick a version or combine them
↓
4 Commit to resolve
Why this works
Why conflicts happen

A conflict appears when the same stretch of a file is changed by two people at once. They're completely normal, every collaborator hits them, the experienced ones included, and they always have a clean resolution.

How to fix them

Read both sides. Keep whichever is correct, or stitch them together into a version that makes sense. Delete the markers, commit, and the conflict is gone from the history.

Examples

The classic same-line conflict

Both you and a teammate changed line 10.
git shows you exactly where you disagree:

<<<<<<< HEAD
button_color = "blue"
=======
button_color = "red"
>>>>>>> feature/login

The markers tell the story: everything above ======= is what's on your current branch, everything below is what your teammate wrote. Read both sides, pick the one that's right (or keep both), delete the markers, and commit to resolve it.

Conflicts between nearby lines

<<<<<<< HEAD
const price = 10
=======
const price = 12
>>>>>>> feature

Not every conflict looks the same. Sometimes two people edit different lines that happen to sit right next to each other. Git still flags them as adjacent changes. GitHub shows both versions with the markers, so you edit the file to keep the lines that belong together, drop the markers, and save.

Fixing it without leaving the browser

Open the PR -> "Resolve conflict"
Edit the lines to the shape you want
Delete <<<< ======= >>>>>
Commit merge

GitHub ships a built-in editor for exactly this. You can pick each side with a button, let it strip the markers for you, and commit from there. It's much easier than poking at conflict markers in a plain text editor at 11 pm.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← A pull request is just a conversation
Previous Lesson
A pull request is just a conversation
Next Lesson
How teams actually stay in sync
How teams actually stay in sync →