Resolving Merge Conflicts

A conflict happens when two branches change the same lines; you edit the file to pick the right result.

A merge conflict occurs when two branches changed the same part of a file and Git cannot decide which version to keep. Git pauses the merge and marks the file with conflict markers.

Conflict markers

  • <<<<<<< HEAD — your current branch's version.
  • ======= — the divider.
  • >>>>>>> branch — the incoming version.

Steps to resolve

  1. Open each conflicted file and edit it to the final desired result.
  2. Delete the conflict markers.
  3. git add the file to mark it resolved.
  4. git commit to finish the merge.

Example

Example · bash
git merge login-page
# CONFLICT (content): Merge conflict in index.html

# Edit index.html to keep the correct lines, remove the markers

git add index.html
git commit    # completes the merge

When to use it

  • Two developers both modify the same function signature on different branches; Git flags the conflict so neither change is silently lost.
  • A developer rebasing onto an updated main resolves conflicts file-by-file before the rebase completes.
  • A team uses a visual merge tool to resolve a complex conflict in a shared configuration file during a sprint merge.

More examples

Trigger and inspect a conflict

Git stops the merge and marks conflicting files; git status identifies every file that needs manual resolution.

Example · bash
git merge feature/redesign
# CONFLICT (content): Merge conflict in src/index.html
# Automatic merge failed; fix conflicts and commit.

git status
# both modified: src/index.html

Resolve conflict markers manually

Shows the three-way conflict marker format and the steps to accept one side, stage, and complete the merge.

Example · bash
# Inside src/index.html Git inserts conflict markers:
<<<<<<< HEAD
<h1>Welcome to MyApp</h1>
=======
<h1>Welcome to MyApp 2.0</h1>
>>>>>>> feature/redesign

# Edit to keep the desired version, then:
git add src/index.html
git commit -m "Merge feature/redesign, keep 2.0 heading"

Abort a merge in progress

merge --abort cancels the entire operation and resets the repo to exactly the state it was in before git merge ran.

Example · bash
# The conflict is too complex to resolve right now
git merge --abort

# Working tree and index are restored to pre-merge state
git status
# On branch main
# nothing to commit, working tree clean

Discussion

  • Be the first to comment on this lesson.