01 / 20

Resolving
Merge Conflicts

Learn how Git detects conflicting edits from different branches, how to read conflict markers, and how to resolve them so your C++ projects compile cleanly.

C++ Git & Version Control

What Is a Merge Conflict?

When two branches modify the same lines in the same file, Git cannot automatically decide which change to keep. It pauses the merge and marks the file with conflict markers so you can choose the correct version.

Why Conflicts Happen

main commit A feature edits line 10 main also edits line 10 ! CONFLICT Both branches changed the same line Git cannot auto-merge: it marks the file for manual resolution

The Three Versions in a Conflict

Ours (HEAD)
The version on your current branch: the code you wrote before merging.
Theirs (incoming)
The version from the branch being merged in: someone else's changes.
Base (ancestor)
The common ancestor commit where both branches diverged.

Anatomy of Conflict Markers

// utils.cpp
int compute(int x) {
<<<<<<< HEAD
    return x * 2;
=======
    return x * 3;
>>>>>>> feature-branch
}
<<<<<<<
Start of conflict: everything below this until ======= is your branch.
=======
Divider between ours (above) and theirs (below).
>>>>>>>
End of conflict: names the incoming branch.

Conflict Merge vs. Fast-Forward

Conflict Merge Fast-Forward
Same lines edited? Yes No
Human intervention Required Not needed
Markers added? Yes No
Merge commit Always created Branch pointer moves forward
Risk level Medium: mistakes possible Low: automatic

How to Resolve: Step by Step

1
Run the merge. Execute git merge feature-branch. Git reports which files conflict.
2
Open the conflicted file. Look for <<<<<<<, =======, and >>>>>>> markers.
3
Decide which code to keep. Keep ours, theirs, or write a combination. Delete all three marker lines.
4
Test your code. Compile and run to verify the resolved file works correctly.
5
Stage and commit. Run git add utils.cpp then git commit to finalize the merge.

Example 1: Keep Ours

Your teammate changed the multiplier to 3, but you know the spec requires 2. Keep your version.

// Before: conflicted file
<<<<<<< HEAD
    return x * 2;
=======
    return x * 3;
>>>>>>> feature-branch
// After: resolved (kept ours)
    return x * 2;

Example 2: Keep Theirs

After reading the updated requirements, you realize your teammate's x * 3 version is correct. Accept their change.

// Before: conflicted file
<<<<<<< HEAD
    return x * 2;
=======
    return x * 3;
>>>>>>> feature-branch
// After: resolved (kept theirs)
    return x * 3;

Example 3: Combine Both Changes

Sometimes both changes are needed. One branch added a validation check; the other added a logging statement. Merge them together.

void process(int val) {
<<<<<<< HEAD
    if (val < 0) throw std::invalid_argument("negative");
=======
    std::cout << "Processing: " << val << std::endl;
>>>>>>> feature-logging
    // rest of function...
}
// After: resolved (combined both)
void process(int val) {
    if (val < 0) throw std::invalid_argument("negative");
    std::cout << "Processing: " << val << std::endl;
    // rest of function...
}

Example 4: Header File Conflict

Two developers added different #include directives to the same location in a header. Both are needed.

#ifndef UTILS_H
#define UTILS_H
<<<<<<< HEAD
#include <vector>
=======
#include <map>
>>>>>>> feature-cache
// ...
// After: resolved (keep both includes)
#ifndef UTILS_H
#define UTILS_H
#include <map>
#include <vector>
// ...

The Resolution Flow

git merge Conflict detected Open conflicted file Choose resolution ours / theirs / combine Keep ours Combine Keep theirs Delete markers git add + git commit

Key Git Commands

git status
Shows which files are in a conflicted state and need resolution.
git diff
Displays the conflict markers inline so you can see both versions.
git add
Marks a file as resolved after you edit out the markers.
git merge --abort
Cancels the merge entirely and returns to the pre-merge state.

Common Mistakes

✕ Leaving markers in code
Forgetting to delete <<<<<<<, =======, or >>>>>>> lines. The compiler treats them as syntax errors.
✕ Deleting both versions
Removing all conflicted code instead of choosing one side. This silently loses functionality.
✕ Not testing after resolution
Committing without compiling. The merge may introduce logic errors even if markers are gone.

Resolution Strategies Summary

Strategy When to Use Action
Keep Ours Your version is correct Delete theirs + all markers
Keep Theirs Incoming version is better Delete ours + all markers
Combine Both Both changes are needed Merge code logically, delete markers
Abort Merge Need more info or discussion git merge --abort

Question 1 of 5

What does the ======= line represent inside a merge conflict?

Question 2 of 5

Which resolution is correct?

Question 3 of 5

After editing a conflicted file to remove all markers and choose the correct code, what is the next step?

Question 4 of 5

When is it appropriate to combine both versions of conflicting code instead of choosing one side?

Question 5 of 5

What happens if you commit a file that still contains conflict markers like <<<<<<<?