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";
>>>>>>> featureEverything 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
Create a Feature Branch
Branch off from main so you can make changes safely.
Create and switch to a new branch:
git checkout -b featureThis makes a new branch called feature and switches to it.
Change the Same Line on feature
Edit the greeting on the feature branch.
Switch Back to main
Now change the same line differently on main.
Merge feature into main
This is where the conflict appears.
Resolve the Conflict
Choose the final version and commit the fix.
💻 Terminal
📝 VS Code
Click + to create a file.
Select a file to edit
💡 Tips
- • Run
git statusduring 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 addthe file to mark it as resolved.