Sub topic 1
Why the same conflict keeps coming back
Prerequisites
This chapter assumes you can:
- Commit, branch, merge, and rebase in Git.
- Read conflict markers (
<<<<<<<,=======,>>>>>>>) and resolve them by hand. - Run Git commands in a terminal.
You don't need to know git rerere yet. This chapter explains the problem it solves. Later chapters cover the tool.
What a conflict is
Git merges two lines of history by comparing each one to their common ancestor. If only one side changed a region of a file, Git takes that change. If both sides changed the same region in different ways, Git can't choose. It stops and writes both versions into the file between conflict markers:
<<<<<<< HEAD
color=green
=======
color=blue
>>>>>>> featureThe text above ======= is your side. The text below it is the other side. You edit the file into the form you want, remove the markers, stage the file, and finish the operation.
A conflict depends on three inputs: the common ancestor, your side, and their side. Git keeps no memory of how you resolved it. Once you commit, the resolution exists only as the content of the merge commit.
Why you resolve the same conflict twice
If the merge commit stays in your history, you never see that conflict again. The trouble starts when the merge result is thrown away or never kept. Then the same three inputs come back, and Git asks the same question again. Common cases:
- Test merges. You merge a topic branch into a scratch copy of the main branch to check that it integrates. You discard the result with
git reset --hardor by deleting the branch. Later you do the real merge, and every conflict returns. - Rebasing a long-lived branch. A rebase replays your commits one at a time onto a new base. If commit 3 of 10 conflicts, you resolve it. Rebase the same branch onto a newer base next week, and that conflict can appear again.
- Aborted operations. You hit a conflict you can't solve right now, run
git merge --abortorgit rebase --abort, and your earlier work on other files is lost with it. - Rewritten history. After
git rebase -i, or when you split a commit, the replayed commits conflict again even though you resolved them before. - Parallel branches. Two release branches both need the same change from
main. Each cherry-pick or merge hits the same clash.
What the repeat costs
One repeated conflict on one line is a minor annoyance. The cost grows with the work involved:
- Time. Large conflicts can take an hour to understand, and you pay that hour again.
- Consistency. You resolve the same conflict differently the second time. The two results then disagree, and the difference can show up as a bug later.
- Context. You may no longer remember why you picked one side. The original decision is gone with the old working tree.
- Willingness to test. If every test merge costs a round of conflict resolution, people stop doing test merges.
Reproducing the problem
You can see the repeat on a small scale. This sequence creates a conflict, resolves it, discards the merge, and merges again:
git init conflict-demo && cd conflict-demo
printf 'color=red\n' > settings.txt
git add settings.txt
git commit -m "base"
git switch -c feature
printf 'color=blue\n' > settings.txt
git commit -am "feature: blue"
git switch -
printf 'color=green\n' > settings.txt
git commit -am "main line: green"
git merge feature # conflict
printf 'color=teal\n' > settings.txt
git add settings.txt
git commit -m "Merge feature"
git reset --hard HEAD^ # throw the merge away
git merge feature # the same conflict again
cat settings.txt # conflict markers are backThe second git merge feature stops with the same conflict, and settings.txt holds markers again. Your teal resolution is gone from the working tree. You would have to type it in again.
The next chapters show how git rerere ("reuse recorded resolution") records the teal decision the first time and fills it in the second time.
Exercise
Name two situations, other than a plain merge, where you can end up resolving the same conflict more than once, and explain why Git asks again each time.
Show answer
Examples: rebasing a long-lived branch onto a newer base, aborting a merge or rebase and starting over, cherry-picking the same change onto several release branches, or redoing a test merge after discarding it. Git asks again because each operation recomputes the merge from the common ancestor, your side, and their side. Git doesn't store the earlier resolution, so identical inputs give the same conflict.