Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 40 ยท Git and Remote Development

Diffs and Merge Conflicts in VS Code

Compare files with the VS Code diff editor, use inline and side-by-side views, and resolve Git merge conflicts with the 3-way merge editor step by step.

Reading differences is a core developer skill. Before you commit, you want to see exactly what changed. When two branches change the same lines, Git cannot decide for you, and you must resolve a merge conflict. VS Code helps with both: a good diff editor for comparing, and a 3-way merge editor for conflicts.

This lesson covers both tools and the thinking behind them. The runnable code blocks are stand-ins written in JavaScript that model a line diff and a three-way merge; they are simplified and not the algorithms Git uses.

The diff editor

Click a changed file in the Source Control view and VS Code opens a diff: the old version on the left, the new on the right, with removed lines in red and added lines in green. You can also compare manually:

  • File: Compare Active File With... compares the current file with another file you pick.
  • In the Explorer, select two files, right-click and choose Compare Selected.
  • Select for Compare, then Compare with Selected on another file.
  • Compare with Clipboard from the editor's context menu.
  • From a terminal: code --diff file1 file2.

In the diff toolbar:

  • Inline view toggle switches between side-by-side and a single column (setting diffEditor.renderSideBySide).
  • Ignore trim whitespace hides whitespace-only changes (diffEditor.ignoreTrimWhitespace).
  • Next and previous change arrows (F7 and Shift+F7) jump between hunks.
  • The editor on the right is editable for working-tree diffs, so you can fix things while you review.
  • Collapsed unchanged regions can be expanded with a click.

The diff editor also detects moved code and can show it (diffEditor.experimental.showMoves).

How merge conflicts happen

A conflict appears when two branches change the same part of a file in different ways, or one deletes a file the other edits. Git stops the merge, marks the file as C (conflict) and inserts markers:

TEXT
<<<<<<< HEAD
const greeting = "Hello";
=======
const greeting = "Hi";
>>>>>>> feature/short-greeting
  • Between <<<<<<< and ======= is your side (the current branch, also called "current" or "ours").
  • Between ======= and >>>>>>> is their side (the incoming branch).

Never commit with markers still in the file; resolve every conflict first.

flowchart LR
    B["base"] --> M["main"]
    B --> F["feature"]
    M --> R["merge"]
    F --> R
    R --> C["conflict if both changed the same lines"]

A three-way merge compares both branches with their common base.

Resolving in the editor

Open a conflicted file. VS Code shows small links above each conflict, called CodeLens:

  • Accept Current Change keeps your side.
  • Accept Incoming Change keeps the other side.
  • Accept Both Changes keeps both, one after another.
  • Compare Changes opens a diff of the two sides.

You can also edit the text by hand, deleting the markers and keeping whatever final result is right. After resolving all conflicts in a file, stage it with + in Source Control. When every conflicted file is staged, commit to finish the merge. If you change your mind, Git: Abort Merge returns to the state before the merge.

The 3-way merge editor

For larger conflicts, click Resolve in Merge Editor in a conflicted file's banner. The merge editor shows:

  • Incoming (left) and Current (right) at the top,
  • a Result editor below, which becomes the final file,
  • a Base view (optional) showing the common ancestor,
  • checkboxes for each change; ticking one applies it to the Result,
  • a counter of remaining conflicts and a Complete Merge button.

Because you can see the base, you understand what each side actually changed. A change only the incoming branch made is applied automatically; a conflict arises only where both changed the same region. Turn the merge editor off if you prefer the classic inline markers with git.mergeEditor.

Habits that reduce conflicts

  • Pull often so your branch is not far behind.
  • Keep changes small and commits focused.
  • Do not reformat whole files in unrelated commits; a formatting change touches every line and invites conflicts. See formatters and linters for format-on-save with "modifications".
  • Talk to the person whose change conflicts with yours when the intent is unclear.
  • Run the tests after resolving. A merge that compiles can still be wrong.

Runnable stand-ins

JS Stand-in: a simple line diff

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Stand-in: three-way merge by line

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does Accept Incoming Change do?

    Incoming is the branch being merged in; accepting it keeps those lines and drops yours for that conflict.
  2. 2. Why is the Base view useful in the merge editor?

    Comparing each side with the base reveals who changed what.
  3. 3. What must you do before committing a merge?

    Markers left in files break code; resolved files need to be staged before the merge commit.
  4. 4. Which command-line option opens VS Code's diff editor?

    code --diff a b opens the two files in the diff editor.

Summary

  • The diff editor compares two versions side by side or inline; F7 jumps between changes.
  • Compare any two files from the Explorer, the Command Palette or code --diff.
  • Conflicts mark your side and the incoming side; resolve, remove markers, stage, commit.
  • The 3-way merge editor adds a Base view and a Result you can build with checkboxes.
  • Small, frequent merges and no unnecessary reformatting reduce conflicts.

Next: Branches, History and the Timeline