Resolving Merge Conflicts in Git from the Command Line

In the previous blog, we learned about Squash and Merge. Now let’s move one step further into a situation every developer faces: merge conflicts.

Conflicts happen when two branches modify the same file or the same lines differently. Git cannot automatically decide which version to keep, so it asks you to resolve it manually.


Scenario Setup

We start with a new project:

mkdir project5
cd project5
git init

Create a file and commit it:

echo "Hello World" > hello.txt
git add hello.txt
git commit -m "Initial commit with hello.txt"

Creating a Feature Branch

Create and switch to a feature branch:

git switch -C feature/module

Modify hello.txt and add a new file:

echo "New line added in feature branch" >> hello.txt
touch feature.txt
git add .
git commit -m "Feature added"

Making Changes on Main Branch

Switch back to main:

git switch main

Modify the same file (hello.txt):

echo "New line added in main branch" >> hello.txt
git add hello.txt
git commit -m "Main branch update"

Merging the Branches

Now try to merge feature/module into main:

git merge feature/module

Output:

Auto-merging hello.txt
CONFLICT (content): Merge conflict in hello.txt
Automatic merge failed; fix conflicts and then commit the result.

Git also merged the new file feature.txt, but hello.txt has a conflict.


Understanding the Conflict

Open hello.txt:

Hello World
<<<<<<< HEAD
New line added in main branch
=======
New line added in feature branch
>>>>>>> feature/module
  • Between <<<<<<< HEAD and ======= → content from the current branch (main).
  • Between ======= and >>>>>>> feature/module → content from the feature branch.

Resolving the Conflict

You must edit the file manually. For example, keep both lines:

Hello World
New line added in main branch
New line added in feature branch

Save the file, then stage it:

git add hello.txt

Finally, complete the merge:

git commit -m "Resolve merge conflict between main and feature/module"

Verifying the Merge

Check merged branches:

git branch --merged

This will now include feature/module.

Check unmerged:

git branch --no-merged
# (none)

The conflict is resolved and the branches are fully merged.


Real-World Note

In real projects, conflicts may appear in multiple files at once. Modern IDEs (like VS Code, IntelliJ, etc.) provide graphical merge tools that make conflict resolution easier. But here, we focused on the command-line approach, which gives you full control and understanding.


Summary

  • Conflicts occur when both branches modify the same file/lines.
  • Git marks conflicts inside files with <<<<<<<, =======, >>>>>>>.
  • Resolve by manually editing the file.
  • Stage and commit to complete the merge.
  • Use git branch --merged and git branch --no-merged to verify.

Keep Learning 🚀

👉 Subscribe to Learning Ocean – Subscribers get coupon codes for my courses, early access to blogs and courses, and even exclusive YouTube videos.

👉 My YouTube Channel – More videos, more fun, and lots of learning!

👉 📺 Watch this topic in video form

Stay curious, keep coding, and let’s make learning fun together! 🎉