Git Merge Conflicts

A Git merge conflict happens when two branches change the same lines of the same file, or one branch edits a file the other deleted. Git cannot know which version you want, so it pauses the merge and asks you to decide. Conflicts are normal and not dangerous once you know the steps.

Creating a Conflict

git switch main
git merge feature/header
Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Reading Conflict Markers

Git writes both versions into the file, separated by markers.

  • Between <<<<<<< HEAD and ======= is your current branch's version.
  • Between ======= and >>>>>>> is the incoming branch's version.
  • Everything outside the markers merged cleanly.
<header>
<<<<<<< HEAD
  <h1>Naeem Amin — Developer</h1>
=======
  <h1>Naeem Amin | Full-Stack Engineer</h1>
>>>>>>> feature/header
</header>
(the file stays in this state until you edit it)

Resolving the Conflict

Edit the file to the final content you want — keep one side, the other, or combine them — and delete all the markers. Then stage the file and commit.

# after editing index.html
git status
git add index.html
git commit
Unmerged paths:
  (use "git add <file>..." to mark resolution)
        both modified:   index.html

[main 2b3c4d5] Merge branch 'feature/header'

VS Code and other editors highlight conflicts and offer buttons such as Accept Current, Accept Incoming and Accept Both, plus a three-way merge editor. They simply edit the file for you; you still stage and commit.

Taking One Side for a Whole File

git checkout --ours package-lock.json     # keep current branch version
git checkout --theirs logo.svg            # keep incoming version
git add package-lock.json logo.svg
Updated 1 path from the index

Aborting a Merge

If things get confusing, go back to exactly how you were before the merge started and try again.

git merge --abort
(working directory restored to pre-merge state)

Avoiding Conflicts

  • Pull or merge main into your branch often, so differences stay small.
  • Keep branches short-lived and pull requests small.
  • Agree on a code formatter (Prettier, ESLint) so whitespace does not cause conflicts.
  • Communicate when several people need to edit the same file.

Frequently asked questions

How do I resolve a merge conflict in Git?

Open each conflicted file, choose the final content between the <<<<<<<, ======= and >>>>>>> markers, delete the markers, then git add the files and git commit.

How do I cancel a merge with conflicts?

Run git merge --abort to return your branch to the state before the merge began.

What do ours and theirs mean in Git?

During a merge, "ours" is the branch you are on and "theirs" is the branch being merged in. During a rebase the meanings are swapped, which often surprises people.