Sub topic 4
Why conflicts happen
This chapter assumes you can commit, create and switch branches, and read git status and git log. It also assumes you can edit a file in a terminal or editor. Here you learn what makes Git stop a merge. Later chapters cover how to fix the conflict.
A conflict is Git declining to guess
A merge conflict means Git found two changes it cannot safely combine on its own. It is not an error in your repository, and nothing is damaged. Git stops, marks the affected files, and asks you to decide.
To see why, recall how a three-way merge works. Git finds the merge base, the most recent commit that both branches share. It then compares the base with each branch tip, which gives it two sets of changes. It applies both sets to the base. Where the sets don't touch the same lines, it combines them. Where they do, it stops.
Reproducing a conflict
This script builds the situation in the diagram. It needs Git 2.28 or later for git init -b, and a configured user.name and user.email.
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 prints something like this:
Auto-merging config.txt
CONFLICT (content): Merge conflict in config.txt
Automatic merge failed; fix conflicts and then commit the result.Compared with the base, main changed line 1 to 30 and feature changed line 1 to 60. Both edits are real changes, and they disagree. Git has no rule for choosing a timeout, so it stops.
The rules Git applies
Git works hunk by hunk, where a hunk is a run of changed lines. For each hunk it asks which sides changed it.
| What happened since the base | Result |
|---|---|
| Only one branch changed the lines | Git takes that branch's version |
| Both branches made the identical change | Git takes it once, with no conflict |
| Branches changed different, separated regions of a file | Git combines both changes |
| Branches changed the same lines differently | Conflict |
| Branches changed adjacent or overlapping lines | Conflict |
The adjacent-lines rule surprises people. If one branch edits line 4 and the other edits line 5, the edits touch the same hunk boundary. Git can't tell whether they are meant to fit together, so it reports a conflict. Edits separated by at least one unchanged line usually merge cleanly.
Here is a merge that succeeds because the edits are far apart:
git init -b main clean-demo && cd clean-demo
printf 'one\ntwo\nthree\nfour\nfive\n' > list.txt
git add list.txt && git commit -m "Add list"
git switch -c feature
printf 'ONE\ntwo\nthree\nfour\nfive\n' > list.txt
git commit -am "Change line 1"
git switch main
printf 'one\ntwo\nthree\nfour\nFIVE\n' > list.txt
git commit -am "Change line 5"
git merge feature --no-edit
cat list.txtThe merge completes and list.txt contains ONE on the first line and FIVE on the last. Both branches edited the same file, but Git found no overlap.
Causes beyond two edits to one line
Content conflicts are the most common kind. Git also reports conflicts when the two histories disagree about a file's existence or location.
- Edit versus delete. One branch changes a file and the other deletes it. Git cannot keep both outcomes, so the status shows
deleted by usordeleted by them. - Rename versus edit. Git detects renames by comparing content. If one branch renames a file and the other edits it, Git normally applies the edit to the renamed file. It can still fail if it doesn't recognize the rename, for example when the file was also heavily rewritten.
- Rename versus rename. Both branches rename the same file to different names.
- Both added. Both branches create a file at the same path with different content. There is no base version, so Git compares the two additions directly.
- Binary files. For images, archives and similar files Git cannot merge by line. If both sides changed the file, it is always a conflict.
A later chapter covers resolving these. For now, note that "conflict" covers more than text overlap.
Causes that look unreasonable
Some conflicts surprise you because the change felt small.
- Whitespace and line endings. Re-indenting a block, trimming trailing spaces, or converting CRLF to LF changes every affected line. Another branch that touched any of those lines now conflicts.
- Automatic formatters. Running a code formatter over a whole file rewrites many lines. Any parallel work in that file is likely to overlap.
- Moved code. Git tracks lines, not meaning. If you move a function to another part of the file, Git sees a deletion in one place and an addition in another. Edits made to the function on the other branch collide with the deletion.
- Generated and lock files. Files such as dependency lock files change in large blocks. Two branches that each add a dependency often edit neighboring lines.
- Long-lived branches. The longer two branches run apart, the more changes pile up on each side, and the more of them overlap.
What Git leaves behind
When a merge hits a conflict, Git merges everything it can and leaves the rest unfinished. Run git status in the first demo and you will see:
git statusOn 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: config.txtThree facts are worth knowing:
- Files that merged cleanly are already staged. Only the conflicted paths are listed as unmerged.
- The conflicted file in your working tree now contains marker lines (
<<<<<<<,=======,>>>>>>>) around the competing text. - Git records that a merge is in progress. Your branch pointer has not moved, and no merge commit exists yet.
A clean merge can still be wrong
Git detects textual overlap only. Suppose one branch renames a function from load() to load_config(), and another branch adds a new call to load() in a different file. Git sees edits in different places, merges without complaint, and leaves code that no longer works.
This is called a semantic conflict. No marker will warn you. The defense is to build and run your tests after every merge, even a clean one.
Reading a conflict as a diagnosis
When a conflict appears, ask three questions in order:
- Which file or files are listed as unmerged? That tells you the scope.
- What did each branch do to those lines since the base? That is the cause.
- Do the two changes need to be both kept, or is one of them obsolete? That decides the fix.
Looking at both histories helps with the second question. This command shows the commits on each side that touched the file since they diverged:
git log --merge --oneline -- config.txtDuring an unfinished merge, --merge limits the log to commits that touch the conflicted paths from either side. In the demo it lists the two timeout commits, which names the two intentions that collided.
Exercise
Branch main changes line 10 of app.py. Branch feature changes line 11 of the same file, and both changes are unrelated edits. Will git merge feature succeed without a conflict, and why? Name one other situation, not involving two edits to the same lines, in which Git also reports a conflict.
Show answer
Expect a conflict. Line 10 and line 11 are adjacent, so the two changes touch the same hunk boundary, and Git does not combine adjacent edits automatically. Edits separated by at least one unchanged line would merge cleanly. Other conflict situations include one branch editing a file that the other deleted, both branches adding a file at the same path with different content, and both branches changing the same binary file.