Sub topic 16
Habits that reduce conflicts
This chapter assumes you can already resolve a conflict by hand, read conflict markers, and use git merge --abort. It covers what to do before a conflict happens. You can't eliminate conflicts, but a few habits make them rarer and much cheaper to fix.
What makes a conflict costly
A conflict happens when two branches change the same lines, or one branch changes lines the other deleted or moved, after their merge base. Two things drive the cost:
- Divergence. The longer two branches live apart, the more lines each one changes and the more likely they overlap.
- Lost context. The longer ago you wrote a change, the less you remember why you wrote it. Resolving a conflict means choosing between two intentions, so forgetting yours makes every choice slower and riskier.
Every habit below reduces divergence, preserves context, or both.
Keep branches short-lived
Merge each branch within days, not weeks. If a feature is too big for that, split it into steps that can each land on their own. Each step should leave the project working. Two common ways to do this:
- Merge preparatory refactors first, in their own branch, so the feature branch only adds behavior.
- Hide unfinished work behind a feature flag (a configuration switch that turns the new code off) so incomplete code can merge to
mainwithout affecting users.
Short branches also give code review less to look at, which is a separate benefit.
Bring in the main branch often
Don't wait until the end to find out how your work interacts with everyone else's. Merge the target branch into your branch regularly, for example at the start of each work session:
git fetch origin
git switch feature
git merge origin/mainIf a conflict appears, it contains only the changes since your last sync. That is usually one or two files, not twenty.
If your team prefers a linear history, rebase instead:
git fetch origin
git switch feature
git rebase origin/mainRemember the trade-off from the rebase chapter. A rebase replays your commits one at a time, so the same conflict can reappear several times. Frequent small rebases keep that count low. Never rebase commits that other people have already pulled.
Preview before you merge
You can find out whether a merge will conflict without changing your branch. The first method starts a real merge and stops before committing:
git switch main
git merge --no-commit --no-ff feature
git merge --abortIf the merge is clean, --abort still works because the merge is in progress but uncommitted. If it conflicts, you can inspect the conflicts and then abort.
The second method asks Git to compute the merge in memory and write nothing to your working tree. It needs Git 2.38 or later:
git merge-tree --write-tree main featureIt exits with status 0 for a clean merge and 1 if there are conflicts. In the conflict case it lists the conflicted files and messages, so it also works in scripts and CI.
The third method is a cheap check for overlap. It lists files that both branches changed since their merge base (this one needs Bash):
comm -12 \
<(git diff --name-only main...feature | sort) \
<(git diff --name-only feature...main | sort)Here main...feature means "changes on feature since the merge base". A file in both lists is not guaranteed to conflict, but it is where conflicts can occur. If the output surprises you, talk to the author of the other branch before you merge.
Make small, focused commits
A commit that does one thing is easier to merge, review, revert, and reason about when it conflicts. When Git shows you a conflict in a commit titled "Fix null check in parser", you know what your side meant. When the commit is "Various fixes", you don't.
Practical rules:
- One logical change per commit.
- Commit before you switch tasks, not at the end of the day.
- Use
git add -pto stage only the hunks that belong together, rather than committing everything in the working tree.
Keep formatting changes separate
Reformatting is the most common cause of avoidable conflicts. Re-indenting a file, sorting imports, or running an auto-formatter changes many lines at once. Every other branch that touches those lines now conflicts, even if its edits were small and unrelated.
Do this instead:
- Put a formatting change in its own commit, with no behavior changes in it.
- Merge it quickly, ideally when few other branches are open, and tell the team so they can sync.
- Agree on one formatter and run it automatically (an editor setting or a pre-commit hook), so formatting never drifts again.
The same applies to renaming a function across many files, moving files between directories, and other mechanical edits. Git detects renames by comparing contents, so a rename plus heavy edits in one commit can defeat detection. Move the file in one commit and edit it in another.
Structure code to spread changes out
Conflicts concentrate where many people edit the same lines. Watch for files that are conflict magnets:
- A single huge file that every feature touches. Splitting it by responsibility means different features edit different files.
- A list that everyone appends to, such as a registry, a routes table, or an enum. Two branches that both add an entry at the end conflict on the same line. Sorting entries alphabetically, or giving each entry its own file, spreads the edits out.
- Generated files such as lock files and compiled output. Don't hand-merge these. Resolve the source files first, then regenerate the file with the proper tool and stage the result. Better still, keep build output out of the repository.
Communicate about shared areas
Tools can't tell you that two people are about to redesign the same module. A short message in the team chat ("I'm restructuring billing/, avoid it until Thursday") prevents a conflict that no command could resolve cheaply, because the problem is two incompatible designs rather than overlapping text.
Reduce the cost when conflicts still happen
Some conflicts are unavoidable. Two settings make them cheaper:
git config --global merge.conflictStyle zdiff3
git config --global rerere.enabled trueThe first shows the base version inside conflict markers, so you can see what each side changed. It needs Git 2.35 or later, and diff3 is the older equivalent. The second lets Git replay a resolution you already made. That helps most when you rebase a long branch repeatedly, or when you merge the same branches in more than one direction.
Finally, verify after resolving. Build the project and run its tests before you commit the merge. A merge can be textually clean and still break the program, for example when one branch renames a function and the other adds a call to the old name. Frequent syncing and automated tests on the merge result catch these semantic conflicts early.
Summary of habits
| Habit | Reduces | Cost to you |
|---|---|---|
| Short-lived branches | Divergence | Planning work in smaller steps |
| Merging or rebasing from main often | Size of each conflict | A few minutes per sync |
| Previewing with `merge-tree` or `--no-commit` | Surprises | One extra command |
| Small, focused commits | Resolution effort | Staging discipline |
| Separate formatting commits | Mass conflicts | Coordinating timing |
| Talking about shared areas | Design-level conflicts | A short message |
Exercise
A teammate says your merge of feature into main "will probably conflict" because you both touched app.py. Which command tells you for certain, without changing your branch or working tree, and what does its exit status mean?
Show answer
git merge-tree --write-tree main feature (Git 2.38 or later) computes the merge in memory. Exit status 0 means the merge is clean, and status 1 means there are conflicts, which it lists. Both branches changing app.py only makes a conflict possible, because Git merges non-overlapping edits in the same file without a conflict.
Exercise
You want to run an auto-formatter over a whole module while three colleagues have open branches that edit it. How do you do it with the least conflict pain?
Show answer
Make the formatter run its own commit with no behavior changes. Tell the team first, and merge it quickly. Then your colleagues merge or rebase from main right away, so their later edits build on the formatted code. Setting up the formatter to run automatically stops the problem from recurring.