Skip to main content

Merge Conflict

Updated 2 min read

Share this page

Send the link, quote the definition with a link back, or show it as a card on your own site.

https://softwaredictionary.org/terms/merge-conflict

In short

A merge conflict happens when Git can't automatically combine two branches because both changed the same lines of a file, so a person must decide the result.

What is a merge conflict in Git?

Git is very good at combining work automatically, as long as different people changed different parts of the code. A merge conflict occurs when two branches have changed the same lines of the same file in different ways, or when one branch edited a file that the other deleted. Git can't know which version is correct, so it pauses the merge and asks you to decide.

When a conflict happens, Git marks the affected sections directly in the file. The lines between <<<<<<< and ======= show your current branch's version, and the lines between ======= and >>>>>>> show the incoming branch's version. To resolve the conflict, you edit the file to the correct final content, delete the markers, stage the file with git add, and complete the merge with git commit. Conflicts can also happen during a rebase, a cherry-pick, or a git pull.

A merge conflict is like two editors revising the same sentence in a shared document in different ways: someone has to read both versions and decide on the final wording. Most code editors show conflicts with buttons to accept one side, the other, or both. You can always back out and return to the state before the merge with git merge --abort.

A merge conflict is not an error or a sign that something broke; it is Git asking for a human decision. Conflicts are also different from logic bugs: Git can cleanly merge two changes that still break the program when combined, which is why tests should run after every merge. Keeping branches short-lived and syncing with the main branch often keeps conflicts small and easy to resolve.

Key takeaways

  • A merge conflict occurs when two branches change the same lines, or one edits a file the other deletes.
  • Git marks conflicts in the file with <<<<<<<, =======, and >>>>>>> markers.
  • Resolve by editing the file, removing the markers, running git add, then committing.
  • git merge --abort cancels the merge and restores the previous state.
  • Small, frequently merged branches cause fewer and simpler conflicts.

Example

Resolving a merge conflictbash
git merge feature/new-header
# CONFLICT (content): Merge conflict in src/header.html

git status            # lists files with unresolved conflicts

# Inside src/header.html, Git has inserted both versions:
# <<<<<<< HEAD
# <h1>Welcome back</h1>
# =======
# <h1>Hello again</h1>
# >>>>>>> feature/new-header

# Edit the file to the final version, remove the markers, then:
git add src/header.html
git commit            # completes the merge

Readers ask

How do I resolve a merge conflict in Git?

Open each conflicted file, choose or combine the versions between the conflict markers, and delete the markers. Then stage the files with git add and run git commit to finish the merge.

How do I cancel a merge that has conflicts?

Run git merge --abort to stop the merge and return your branch to the state it was in before the merge started. During a rebase, the equivalent is git rebase --abort.

How can I avoid merge conflicts?

You can't avoid them entirely, but you can make them rare and small. Keep branches short-lived, pull the latest changes from the main branch often, and avoid mixing large formatting changes with feature work.

See also

Spotted a mistake or something missing on this page?Suggest an edit

Read a random page
Open today's review
Switch to the dark theme
Read this page in Türkçe

More

Settings