softare development

How to Resolve Git Merge Conflicts

Few things halt a developer’s flow faster than seeing the dreaded word: CONFLICT.

A Git merge conflict happens when Git cannot automatically reconcile differences made to the exact same part of a file, or when one branch deletes a file while another modifies it. While it might look intimidating at first, resolving a conflict is just a matter of reviewing the code, picking what stays, and completing the commit.

Here is the standard workflow to fix Git merge conflicts without breaking your codebase.

Learn software development

1. Start the Merge

You initiate a merge into your active branch:

git merge feature-branch

If Git runs into conflicting lines, it pauses the operation and outputs a message like this:

CONFLICT (content): Merge conflict in app.js
Automatic merge failed; fix conflicts and then commit the result.

2. Locate the Conflicted Files

To see every file that needs your attention, run:

git status

Git will list all unmerged paths clearly.

3. Understand the Conflict Markers

Open any conflicted file (app.js, for instance) in your editor. You will spot markers generated by Git that look like this:

<<<<<<< HEAD
console.log("Current branch");
=======
console.log("Feature branch");
>>>>>>> feature-branch

Here is how to decode those lines:

  • <<<<<<< HEAD: Marks the beginning of the conflicting code on your current branch.
  • =======: The divider separating the two conflicting versions.
  • >>>>>>> feature-branch: Marks the end of the incoming changes from the branch you are merging.

4. Resolve the Conflict

To resolve the conflict, edit the file directly. You can choose to keep:

  • Your current branch’s changes
  • The incoming branch’s changes
  • A combination of both

For example, to keep both versions:

console.log("Current branch");
console.log("Feature branch");

Important: Make sure to delete all Git markers (<<<<<<<=======, and >>>>>>>) from the file before saving.

5. Mark as Resolved & Complete the Merge

Once you have saved the cleaned-up file, stage it to mark the conflict as resolved:

git add app.js

Finally, finish the merge with a commit:

git commit

If Git opens your default text editor with a pre-filled merge commit message, simply save and close the file.

Putting It All Together: Quick Example

git checkout main
git merge feature-login

# Edit src/login.js to manually clear markers and select valid code

git add src/login.js
git commit

Pro-Tips & Useful Commands

Need Quick Cheat Commands?

  • List only conflicted files:
    Bashgit diff --name-only --diff-filter=U
  • Bail out completely (reset to pre-merge state):
    Bashgit merge --abort
  • Launch a visual diff tool:
    Bashgit mergetool

Take Advantage of Your IDE

If you use modern editors like VS Code or IntelliJ IDEA, you rarely need to edit markers manually. They offer convenient inline action buttons right above the conflict:

  • Accept Current Change
  • Accept Incoming Change
  • Accept Both Changes

They also offer three-way or side-by-side graphical merge editors that let you compare changes visually.

Recent Posts

The SOC Analyst

A SOC Analyst (Security Operations Center Analyst) is one of the frontline defenders of an organization’s cybersecurity…

2 days ago

Building an Offline-Friendly Image Upload System

Most image upload systems assume one thing: the user has a stable internet connection. That assumption…

1 week ago

JavaScript Background Sync API

Imagine a user submits a form while their internet connection suddenly disappears. Normally, the request…

1 week ago

10 Signs Your PC Has Been Hacked

Cybercriminals don't always announce their presence. Many compromises are designed to remain unnoticed for weeks…

2 weeks ago

What Killed jQuery?

For years, jQuery was everywhere. If you were building websites in the 2010s, there was a…

2 weeks ago

How to Create REST APIs in Java Spring Boot

Spring Boot is one of the most popular frameworks for building REST APIs in Java.…

3 weeks ago