KhueApps
Home/DevOps/How to Fix 'CONFLICT (content)' Merge Conflicts in Git

How to Fix 'CONFLICT (content)' Merge Conflicts in Git

Last updated: October 07, 2025

Overview

The error 'CONFLICT (content): Merge conflict in <file>' appears when Git cannot automatically merge changes. It inserts conflict markers into the file, and the merge or rebase pauses until you resolve, stage, and continue.

This guide shows fast, reliable ways to resolve conflicts for day-to-day DevOps work and CI/CD pipelines.

Quickstart

  1. Identify conflicted files:
    • git status or git diff --name-only --diff-filter=U
  2. Open each conflicted file and resolve content between the markers:
    • <<<<<<< HEAD, =======, >>>>>>> branch
  3. Stage resolved files: git add <file>
  4. Continue:
    • If merging: git commit (or Git creates the merge commit automatically)
    • If rebasing: git rebase --continue
  5. Verify: run tests and git status should be clean.

Minimal working example

The commands below create a conflict, then resolve it.

# 1) Setup a repo with two branches that will conflict
mkdir demo-conflict && cd demo-conflict
git init -b main
printf 'line1\nline2\n' > app.txt
git add app.txt && git commit -m 'init app.txt'

git checkout -b feature
printf 'line1\nfeature change\n' > app.txt
git commit -am 'feature: modify line2'

git checkout main
printf 'line1\nmain change\n' > app.txt
git commit -am 'main: modify line2'

# 2) Merge feature into main (produces a conflict)
git merge feature || true
# You will see: 'CONFLICT (content): Merge conflict in app.txt'

# 3) Inspect and resolve
sed -n '1,200p' app.txt
# The file now contains conflict markers. Edit app.txt to the final content you want.
# For example, keep both:
# line1
# main change and feature change

# 4) Stage and finish
git add app.txt
git commit -m 'Merge feature into main: resolve conflict'

Step-by-step resolution

  1. Detect conflicts
    • git status shows files in both added by us and both modified states.
    • git diff --name-only --diff-filter=U lists unresolved files.
  2. Choose a resolution approach per file
    • Edit manually to combine changes.
    • Pick one side entirely (ours/theirs).
    • Use a merge tool for 3-way merge.
  3. Apply and stage
    • After editing, ensure no conflict markers remain. Stage with git add <file>.
  4. Complete the operation
    • Merge: git commit to produce a merge commit.
    • Rebase: git rebase --continue to advance to the next commit.
  5. Abort if needed
    • Merge: git merge --abort
    • Rebase: git rebase --abort

Understanding conflict markers

Inside conflicted files you will see something like:

<<<<<<< HEAD
main change
=======
feature change
>>>>>>> feature
  • The HEAD block is your current branch content.
  • The bottom block is the incoming branch content.
  • Edit to the final version you want, then remove the markers.

Choosing one side quickly (ours/theirs)

Use these commands to take an entire file from one side:

# During a merge (from branch A into B while currently on B):
# Take current branch (ours):
git checkout --ours -- path/to/file   # or: git restore --source=HEAD -- path/to/file
# Take incoming branch (theirs):
git checkout --theirs -- path/to/file # or: git restore --source=MERGE_HEAD -- path/to/file

git add path/to/file

During an interactive rebase, the meaning of ours/theirs flips for the commit being replayed. When in doubt, inspect with git show :1:path :2:path :3:path or open a merge tool.

Using a merge tool

If manual edits are error-prone, configure and run a merge tool:

git mergetool  # launches your configured GUI/CLI tool
# After resolving in the tool:
git add <file>
# Finish with commit or rebase --continue

Keep both changes

Sometimes you want both lines (e.g., changelogs).

# Manually edit to combine the blocks, then:
git add <file>

Alternatively, for specific files (like CHANGELOG.md), you can set a union merge driver:

echo '*.md merge=union' >> .gitattributes
git config merge.union.driver true

Special cases

  • Binary files: Git cannot auto-merge. Choose one side: git checkout --ours/--theirs <binary> then git add.
  • Renames: Use git status and git diff to confirm intent; you may need to manually move and merge content.
  • Whitespace-only conflicts: Use git merge -X ignore-space-change if appropriate; review carefully.

Make future merges easier

  • Enable rerere to remember your resolutions:
git config --global rerere.enabled true
  • Keep commits focused and small; rebases/merges are simpler.
  • Update often: git fetch and rebase/merge main frequently to reduce drift.
  • Add .gitattributes for binary, generated, or lock files to avoid noisy merges.

Pitfalls to avoid

  • Leaving markers in files: CI often fails if '<<<<<<<' remains. Always search before committing.
  • Blindly using -X theirs or -X ours: Can hide data loss; use sparingly and review diffs.
  • Forgetting to stage: Resolving in the editor is not enough; run git add <file>.
  • Rebase confusion: Ours/theirs semantics differ during rebase. Inspect with a mergetool if unsure.
  • Line endings: Mixed CRLF/LF can cause conflicts. Standardize via .gitattributes (e.g., text=auto) and consistent editor settings.
  • Generated artifacts: Do not resolve by editing compiled or generated files; regenerate after merging sources.

Performance notes

  • Large repos: Use sparse-checkout or partial clone to reduce working set when resolving conflicts.
  • Mergetool speed: Configure a fast local tool; avoid remote file systems if possible to reduce I/O latency.
  • Minimize re-runs: Stage and commit in logical batches; rerere prevents repeating identical resolutions.
  • Avoid massive single-file diffs: Consider splitting long files to reduce future conflict scope.

Example: resolving during rebase

# Rebase feature onto main
git checkout feature
git fetch origin
git rebase origin/main || true
# Resolve conflicts as usual
$EDITOR app.txt
git add app.txt
git rebase --continue
# If it goes wrong:
# git rebase --abort

Verification checklist

  • git status shows no unmerged paths.
  • Build and tests pass locally.
  • git log shows the intended merge or rebased commits.
  • CI pipeline is green.

Tiny FAQ

  • Q: How do I list just conflicted files? A: git diff --name-only --diff-filter=U
  • Q: How do I take all files from the incoming branch? A: For each path: git checkout --theirs -- <path>; then git add .
  • Q: Can I resume later? A: Yes. Your worktree keeps conflict state. Commit or abort before switching branches.
  • Q: How do I prevent repeats? A: Enable rerere and rebase/merge main frequently.

Series: Git

DevOps