Branching

Resolving Conflicts

~25 min

Resolving Conflicts

Merge conflicts happen when two branches change the same lines in the same file. Git stops and asks you to decide what the final code should look like.

ℹ️Why conflicts happen

If both branches edit the same line (or one deletes a line the other edits), Git cannot decide which version to keep.

What a conflict looks like

<<<<<<< HEAD
const message = "Hello from main";
=======
const message = "Hello from feature";
>>>>>>> feature

Everything between the markers is the conflict. You choose the final version and remove the markers.

Practice: Create and Resolve a Conflict

Follow the steps to create a real conflict, then resolve it using the VS Code editor.

Steps

1

Create a Feature Branch

Branch off from main so you can make changes safely.

Create and switch to a new branch:

git checkout -b feature

This makes a new branch called feature and switches to it.

2

Change the Same Line on feature

Edit the greeting on the feature branch.

3

Switch Back to main

Now change the same line differently on main.

4

Merge feature into main

This is where the conflict appears.

5

Resolve the Conflict

Choose the final version and commit the fix.

💻 Terminal

Terminal — /
We will create a conflict by changing the same line on two branches.
Try: git checkout -b feature
$

📝 VS Code

Explorer
No files yet.
Click + to create a file.

Select a file to edit

💡 Tips

  • Run git status during a conflict to see which files need attention.
  • If you are unsure, open the file and read the <<<<<<< and >>>>>>> sections carefully.
  • After resolving, always git add the file to mark it as resolved.