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.
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
The Three Versions in a Conflict
Anatomy of Conflict Markers
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
git merge feature-branch. Git reports which files conflict.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.
Example 2: Keep Theirs
After reading the updated requirements, you realize your teammate's x * 3 version is correct. Accept their change.
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.
Example 4: Header File Conflict
Two developers added different #include directives to the same location in a header. Both are needed.
The Resolution Flow
Key Git Commands
Common Mistakes
<<<<<<<, =======, or >>>>>>> lines. The compiler treats them as syntax errors.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?
======= line separates the HEAD (ours) section above from the incoming (theirs) section below. It is one of the three markers you must delete.Question 2 of 5
You run git merge feature-logging and get this conflict in main.cpp:
The spec says the default argument should be 10.
Which resolution is correct?
compute(10)), delete the HEAD line, and remove all three marker lines before staging.Question 3 of 5
After editing a conflicted file to remove all markers and choose the correct code, what is the next step?
git add before committing. Git needs to know you've finished resolving that file. Committing without staging leaves the merge incomplete.Question 4 of 5
When is it appropriate to combine both versions of conflicting code instead of choosing one side?
#include directives or a validation check and a log statement). You keep both and arrange them in logical order.Question 5 of 5
What happens if you commit a file that still contains conflict markers like <<<<<<<?
<<<<<<< and ======= are not valid C++, so the compiler will fail. Always compile and test before committing a resolved file.