Sub topic 4
Why conflicts happen
This chapter assumes you can create commits and branches, and that you know how a three-way merge uses a merge base. Here you will see exactly when Git stops with a conflict and when it doesn't.
The rule: overlapping changes
Git compares each branch to the merge base, which is the last commit both branches share. It combines the changes from both sides automatically when they touch different parts of a file. A conflict happens when both sides changed the same lines, or lines so close together that Git cannot tell which change should win. Git does not understand what your code means. It only compares text.
| Situation | Result |
|---|---|
| Only one branch changed a file | Merges cleanly |
| Both branches changed different, well-separated regions | Merges cleanly |
| Both branches changed the same lines differently | Conflict |
| Both branches made the identical change | Merges cleanly |
| One branch edited a file and the other deleted or renamed it | Conflict (covered later) |
Reproduce a conflict
This script builds a repository where both branches change the same line after their common commit.
git init -b main demo && cd demo
printf 'timeout = 10\n' > config.txt
git add config.txt && git commit -m "Add config"
git switch -c feature
printf 'timeout = 60\n' > config.txt
git commit -am "Raise timeout to 60"
git switch main
printf 'timeout = 30\n' > config.txt
git commit -am "Raise timeout to 30"
git merge featureThe last command stops with a message like this:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.Git compared the base (timeout = 10) with each side. Main says 30 and feature says 60. Neither is the base, and they disagree, so Git cannot choose.
What Git has done at this point
Git has not failed or lost anything. It merged every file it could and left only the conflicted files for you. The repository is now in a merging state. Both commits are still intact, and the merge is unfinished until you create the merge commit.
Common causes in practice
- Long-lived branches. The longer two branches diverge, the more lines they both touch.
- Reformatting. Re-indenting or auto-formatting a file changes many lines and collides with other people's edits.
- Hot files. Files that everyone edits, such as a changelog, a dependency list, or a routing table, conflict often.
- Moved or renamed code. Git tracks lines, so moving a block while someone else edits it can produce confusing overlaps.
Conflicts that are not conflicts to Git
A clean merge can still be wrong. If one branch renames a function and the other adds a call to the old name, the changes touch different lines, so Git merges them without complaint. The code then fails to compile or run. Always build and test after a merge, even when Git reports no conflicts.
Exercises
Exercise
Branch main changes line 1 of app.py, and branch feature changes line 40 of the same file. Both start from the same base commit. Will git merge feature on main conflict, and why?
Show answer
Usually not. The edits are in different, well-separated regions compared with the merge base, so Git applies both. It would conflict only if the changed lines were adjacent or overlapping.
Exercise
A merge finished with no conflicts, but the program now crashes. What kind of problem is this, and what should you do?
Show answer
It is a semantic conflict. The changes did not overlap as text, but they are incompatible in behavior. Run the build and tests after every merge, and fix the code in a follow-up commit.