We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Merge Conflicts

A merge conflict occurs when two commits modify the same line and Git can't automatically decide which change to keep and which change to discard.

Consider the following commit history:

    C     feature
  /
A - B     main

The main branch has a file with these lines:

package main

func isNice(num int) bool {
    return num == 69 // commit B changed this line
}

While feature has:

package main

func isNice(num int) bool {
    return num == 420 // commit C changed this line
}

If we merge feature into main, Git will detect that the return line was changed in both branches independently: which creates a conflict.

When a conflict happens (usually as the result of a merge or rebase) Git will prompt you to manually decide which change to keep. It's okay when the same line is modified in one commit, and then again in a later commit. The problem arises when the same line is modified in two commits that aren't in a parent-child relationship.