Imagine you and your friend are working on the same Java project.
Both of you edit the same file:
Login.java
You change the login message to:
System.out.println("Welcome User");
At the same time, your friend changes the same line to:
System.out.println("Welcome Student");
Now both of you try to merge your work into the main branch.
Git faces a problem.
It sees two different changes to the same part of the same file and cannot determine which version should be kept.
Instead of making a wrong decision, Git stops the merge and asks you to resolve the conflict manually.
This situation is called a Merge Conflict.
Although merge conflicts may seem intimidating at first, they are a normal part of collaborative software development. Every professional developer encounters them, and Git provides clear tools to resolve them safely.
In this article, you will learn:
What a merge conflict is
Why conflicts happen
When conflicts occur
How Git displays conflicts
How to resolve conflicts step by step
Useful commands during conflict resolution
How to avoid conflicts
Best practices
By the end of this article, you'll be able to resolve merge conflicts with confidence.
What is a Merge Conflict?
A merge conflict occurs when Git cannot automatically combine changes from two branches.
Git can merge different changes automatically.
However, if the same part of the same file has been modified differently, Git needs human input to decide which version is correct.
Why Do Merge Conflicts Occur?
Suppose the original code is:
System.out.println("Welcome");
Developer A changes it to:
System.out.println("Welcome User");
Developer B changes it to:
System.out.println("Welcome Student");
When Git tries to merge these branches, both versions cannot exist on the same line.
Git therefore pauses the merge and requests your decision.
Common Situations That Cause Conflicts
Merge conflicts commonly occur when:
Two developers edit the same line of a file.
One developer deletes a file while another modifies it.
A file is renamed in one branch and modified in another.
The same block of code is edited differently in multiple branches.
Large feature branches remain unmerged for a long time.
Situations That Usually Do NOT Cause Conflicts
Suppose:
Developer A edits:
login()
Developer B edits:
payment()
These are different parts of the file.
Git can usually merge these changes automatically without any conflict.
A Practical Example
Suppose your repository contains:
public class Login
{
public static void main(String args[])
{
System.out.println("Welcome");
}
}
Create a branch:
git switch -c feature-login
Modify the line:
System.out.println("Welcome User");
Commit the change.
Now switch back:
git switch main
Modify the same line differently:
System.out.println("Welcome Student");
Commit again.
Now merge:
git merge feature-login
Git displays something similar to:
Auto-merging Login.java
CONFLICT (content): Merge conflict in Login.java
Automatic merge failed; fix conflicts and then commit the result.
The merge pauses until the conflict is resolved.
What Happens Internally?
Git compares:
Common Ancestor
│
├──────── main
│
└──────── feature-login
It tries to combine both branches.
When two incompatible changes affect the same section, Git cannot choose automatically.
Conflict Markers
Git inserts special markers into the affected file.
Example:
<<<<<<< HEAD
System.out.println("Welcome Student");
=======
System.out.println("Welcome User");
>>>>>>> feature-login
These markers are temporary and help you identify the conflicting changes.
Understanding the Conflict Markers
<<<<<<< HEAD
This marks the beginning of the changes from the branch you are currently on.
Example:
<<<<<<< HEAD
If you are on main, this section shows the main version.
=======
This separates the two versions.
Everything above it belongs to the current branch.
Everything below it belongs to the branch being merged.
>>>>>>> feature-login
This marks the end of the conflicting section and identifies the incoming branch.
How to Resolve the Conflict
Read both versions carefully.
Decide which one should remain.
Possible choices:
Option 1
Keep the current branch:
System.out.println("Welcome Student");
Option 2
Keep the incoming branch:
System.out.println("Welcome User");
Option 3
Combine both ideas:
System.out.println("Welcome User");
System.out.println("Welcome Student");
or perhaps:
System.out.println("Welcome User and Student");
Choose the version that best fits your application.
Remove the Conflict Markers
After deciding, remove:
<<<<<<< HEAD
=======
>>>>>>> feature-login
The file should contain only valid source code.
For example:
public class Login
{
public static void main(String args[])
{
System.out.println("Welcome User");
}
}
Completing the Merge
After editing the file:
Stage it:
git add Login.java
Complete the merge:
git commit
Git creates the merge commit.
The conflict has now been resolved.
Checking the Repository
Run:
git status
If everything has been resolved successfully, Git reports a clean working tree after the merge commit.
Viewing the Merge History
Use:
git log --oneline --graph
Example:
* Merge branch 'feature-login'
|\
| * Update login message
* | Update welcome text
|/
* Initial commit
The graph shows both branches joining into a merge commit.
What if You Want to Cancel the Merge?
Suppose you start a merge but decide you are not ready.
Before completing the merge, you can abort it:
git merge --abort
Git attempts to return your repository to the state it was in before the merge started.
This command works only if the merge has not yet been completed.
Merge Conflict vs Syntax Error
These are different problems.
| Merge Conflict | Syntax Error |
|---|---|
| Caused by incompatible changes between branches | Caused by invalid program code |
| Git detects it during a merge | Compiler or interpreter detects it while building or running the program |
| Resolved by editing conflicting sections | Resolved by fixing the program logic or syntax |
Real-World Example
Suppose four developers are working on an E-Commerce website.
Developer A:
feature-login
Developer B:
feature-payment
Developer C:
feature-cart
Developer D:
feature-orders
Developer A and Developer B both modify the same authentication method.
When the branches are merged, Git reports a conflict.
The team discusses the correct implementation, edits the file, stages it, and completes the merge.
This collaborative decision helps ensure the final code is correct.
How to Reduce Merge Conflicts
Although conflicts cannot always be avoided, you can reduce them by following good practices.
Pull the latest changes frequently.
Merge or rebase feature branches regularly.
Keep feature branches small and focused.
Avoid making unrelated changes in the same branch.
Communicate with teammates before modifying shared code.
Review code before merging.
Common Beginner Mistakes
Mistake 1
Deleting the conflict markers incorrectly and leaving invalid code behind.
Always verify that the file still compiles.
Mistake 2
Choosing one version without reading the other.
Review both changes carefully before deciding.
Mistake 3
Forgetting to stage the resolved file.
Remember:
git add <filename>
is required after resolving the conflict.
Mistake 4
Thinking Git is broken.
Merge conflicts simply mean Git needs human guidance.
They are a normal part of collaborative development.
Best Practices
Commit your work before starting a merge.
Read both conflicting versions carefully.
Test the application after resolving conflicts.
Use meaningful commit messages.
Keep branches short-lived when possible.
Communicate with teammates about shared code.
Useful Commands During Conflict Resolution
| Command | Purpose |
|---|---|
git status | Show conflicted files |
git merge feature-name | Start the merge |
git add <file> | Mark a conflict as resolved |
git commit | Complete the merge |
git merge --abort | Cancel an unfinished merge |
git log --oneline --graph | View merge history |
Summary
Merge conflicts occur when Git cannot safely combine changes from different branches automatically. Rather than making an incorrect decision, Git pauses the merge and asks the developer to resolve the conflicting sections.
By understanding conflict markers, reviewing both versions carefully, and completing the merge with git add and git commit, you can confidently resolve conflicts while preserving the correct functionality of your project.
Merge conflicts are not errors in Git—they are an expected part of collaborative software development.
Comments
Post a Comment