01 / 21

Resolving Merge
Conflicts

Learn to identify, understand, and resolve merge conflicts that arise when Git cannot automatically combine changes from different branches.

Computer Science Git

What Is a Merge Conflict?

A merge conflict occurs when Git tries to combine changes from two branches but finds that both branches modified the same lines of the same file. Git cannot decide which version to keep, so it pauses and asks you to resolve it manually.

When Do Conflicts Happen?

Same Line Edits
Two branches change the exact same line of code in a file.
Adjacent Edits
Edits are so close together that Git cannot safely combine them.
Delete vs. Edit
One branch deletes a line that another branch has modified.

How Branches Diverge

commit A commit B branch point feature branch main branch CONFLICT! edits line 5 edits line 5

When two branches independently modify the same line, Git cannot automatically merge. It marks the file as conflicted and requires manual intervention.

Anatomy of Conflict Markers

// Greeting.java
<<<<<<< HEAD
    String msg = "Hello World";
=======
    String msg = "Hi World";
>>>>>>> feature-branch
<<<<<<< HEAD
Marks the start of the conflict. Everything below until the separator is your current branch.
=======
The divider between the two conflicting versions.
>>>>>>>
Marks the end of the conflict. Content above is from the incoming branch.

The Three Sections

<<<<<<< HEAD Your changes (current branch) ======= >>>>>>> feature-branch Incoming changes (other branch) Resolved: your chosen version

Merge vs. Rebase Conflicts

git merge git rebase
When it happens Combining two branch tips Replaying commits onto a new base
Conflict markers Same format: <<<, ===, >>> Same format: <<<, ===, >>>
HEAD means Your current branch The branch you are rebasing onto
After resolving git add + git commit git add + git rebase --continue
History shape Creates a merge commit Linear history (no merge commit)

How to Resolve Step by Step

1
Run the merge. Execute git merge feature-branch. Git reports which files have conflicts.
2
Open the conflicted file. Look for the <<<<<<<, =======, and >>>>>>> markers.
3
Decide which code to keep. Keep your version, theirs, or write a new combined version.
4
Remove all conflict markers. Delete every <<<, ===, and >>> line completely.
5
Stage and commit. Run git add Greeting.java then git commit to finalize the merge.

Example 1: Keep One Side

Scenario: your main branch has "Hello World" and your feature branch has "Hi World". You decide to keep the feature branch version.

// Before: conflicted file
<<<<<<< HEAD
    String msg = "Hello World";
=======
    String msg = "Hi World";
>>>>>>> feature-branch
// After: resolved file
    String msg = "Hi World";

Example 2: Combine Both Versions

Scenario: main added a greet() method and feature added a farewell() method at the same location. You need both.

// Before: conflicted file
<<<<<<< HEAD
    public void greet() {
        System.out.println("Hello!");
    }
=======
    public void farewell() {
        System.out.println("Goodbye!");
    }
>>>>>>> feature-branch
// After: resolved with both methods
    public void greet() {
        System.out.println("Hello!");
    }

    public void farewell() {
        System.out.println("Goodbye!");
    }

Example 3: Write a New Version

Sometimes neither version is correct on its own. You can write entirely new code that incorporates the intent of both changes.

// Before: conflicted file
<<<<<<< HEAD
    int maxRetries = 3;
=======
    int maxRetries = 10;
>>>>>>> feature-branch
// After: a new compromise
    int maxRetries = 5; // compromise agreed by team

The Complete Workflow

git merge CONFLICT reported Open file find markers Edit code choose version Remove markers <<< === >>> Test code compile + run git add . git commit ✓ Merge complete!

Reading git status During a Conflict

$ git status
On branch main
You have unmerged paths.
  (fix conflicts and run "git commit")

Unmerged paths:
  both modified:   Greeting.java

When to Abort a Merge

If a merge conflict is too complex or you need more time, you can safely undo the entire merge attempt and return to your pre-merge state.

// Abort the merge and go back to the state before
$ git merge --abort
When to abort
You merged the wrong branch, you need to discuss with a teammate first, or the conflict is too large to resolve right now.
What it does
Reverts all files to their state before the merge started. No work is lost on either branch.

Common Mistakes to Avoid

✕ Leaving Markers In
Committing the file with <<<<<<<, =======, or >>>>>>> still in it. The code will not compile.
✕ Deleting the Wrong Side
Accidentally keeping the old code and removing the new feature. Always read both sections before choosing.
✕ Forgetting git add
Editing the file but not staging it. Git still considers it unresolved until you run git add.

Quick Reference Summary

Situation Command / Action
Start a merge git merge branch-name
See conflicted files git status
Find conflict markers Search for <<<<<<< in the file
Resolve the conflict Edit the file, remove markers, keep desired code
Mark as resolved git add filename
Finish the merge git commit
Cancel everything git merge --abort

Question 1 of 5

What does the <<<<<<< HEAD marker indicate in a conflicted file?

Question 2 of 5

What should the resolved file look like?

Question 3 of 5

After editing a conflicted file to resolve the conflict, what is the next Git command you should run?

Question 4 of 5

Which of the following will not cause a merge conflict?

Question 5 of 5

What does git merge --abort do?