On This Page

How to Resolve Git Merge Conflicts (Step-by-Step)

Quick Answer: Merge conflicts happen when Git cannot automatically reconcile different changes made to the same lines of code. To resolve a conflict: (1) Run git status to see conflicting files, (2) Open files and edit conflict markers (<<<<<<<, =======, >>>>>>>), (3) Stage resolved files with git add <file>, and (4) Complete the merge with git commit.


1. What a Merge Conflict Looks Like

When Git encounters overlapping edits during git merge or git pull, it pauses and decorates the conflicting file with conflict markers:

<<<<<<< HEAD
console.log("Welcome to main branch");
=======
console.log("Welcome to feature branch");
>>>>>>> feature-branch
  • <<<<<<< HEAD: Start of your current checked-out branch changes.
  • =======: Divider separating the two competing versions.
  • >>>>>>> branch-name: End of the incoming branch changes.

2. Step-by-Step Resolution Process

Step 1: Identify Conflicting Files

Run git status to get a clear list of files marked as both modified:

$ git status
Unmerged paths:
  (use "git add <file>..." to mark resolution)
	both modified:   src/index.js

Step 2: Open and Edit the Conflicting Files

Open src/index.js in your editor (VS Code, WebStorm, or terminal editor). Delete the conflict markers (<<<<<<<, =======, >>>>>>>) and keep only the final correct code:

// Final desired code
console.log("Welcome to dotgit.dev");

Step 3: Stage the Resolved Files

Tell Git that the conflict in src/index.js is resolved by staging it:

$ git add src/index.js

Step 4: Finalize the Merge Commit

Complete the merge process:

$ git commit -m "Merge branch 'feature-branch' into main (resolved conflicts)"

3. How to Abort a Merge Safely

If a merge conflict is too messy and you want to return your repository to the exact state before you ran git merge, run:

$ git merge --abort

This immediately cancels the merge and restores your previous workspace state.


4. Useful Tools & Shortcuts

Accept Current Changes (ours) or Incoming Changes (theirs)

If you want to completely accept one side’s version for a conflicting file:

# Keep your current branch version
$ git checkout --ours -- path/to/file.js
$ git add path/to/file.js

# Accept the incoming branch version
$ git checkout --theirs -- path/to/file.js
$ git add path/to/file.js

To dive deeper into branch merging mechanics, check out /git-merge and /git-rebase.