How to Resolve Merge Conflicts in Git

Merge conflicts in Git can feel scary at first. Your screen turns red. Strange markers appear. Git refuses to continue. It feels like everything is broken. But here is the good news. Merge conflicts are normal. They happen to everyone. Even experienced developers. Once you understand what is going on, they are not scary at all. In fact, they are just Git asking you a simple question: “Which version do you want?”

TLDR: Merge conflicts happen when Git cannot decide between two changes. You must open the conflicting file and choose what to keep. Remove the conflict markers, save the file, and commit the result. With a calm mindset and a simple process, merge conflicts become easy to fix.

What Is a Merge Conflict?

A merge conflict happens when Git cannot automatically combine changes from different branches.

This usually happens when:

  • Two people edit the same line of a file.
  • One person deletes a file that another person edits.
  • You edit something locally that was also changed in the remote branch.

Git tries to merge changes automatically. Most of the time, it succeeds. But when changes overlap in a way that Git cannot safely decide, it stops and asks for help.

That help comes from you.

What Does a Conflict Look Like?

When a conflict happens, Git modifies the file and adds special markers.

It looks something like this:

<<<<<<< HEAD
Your changes
=======
Incoming changes
>>>>>>> branch-name

Let us break this down.

  • <<<<<<< HEAD shows your current branch version.
  • ======= separates the two versions.
  • >>>>>>> branch-name shows the incoming branch version.

Your job is simple. Decide what the final version should look like.

Keep one side. Or combine both. Or rewrite everything. You are in control.

Step-by-Step: How to Resolve a Merge Conflict

Let us walk through the process in simple steps.

1. Try to Merge

You run:

git merge branch-name

If there is a conflict, Git will stop and show a message.

2. Check Which Files Have Conflicts

Run:

git status

Git will list files under “Unmerged paths.” These are your problem files.

3. Open the Conflicting File

Open it in your code editor.

You will see the conflict markers.

4. Decide What to Keep

This is the thinking step.

Ask yourself:

  • Which change is correct?
  • Do I need parts of both?
  • Has something become outdated?

Edit the file so it looks exactly the way you want. Remove all the conflict markers.

No markers must remain.

5. Stage the Fixed File

After editing, run:

git add filename

This tells Git the conflict is resolved.

6. Commit the Merge

Finally, run:

git commit

Git will often create a default merge message. Save it.

Done.

You survived your merge conflict.

Common Conflict Scenarios

Scenario 1: Same Line Edited

This is the most common case.

Two developers change the same line differently.

Solution: Choose the correct version. Or combine them carefully.

Scenario 2: File Deleted vs Modified

One branch deletes a file. The other edits it.

Git asks: should the file exist or not?

Solution: Decide if the file is still needed.

Scenario 3: Formatting vs Logic Changes

One person reformats code. Another changes logic.

This can create messy conflicts.

Solution: Carefully compare both. Keep functionality intact.

Using Visual Tools to Make It Easier

You do not have to resolve conflicts in plain text. Many tools make it visual and easier.

Here are some popular ones:

  • VS Code
  • GitKraken
  • Sourcetree
  • IntelliJ IDEA

Comparison Chart

Tool Beginner Friendly Visual Conflict Editor Free Version Best For
VS Code Yes Yes Yes Developers who love lightweight editors
GitKraken Very Excellent Limited Teams wanting strong visuals
Sourcetree Yes Good Yes Simple graphical interface users
IntelliJ IDEA Moderate Advanced Limited Professional Java and enterprise developers

These tools show side-by-side comparisons. You can click buttons like “Accept Current” or “Accept Incoming.” It feels much safer.

Tips to Avoid Merge Conflicts

You cannot eliminate conflicts completely. But you can reduce them.

  • Pull frequently. Stay updated with the main branch.
  • Make small commits. Easier to merge.
  • Communicate with your team. Avoid editing the same file at the same time.
  • Use feature branches. Keep work isolated.
  • Keep branches short-lived. Long branches mean more conflicts.

Think of it this way. The longer you stay away from the main branch, the more surprises wait for you.

Rebasing vs Merging Conflicts

Conflicts can also happen during a rebase.

The process is similar.

When a conflict appears during rebase:

  • Fix the file.
  • Run git add filename
  • Run git rebase –continue

If things go terribly wrong, you can stop with:

git rebase –abort

This brings you back to safety.

Staying Calm During Conflicts

Here is something important.

Merge conflicts do not mean you broke the project.

They mean Git needs help deciding.

Take a breath.

Read carefully.

Understand both changes.

If unsure, talk to the person who wrote the other code.

Collaboration solves most conflicts quickly.

A Simple Mental Model

Think of Git like a very careful editor.

If two writers submit different endings to the same paragraph, the editor stops.

The editor says, “Please confirm the final version.”

That is all a merge conflict is.

No drama. Just confirmation.

Practice Makes It Easy

The first merge conflict feels confusing.

The second feels manageable.

The third feels routine.

Soon, you will resolve them in minutes.

If you want to practice, create two branches. Edit the same line differently. Merge them. Resolve it.

Safe environment. No pressure.

Final Thoughts

Merge conflicts are part of teamwork. They appear when multiple people improve the same codebase. That is actually a good sign. It means progress is happening.

The key steps are simple:

  1. Identify the conflicting files.
  2. Edit and choose the correct code.
  3. Remove conflict markers.
  4. Stage the file.
  5. Commit the result.

That is it.

No mystery. No magic.

Just careful reading and clear decisions.

Once you understand the pattern, you will not fear the red text anymore. You will see it and think, “Okay. Time to choose.”

And then you will fix it. Calmly. Confidently. Like a Git pro.