Lesson 24 of 60 – Merge Conflicts
40%

Merge Conflicts in Git

A merge conflict occurs when Git cannot automatically combine changes from different branches. This commonly happens when two branches modify the same part of a file in different ways.

Note: A merge conflict is not a Git error that means your repository is broken. Git is asking you to decide which changes should be kept in the final version.

1. What is a Merge Conflict?

A merge conflict happens when Git cannot determine automatically how to combine changes from two different development histories.

For example, two developers may edit the same line of a file differently.

Branch A → Change X
Branch B → Change Y

Git needs a developer to decide which final content should remain.

2. Why Do Merge Conflicts Happen?

Conflicts commonly happen when different branches make incompatible changes to the same part of a file.

  • Both branches modify the same line.
  • One branch changes a file while another changes the same area.
  • One branch deletes a file while another modifies it.
  • Branches have developed independently for a long time.

3. Simple Conflict Example

Suppose the main branch contains:

<h1>Welcome to My Website</h1>

The feature branch changes the same line to:

<h1>Welcome to Our Website</h1>

If another branch has changed that same line differently, Git may not be able to merge the changes automatically.

4. Creating a Situation for a Conflict

A simple workflow that can lead to a conflict is:

git switch main
git switch -c feature-one

Change a particular line and commit it. Then another branch can modify the same line differently.

5. Starting a Merge

A conflict is usually discovered when you merge one branch into another:

git switch main
git merge feature-one

If Git cannot automatically combine the changes, the merge stops and Git reports the conflicting files.

6. Checking for Conflicts

Use git status after Git reports a conflict.

git status

Git will identify files that contain unresolved conflicts.

7. Conflict Markers

Git places special markers in conflicted files.

<<<<<<< HEAD
Current branch content
=======
Incoming branch content
>>>>>>> feature-login

These markers show the two versions that Git could not automatically combine.

8. Meaning of <<<<<<< HEAD

The line:

<<<<<<< HEAD

marks the beginning of the content from the branch currently checked out during the merge.

9. Meaning of =======

The separator:

=======

separates the current branch's content from the incoming branch's content.

10. Meaning of >>>>>>>

The ending marker identifies the incoming branch:

>>>>>>> feature-login

The branch name may be different depending on the branch being merged.

11. Reading a Conflict

<<<<<<< HEAD
Welcome to My Website
=======
Welcome to Our Website
>>>>>>> feature-homepage

The current branch contains one version, while the incoming branch contains another version.

12. Choosing the Final Content

To resolve the conflict, edit the file and decide what the final content should be.

For example, you might keep:

Welcome to Our Website

Or you may create a completely different final version.

13. Removing Conflict Markers

After deciding the correct content, remove all conflict markers:

<<<<<<<
=======
>>>>>>>

The final file should contain only the content that you want to keep.

14. Staging a Resolved File

After resolving a conflict, stage the file:

git add index.html

Staging the file tells Git that you have resolved its conflict.

15. Checking Resolution Status

Run:

git status

Git should show the resolved file as staged and no longer list it as an unresolved conflict.

16. Completing the Merge

After resolving all conflicts and staging all affected files, complete the merge:

git commit

Git can use a merge commit message that you can review and edit.

17. Merge Conflict Workflow

git merge feature-login

# Conflict occurs

git status

# Edit conflicted files

git add .

git commit

This is the basic workflow for resolving a merge conflict.

18. Aborting a Merge

If you do not want to continue the merge, you can abort the merge while it is in progress:

git merge --abort

Git attempts to return the repository to the state before the merge started.

19. Multiple Conflicted Files

A single merge can produce conflicts in multiple files.

git status

Review every conflicted file, resolve each one, and stage each resolved file.

git add index.html
git add style.css
git add script.js

20. Resolving All Conflicts at Once

After carefully resolving every conflicted file, you can stage all resolved changes:

git add .

Always verify the result with git status before completing the merge.

21. Conflict in HTML File

<<<<<<< HEAD
<h1>My Website</h1>
=======
<h1>Our Website</h1>
>>>>>>> feature-title

After resolving:

<h1>Our Website</h1>

Then stage the file:

git add index.html

22. Conflict in CSS File

<<<<<<< HEAD
font-size: 20px;
=======
font-size: 24px;
>>>>>>> feature-style

Choose the required value and remove the conflict markers.

font-size: 24px;

23. Conflict in JavaScript File

<<<<<<< HEAD
let message = "Hello";
=======
let message = "Welcome";
>>>>>>> feature-message

Choose or combine the required code and remove the conflict markers before staging the file.

24. Using a Merge Tool

Git can also work with external merge tools configured on your computer.

git mergetool

A merge tool can provide a graphical or specialized interface for comparing and resolving conflicting versions.

25. Avoiding Merge Conflicts

Conflicts cannot always be avoided, but good development practices can reduce unnecessary conflicts.

  • Keep branches reasonably up to date.
  • Make focused commits.
  • Communicate when editing shared files.
  • Merge completed work regularly when appropriate.
  • Keep changes small and understandable.
  • Review changes before merging.

26. Common Merge Conflict Mistakes

  • Leaving conflict markers in the source code.
  • Resolving only some of the conflicted files.
  • Forgetting to stage resolved files.
  • Committing without reviewing the final result.
  • Choosing changes without understanding their purpose.
  • Using git merge --abort after already completing the merge.
  • Forgetting to test the application after resolving conflicts.

27. Useful Conflict Commands

git merge branch-name
git status
git diff
git add filename
git add .
git commit
git merge --abort
git mergetool

28. Complete Conflict Resolution Workflow

# Switch to target branch
git switch main

# Start merge
git merge feature-login

# Check conflicts
git status

# Open and resolve conflicted files

# Stage resolved files
git add .

# Check status
git status

# Complete merge
git commit

29. Practical Merge Conflict Example

git switch main

git merge feature-homepage

# CONFLICT occurs

git status

# Edit index.html
# Remove conflict markers
# Keep the correct final content

git add index.html

git status

git commit -m "Resolve homepage merge conflict"

git log --oneline --graph --all

After the conflict is resolved and committed, the two development histories have been successfully combined.

30. Summary of Merge Conflicts

A merge conflict occurs when Git cannot automatically combine changes from different branches.

git status
git add .
git commit

The basic process is to inspect the conflicted files, decide what the final content should be, remove conflict markers, stage the resolved files, and complete the merge.

If you decide not to continue the merge, use:

git merge --abort

📌 Key Points

  • A merge conflict occurs when Git cannot automatically combine changes.
  • Conflicts commonly occur when the same part of a file is changed differently.
  • git status shows conflicted files.
  • Git uses conflict markers such as <<<<<<<, =======, and >>>>>>>.
  • Review both versions before deciding the final content.
  • Remove all conflict markers after resolving a conflict.
  • Use git add filename to mark a file as resolved.
  • Use git commit to complete the merge.
  • Use git merge --abort to cancel a merge in progress.
  • Multiple files can contain conflicts during one merge.
  • git mergetool can be used with a configured merge tool.
  • Always test the project after resolving important conflicts.

🧠 Quick Quiz

Question: What should you normally do after resolving a merge conflict in a file?