Part IV: Chapter 4.7

Merge Conflicts

The conflict markers explained.

A merge conflict is not an error — it is Git saying "both branches changed the same lines and I don't know which version you want." Git writes both versions into your file, separated by markers, and waits for you to decide.

The Conflict Lifecycle

Start the merge to see how the conflict markers appear, then resolve it using one of the three strategies.

BASE (common ancestor)
function greet(name) {
return "Hello, " + name;
}
OURS — main (HEAD)
function greet(name) {
return `Hi there, ${name}!`;
}
THEIRS — feature
function greet(name) {
return `Hey ${name}, welcome!`;
}

What Causes a Conflict?

Git runs two diffs against the common ancestor: one for your branch, one for theirs. A conflict occurs when the same line (or adjacent lines) was changed on both sides. Git cannot auto-merge because it has no way to know which intent should win.

Git will never conflict on lines that were only changed on one side — those are taken automatically.

Deleting a file on one branch while the other branch modified it will also cause a conflict.

The Incomplete Merge State

While a conflict is unresolved, Git records that the merge is in progress inside .git/MERGE_HEAD — it stores the commit hash of the branch being merged. git statuswill show the repo as "merging" until all conflicts are resolved and committed.

If you want to abandon the merge entirely and get back to where you were, use git merge --abort. Git will restore the pre-merge state.

The Full Resolution Workflow

After resolving every conflict in every file, you stage them and complete the merge with a commit. Notice: there is no -m flag needed — Git pre-fills the commit message.

Terminal
$git merge feature
Auto-merging greet.js
CONFLICT (content): Merge conflict in greet.js
Automatic merge failed; fix conflicts and then commit the result.
$git status
On branch main
You have unmerged paths.
(fix conflicts and run "git commit")
Unmerged paths:
both modified: greet.js
$# ... edit greet.js, remove the markers, keep what you want ...
$git add greet.js
$git commit
[main m1n2o3p] Merge branch 'feature'