VS Code SetupIntermediate

How to Resolve Git Merge Conflicts in VS Code

Use VS Code's conflict editor to understand both sides of a merge conflict and resolve it cleanly without breaking your code.

9 minIntermediate

A merge conflict happens when two changes touch the same lines and git cannot decide which to keep. It looks scary the first time, but VS Code shows both versions clearly and lets you choose. This guide walks through spotting a conflict, reading it, and resolving it with the built-in tools.

What you need

  • A git repository open in VS Code
  • A merge or pull that produced a conflict (the steps below explain what that looks like)
  • A basic understanding of staging and committing

Step 1: Recognize the conflict

When you pull or merge and git cannot reconcile changes, it stops and reports a conflict. In the Source Control view the affected files appear under Merge Changes with a warning icon, and the files contain conflict markers.

zsh - my-app
$git merge feature-branch
Auto-merging src/config.js
CONFLICT (content): Merge conflict in src/config.js
Automatic merge failed; fix conflicts and commit the result.
$

Step 2: Read the conflict markers

Open the conflicted file. Git inserts markers around the clashing region. The block after the line with seven less-than signs is your current version (HEAD), and the block after the seven equals signs is the incoming version, ending at the seven greater-than signs. VS Code highlights both and adds clickable actions above them.

VS Code - conflict editor
Explorer
config.js
src/config.js
1 Accept Current | Accept Incoming | Accept Both
2 <<<<<<< HEAD
3 timeout: 3000
4 =======
5 timeout: 5000
6 >>>>>>> feature-branch
Current and incoming changes shown with action links.

Step 3: Choose a resolution

Click one of the action links above the conflict: Accept Current Change keeps your version, Accept Incoming Change takes theirs, and Accept Both keeps both blocks. If neither side is fully right, just edit the text by hand and delete all the marker lines yourself. The goal is code with no markers left.

Delete every marker
If you edit by hand, make sure no lines starting with seven less-than, equals, or greater-than signs remain. Leftover markers are not valid code and will break your file.

Step 4: Stage and commit the resolution

Once the file has no conflict markers, save it. In Source Control, stage the resolved file with the plus icon; this tells git the conflict is handled. When every conflicted file is staged, type a commit message and commit to complete the merge.

zsh - my-app
$git add src/config.js
$git commit -m "Merge feature-branch, keep 5000ms timeout"
[main 9f8e7d6] Merge feature-branch, keep 5000ms timeout
$

Result

The conflict is resolved, the markers are gone, and the merge commit is recorded. Your branch now combines both sets of changes the way you intended.

Watch related tutorials

Tags
#git#merge-conflict#source-control#merge