Skip to content

How to Fix Merge Conflicts

← Back to Git


βš”οΈ How to Fix Merge Conflicts

A Merge Conflict occurs when Git cannot automatically determine how to combine changes from two branches (e.g., when the same line in a file is modified differently).

1. Setup Conflict Scenario

Create a repository mergeconflict. Clone it:

git clone https://github.com/vigneshsweekaran/mergeconflict.git
Git Clone

Create cat.txt with initial content:

1. In terms of development, the first year of a cat’s life is equal to the first 15 years of a human life. After its second year, a cat is 25 in human years. And after that, each year of a cat’s life is equal to about 7 human years.
2. Cats can rotate their ears 180 degrees.
3. The hearing of the average cat is at least five times keener than that of a human adult.
4. In the largest cat breed, the average male weighs approximately 20 pounds.
5. Domestic cats spend about 70 percent of the day sleeping. And 15 percent of the day grooming.

Commit and push to GitHub.

2. Create Changes (Local & Remote)

Local Change: Change line 2 angle from 180 to 150. Commit locally.

Local Commit

Remote Change: On GitHub, change line 2 angle from 180 to 200. Commit on GitHub.

Remote Edit

Remote Commit

3. Trigger Conflict

Check local history:

git log
Log Before

Pull remote changes:

git pull origin master
Pull Conflict

Conflict! Git failed to auto-merge because both sides changed line 2.

4. Resolve Conflict

Open cat.txt. You will see conflict markers:

Conflict View

Decide which change to keep (e.g., 200). Remove markers <<<<<<<, =======, >>>>>>>.

Fixed File

5. Finalize Merge

Add and commit the resolution:

git add cat.txt
git commit
Commit Fix

Check logs to see the merge commit: Log After

Push to GitHub:

git push origin master


🧠 Quick Quiz β€” Conflicts

#

What do the <<<<<<< HEAD markers indicate in a file?


πŸ“ Want More Practice?

πŸ‘‰ Start Git Advanced Quiz (20 Questions)


πŸ“¬ DevopsPilot Weekly β€” Learn DevOps, Cloud & Gen AI the simple way.
πŸ‘‰ Subscribe here