Git can usually combine changes from different branches automatically, but not always. If two branches modify the same part of the same file in different ways, Git may not know which version should win. This situation is called a merge conflict.
A merge conflict may sound serious, but it really means:
“There are two different changes here, and Git needs you to decide what the final version should be.”
A Simple Conflict Example
Suppose main contains:
const title = "Welcome";Someone changes it on main to:
const title = "Welcome to Git Demo";At the same time, another change on feature/homepage turns the same line into:
const title = "Welcome Back";Now you run:
git switch main
git merge feature/homepageGit cannot automatically decide whether the final result should be:
const title = "Welcome to Git Demo";or:
const title = "Welcome Back";So Git stops the merge and reports a conflict.
How Do You Know There Is a Conflict?
After the merge command, you may see something like:
CONFLICT (content): Merge conflict in app.js
Automatic merge failed; fix conflicts and then commit the result.Run:
git statusto see which files are affected.
You may see:
both modified: app.jsThis means both branches changed app.js, and Git needs your help to finish the merge.
What Does a Conflict Look Like Inside a File?
If you open the conflicted file directly, Git may have inserted markers such as:
<<<<<<< HEAD
const title = "Welcome to Git Demo";
=======
const title = "Welcome Back";
>>>>>>> feature/homepageYou can read them roughly as:
<<<<<<< HEAD
Current branch version
=======
Incoming branch version
>>>>>>> feature/homepageYour job is to decide what the final code should be.
For example, you may decide the correct result is:
const title = "Welcome Back to Git Demo";In that case, replace the entire conflict section with:
const title = "Welcome Back to Git Demo";and remove the special conflict markers:
<<<<<<<
=======
>>>>>>>Conflict decision editor
Merge feature/theme, remove conflict markers in the editor, then add and commit the resolution.
—
—
No commits yet
Merge feature/theme, remove conflict markers in the editor, then add and commit the resolution.Using VS Code to Resolve Conflicts
For beginners, VS Code is usually easier than editing conflict markers manually.
When you open a conflicted file, VS Code can visually separate the two versions and may offer options such as:
Accept Current Change
Accept Incoming Change
Accept Both ChangesCurrent Change usually refers to the branch you are currently on, while Incoming Change refers to the branch being merged into it.
You can also keep both versions and then edit the result manually.
The important point is not simply to make the conflict warning disappear. You need to decide which result actually makes sense for the project.
For example, if one branch says:
const timeout = 3000;and another says:
const timeout = 5000;Git cannot know which value is correct for your application. That is a development decision, not a Git decision.
Finishing the Merge
Once you have resolved all conflicts, check the repository:
git statusThen stage the resolved file:
git add app.jsor stage all resolved changes:
git add .Finally, create the commit:
git commit -m "Resolve merge conflict"The merge is now complete.
The overall process is:
git merge → Conflict occurs → Find conflicted files → Choose the correct final code → Remove conflict markers → git add → git commit
KNOWLEDGE QUIZ
Always Test After Resolving a Conflict
A conflict being resolved does not automatically mean the code is correct.
For example, you might accidentally end up with:
const timeout = 3000;
const timeout = 5000;Git may consider the conflict resolved because the conflict markers are gone, but the application may now contain a bug or invalid logic.
After resolving conflicts, run the project or test the affected feature to make sure everything still works.
What If You Want to Cancel the Merge?
If the conflict has just occurred and you decide that the merge was a mistake, you can cancel it with:
git merge --abortGit will try to return the repository to the state it was in before the merge started.
This is a very useful command for beginners. If you merged the wrong branch or the conflict is more complicated than expected, you can abort the merge, review the situation, and try again later.
Practice
A merge creates a conflict on one line of app.js. Write the concrete steps and commands from identifying the conflicted file through resolving, checking markers, staging, and completing the merge commit.