TracksPractical Coding FoundationsVersion ControlResolving Merge Conflicts(10 of 12)

Resolving Merge Conflicts

Merge conflicts happen when two branches change the same lines in the same file. Git can't decide which version to keep, so it asks you to resolve the conflict manually. This sounds intimidating, but conflicts are a normal part of development — and they're straightforward to fix once you understand the process.

Why Conflicts Occur

Imagine you're on main and change line 10 of app.py to say "Hello". Meanwhile, someone (or you on another branch) changes the same line 10 to say "Welcome". When you try to merge, Git sees two different changes to the same line and doesn't know which one you want.

Recognizing a Conflict

When a merge has conflicts, Git tells you:

Running git status shows which files have conflicts:

Reading Conflict Markers

Open the conflicted file and look for markers that Git inserted:

def greet():
<<<<<<< HEAD
    return "Hello"
=======
    return "Welcome"
>>>>>>> feature-branch

Here's what these markers mean:

  • <<<<<<< HEAD — start of your current branch's version
  • ======= — divider between the two versions
  • >>>>>>> feature-branch — end of the incoming branch's version

Everything between <<<<<<< HEAD and ======= is what's on your current branch. Everything between ======= and >>>>>>> is what's coming from the branch you're merging.

Resolving the Conflict

You have several options:

Keep your version: Delete the incoming changes and the markers.

Keep their version: Delete your changes and the markers.

Combine both: Write new code that incorporates both changes.

Write something new: Replace everything with a better solution.

After deciding, remove all the conflict markers. The file should look like normal code:

def greet():
    return "Welcome"

Completing the Merge

Once you've resolved all conflicts in all files:

Stage the resolved files with git add, then commit to complete the merge.

Getting Help From AI

When conflicts are confusing, ask an AI to explain:

"I have a merge conflict. Here's what Git shows: [paste the conflict]. Help me understand what each version does and how to resolve it."

AI can explain what both versions are trying to do and suggest how to combine them.

See More

Further Reading

You need to be signed in to leave a comment and join the discussion