Sub topic 16
Habits that reduce conflicts
You can't remove merge conflicts entirely, but a few habits make them rarer and cheaper to resolve. This chapter assumes you can branch, merge, and resolve a conflict by hand, as covered earlier in this tutorial.
Why habits matter
A conflict happens when two branches change the same lines after their merge base. The longer two branches stay apart, the more lines each one changes, and the more likely they overlap. Most good habits come down to one idea: keep the gap between branches small.
Keep branches short-lived
Merge work in days, not weeks. Split a large feature into pieces that can each land on its own. A branch that touches fewer files for less time has fewer chances to collide.
Sync with the main branch often
Bring the latest main into your branch regularly, so you resolve small conflicts as they appear instead of one large conflict at the end.
git fetch origin
git merge origin/mainIf your team prefers a linear history, rebase instead:
git fetch origin
git rebase origin/mainRebase only branches that nobody else has pulled. Rewriting shared history creates new problems.
Make small, focused commits
Small commits make it clear which change collided with which. They also let you drop or redo one commit during a rebase without losing unrelated work.
Separate formatting from logic
Reformatting a file changes many lines at once, and any other branch that touches that file will conflict with it. Do these things instead:
- Put whole-file formatting or renaming in its own commit or pull request.
- Merge that change quickly, before others branch off.
- Agree on a shared formatter and settings, so editors don't rewrite lines differently.
Talk about shared files
Some files attract conflicts: lock files, changelogs, route tables, and generated code. Tell teammates before you make a large change to one. For generated files, don't resolve by hand. Resolve the source file, then regenerate the output.
Check before you merge
A dry run shows whether a merge will conflict without leaving you in the middle of one:
git merge --no-commit --no-ff origin/main
git merge --abortIf the first command reports a conflict, you can look at it or abort. If it succeeds, abort anyway, or commit if you do want the merge.
Reduce the cost of the conflicts that remain
- Turn on
merge.conflictStyle zdiff3so markers show the base version. - Turn on
rerere.enabledso Git reuses resolutions you've already made. - Run your tests after every resolution, not only after a clean merge.
Summary table
| Habit | What it prevents |
|---|---|
| Short-lived branches | Large divergence from main |
| Frequent syncing | One big conflict at the end |
| Small commits | Hard-to-trace conflicts |
| Separate formatting commits | Wide, meaningless overlaps |
| Team communication | Two people editing the same hot file |
| Dry-run merges | Surprises in the middle of a merge |
Exercise
Your feature branch is three weeks old and main has changed a lot. Name two habits that would have made the merge easier, and say why.
Show answer
Syncing with main often would have shown conflicts in small pieces as they appeared. Keeping the branch short-lived, by splitting the feature into smaller changes, would have reduced how many lines could overlap. Putting any formatting changes in a separate, quickly merged commit would also have helped.
Exercise
Which command lets you test whether merging origin/main will conflict, without committing anything, and how do you leave that state?
Show answer
Run git merge --no-commit --no-ff origin/main. If it stops with conflicts or leaves a pending merge, run git merge --abort to return to the state before the merge.