Resolving Merge
Conflicts
Learn to identify, understand, and resolve merge conflicts that arise when Git cannot automatically combine changes from different branches.
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?
How Branches Diverge
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
<<<<<<< HEAD
String msg = "Hello World";
=======
String msg = "Hi World";
>>>>>>> feature-branch
The Three Sections
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
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.
<<<<<<< HEAD
String msg = "Hello World";
=======
String msg = "Hi World";
>>>>>>> feature-branch
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.
<<<<<<< HEAD
public void greet() {
System.out.println("Hello!");
}
=======
public void farewell() {
System.out.println("Goodbye!");
}
>>>>>>> feature-branch
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.
<<<<<<< HEAD
int maxRetries = 3;
=======
int maxRetries = 10;
>>>>>>> feature-branch
int maxRetries = 5; // compromise agreed by team
The Complete Workflow
Reading git status During a Conflict
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.
$ git merge --abort
Common Mistakes to Avoid
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
You run git merge feature-login and see this in App.java:
int timeout = 30;
=======
int timeout = 60;
>>>>>>> feature-login
You decide to keep the feature-login value.
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?