When working with Git, especially in a team, we will eventually see a message that many beginners find scary:
CONFLICT (content): Merge conflict in file.txt
Automatic merge failed; fix conflicts and then commit the result.
Don’t worry — merge conflicts are normal!
They simply mean Git needs our help to decide which changes to keep.
In this post, we’ll learn:
- What merge conflicts are
- Why they happen
- How to fix them step by step
- Tips to avoid conflicts in the future
1. What Is a Merge Conflict?
A merge conflict happens when Git cannot automatically combine changes from two branches.
This usually occurs when:
- Two people edited the same part of the same file, or
- We edited a file differently on two branches, and we try to merge them.
Git doesn’t know which version we want — so it asks us to choose.
2. What Merge Conflicts Look Like
When a conflict happens, Git marks the file with conflict boundaries like this:
<<<<<<< HEAD
Your changes in the current branch
=======
Changes from the branch you're merging
>>>>>>> other-branch
HEAD
Represents our current branch.
other-branch
Shows the incoming changes we are trying to merge.
Our job is to edit the file and choose:
- our version,
- the other branch’s version,
- or a combination of both.
3. How to Resolve a Merge Conflict (Step-by-Step)
Step 1: Identify the conflicting files
Run:
git status
You will see something like:
both modified: app.js
Step 2: Open the conflicting file
Look for conflict markers:
<<<<<<< HEAD
Current branch changes
=======
Incoming branch changes
>>>>>>> feature-branch
Step 3: Choose what the final version should look like
For example, say this is the conflict:
<<<<<<< HEAD
console.log("Hello from main");
=======
console.log("Hello from feature branch");
>>>>>>> feature-branch
We can choose either one:
console.log("Hello from main");
OR
console.log("Hello from feature branch");
OR combine both:
console.log("Hello from main");
console.log("Hello from feature branch");
Step 4: Remove all conflict markers
Delete:
<<<<<<< HEAD=======>>>>>>> feature-branch
The file should look clean after editing.
Step 5: Mark the conflict as resolved
Add the resolved file:
git add app.js
Step 6: Complete the merge
git commit
Git will auto-generate a merge commit message.
We can keep it or update it.
4. Using Tools to Resolve Conflicts (Easier Way)
Most IDEs and editors offer a visual way to fix conflicts:
- VS Code
- IntelliJ / WebStorm
- GitHub Desktop
- Sourcetree
In VS Code, for example, we’ll see buttons like:
- Accept Current Change
- Accept Incoming Change
- Accept Both Changes
- Compare Changes
This makes resolution much easier—no need to manually remove markers.
5. Common Reasons Merge Conflicts Happen
- Multiple people editing the same file.
- Renaming or deleting files on one branch.
- Reformatting code while another developer makes changes.
- Long-lived branches getting out of sync with
main.
6. Tips to Avoid Merge Conflicts
Here are simple practices that reduce conflict frequency:
1. Pull often
git pull
2. Commit small, frequent changes
Smaller changes = fewer conflicts.
3. Communicate with team
If two people edit the same file, talk before merging.
4. Keep branches short-lived
To avoid merge conflicts, try to keep your feature or task branches open for no more than a few days.
Recommended practice:
- Small fixes → same day
- Normal features → 1–5 days
- Larger work → break into smaller parts and merge weekly
Long-running branches (10–14+ days) drift away from the main codebase, making merges harder and conflict-prone. Short-lived branches keep our work fresh, easier to review, and easier to merge.
5. Avoid formatting changes mixed with code changes
Run formatters in separate commits.
7. Summary
| Task | Solution |
|---|---|
| See conflicting files | git status |
| Edit the file to resolve conflict | Manually or via editor |
| Add the resolved file | git add <file> |
| Finish the merge | git commit |
Final Thoughts
Merge conflicts are a normal part of collaborating with Git.
Once you understand how the conflict markers work and how to choose the right changes, resolving conflicts becomes simple.