DEV Community

Cover image for Merge Conflicts Are Preventable — Here's Why You Still Have Them
xxxn3m3s1sxxx
xxxn3m3s1sxxx

Posted on

Merge Conflicts Are Preventable — Here's Why You Still Have Them

Merge conflicts are one of the most frustrating parts of working with Git. You spend hours writing code, push it up, and then... conflict. Your teammate changed the same file. Now you have to manually resolve overlapping changes while hoping you don't break anything.

But here's the thing: merge conflicts are almost always preventable.

Why Conflicts Happen

Merge conflicts occur when Git can't automatically reconcile changes from two branches. This happens because:

  1. Both branches modified the same lines in the same file
  2. One branch deleted a file the other branch modified
  3. Both branches added a file with the same name but different content

The root cause is almost always poor coordination.

The Real Problem: Code Ownership

Traditional development has teams own entire codebases. You work on Feature A, your teammate works on Feature B. Both of you touch the same files because the code is tightly coupled.

This is the code ownership anti-pattern.

When you own "the authentication module," you're incentivized to make sweeping changes across that module. Your teammate working on "user profiles" also touches authentication because profiles need to verify users.

Both of you are working on different features. Both of you touch the same code. Conflict is inevitable.

Solution 1: Small, Frequent Commits

The simplest way to prevent conflicts is to commit small changes frequently. When your changes are small and focused, there's less opportunity for overlap.

Before:

# You work for 3 days, 500 lines changed
git commit -m "Added user authentication"
Enter fullscreen mode Exit fullscreen mode

After:

# You work for 1 hour, 50 lines changed
git commit -m "Added password validation"
Enter fullscreen mode Exit fullscreen mode

Small commits mean smaller conflicts when they do happen.

Solution 2: Feature Flags Instead of Long-Lived Branches

Long-lived branches are conflict factories. The longer a branch exists, the more likely it is to conflict with main.

Instead of:

git checkout -b feature/user-auth
# ... work for 2 weeks ...
git merge main  # CONFLICT!
Enter fullscreen mode Exit fullscreen mode

Use feature flags:

git checkout -b feature/user-auth
# ... work for 1 day ...
git merge main  # Clean merge
# ... work for 1 day ...
git merge main  # Clean merge
Enter fullscreen mode Exit fullscreen mode

Merge frequently. Use feature flags to hide incomplete work.

Solution 3: CODEOWNERS for Clear Boundaries

GitHub's CODEOWNERS file defines who owns which parts of the codebase. When you know exactly who to coordinate with, conflicts decrease.

# .github/CODEOWNERS
/src/auth/ @alice @bob
/src/api/ @charlie
/src/ui/ @diana
Enter fullscreen mode Exit fullscreen mode

If both Alice and Bob need to modify auth code, they know to coordinate first.

Solution 4: Stacked PRs for Large Changes

For large changes, use stacked PRs instead of one massive PR:

# Instead of one giant PR:
git checkout -b feature/big-change
# 1000 lines changed...
git push  # CONFLICT!

# Use stacked PRs:
git checkout -b feature/part-1
# 200 lines changed
git push  # Clean

git checkout -b feature/part-2
# 200 lines changed
git push  # Clean
Enter fullscreen mode Exit fullscreen mode

Each PR is small enough to merge cleanly.

Solution 5: Trunk-Based Development

The ultimate conflict prevention strategy: everyone commits to main (or a shared branch) multiple times per day.

This requires:

  • Comprehensive tests to catch regressions
  • Feature flags to hide incomplete work
  • Small, focused changes

But it virtually eliminates merge conflicts.

The Bottom Line

Merge conflicts aren't a Git problem. They're a process problem. They happen because teams don't coordinate effectively, work on long-lived branches, and make large changes.

Fix the process, and the conflicts disappear.

What's your experience with merge conflicts? Do you have other strategies for preventing them?

Top comments (1)

Collapse
 
xxxn3m3s1sxxx profile image
xxxn3m3s1sxxx

Trunk-based development is underrated. We switched to it last year and merge conflicts dropped by 80%. The key was getting comprehensive test coverage first.