How to Resolve a Merge Conflict in Git

One of the situations that most often makes beginner developers panic is the appearance of a merge conflict in Git. But don't worry — merge conflicts are normal, and even experienc...

How to Resolve a Merge Conflict in Git

One of the situations that most often makes beginner developers panic is the appearance of a merge conflict in Git. But don't worry — merge conflicts are normal, and even experienced developers face them often. Understanding how to read and resolve them correctly is an important skill you'll need every time you work in a team.

What Is a Merge Conflict?

A merge conflict happens when two branches change the same line in the same file, and Git can't automatically determine which version to keep. Git stops the merge process and asks you to resolve the conflict manually before continuing.

Conflicts usually appear when:

  • Two developers change the same line in the same file.
  • One person changes a file while another deletes it.
  • You rebase a branch that hasn't been synced with main for a long time.

Example Merge Conflict Scenario

Imagine you and a teammate both change the file index.html on the same line. When you try to merge your teammate's branch into yours:

git checkout main
git merge feature-header

Git shows a message like this:

Auto-merging index.html
CONFLICT (content): Merge conflict in index.html
Automatic merge failed; fix conflicts and then commit the result.

Reading the Conflict Markers in a File

Open the conflicting file. Git marks the conflicting sections with special markers:

<<<<<<< HEAD
<h1>Welcome to Our Website</h1>
=======
<h1>Hello, Welcome!</h1>
>>>>>>> feature-header

An explanation of those markers:

  • <<<<<<< HEAD — the code version from your current branch (main).
  • ======= — the separator between the two conflicting versions.
  • >>>>>>> feature-header — the code version from the branch you want to merge in.

Steps to Resolve a Merge Conflict

  1. Identify the conflicting files with git status. Conflicting files are marked as both modified.
  2. Open the file using your favorite text editor or IDE.
  3. Choose the correct version — either one, or a combination of both. Remove all the Git markers (<<<<<<<, =======, >>>>>>>) and leave only the content you want.
  4. Save the file after the conflict is resolved.
  5. Mark it as resolved by adding the file to staging:
git add index.html
  1. Finish the merge by creating a commit:
git commit -m "Resolve merge conflict in index.html"

Using the Built-in Merge Tool

Git provides a command to open a graphical merge tool that is easier to read:

git mergetool

You can configure which tool to use, for example VS Code:

git config --global merge.tool vscode
git config --global mergetool.vscode.cmd "code --wait $MERGED"

Avoiding Merge Conflicts in the Future

Some best practices to minimize conflicts:

  • Do git pull from the main branch frequently so your branch is always up to date.
  • Divide tasks clearly within the team so two people don't work on the same file at the same time.
  • Create short-lived branches — finish the feature, then merge as soon as possible.
  • Communicate actively with the team before changing critical files like the main configuration.

Aborting an In-Progress Merge

If you decide to cancel the merge process and return to the state before the merge started:

git merge --abort

This command can only be run while the merge is still in progress (before the commit is finished).

Conclusion

Merge conflicts are a natural part of collaborating with Git. The key is to read the conflict markers carefully, choose the most correct code version (or combine both), remove all markers, then commit the result. With practice and good team communication, you'll get faster and more confident at resolving conflicts. Don't panic — a conflict isn't an error, but a signal that Git needs your human judgment.

merge conflict git cara mengatasi conflict git git merge conflict resolve conflict git git tutorial konflik git
Share this article
Back to Blog
🚀 Partner Recommendation

Need Premium Source Code & Business Apps?

Access Laravel applications, POS systems, School Management, Clinic Software, ERP solutions, and ready-to-use premium source code at GudangCode.

GudangCode
  • ✔ Premium Source Code
  • ✔ Ready-to-Use Systems
  • ✔ Lifetime Updates
  • ✔ Lifetime Membership
  • ✔ Daily App Updates
Join Membership →