[!WARNING] Don’t Panic: A merge conflict is not an error. It’s just Git saying, “I have two valid versions of this line. Which one do you want?”

The Anatomy of a Conflict

When Git can’t automatically merge code (usually because the same line was changed in both branches), it pauses and inserts Conflict Markers into your file.

It looks like this:

<<<<<<< HEAD
const API_URL = "https://prod.api.com";
=======
const API_URL = "https://dev.api.com";
>>>>>>> feature-login
  • <<<<<<< HEAD: The start of Your changes (what you have on your current branch).
  • =======: The divider between the two versions.
  • >>>>>>> feature-login: The end of Their changes (what is coming in from the other branch).

Interactive: The Conflict Surgeon

Practice resolving a conflict. Choose which version to keep.

File: config.js
<<<<<<< HEAD
const MAX_RETRIES = 5;
=======
const MAX_RETRIES = 10;
>>>>>>> feature-retry
  
Status: Unresolved Conflict

How to Resolve (Step-by-Step)

1. Identify the Conflict

Git tells you which file has issues.

$ git merge feature-login
Auto-merging config.js
CONFLICT (content): Merge conflict in config.js
Automatic merge failed; fix conflicts and then commit the result.

2. Open and Edit

Open config.js in your editor. You will see the markers. Delete the markers (<<<<, ====, >>>>) and the code you don’t want. Leave only the correct code.

Before:

<<<<<<< HEAD
const MAX_RETRIES = 5;
=======
const MAX_RETRIES = 10;
>>>>>>> feature-retry

After (Manual Edit):

const MAX_RETRIES = 10;

3. Mark as Resolved

Tell Git you’re done by adding the file.

git add config.js

4. Commit

Finish the merge.

git commit

(Git will provide a default message like “Merge branch ‘feature-login’”).

Tooling

Most modern editors (VS Code, IntelliJ) provide a “Click to Resolve” interface. They essentially do the “delete markers and keep selection” steps for you.

You can also configure a dedicated merge tool:

git config --global merge.tool vimdiff
git mergetool

Summary

  1. Don’t Panic. It’s normal.
  2. Read the Markers. HEAD is you, the other hash is them.
  3. Edit the File. Remove the markers, keep the code.
  4. Add & Commit.