Tour of Git
Branching & Merging / Lesson 2.5

Merge Conflicts — Resolving Differences

A merge conflict happens when two branches modify the same part of the same file. Git can't automatically decide which change to keep, so it asks you to resolve it manually.

When Do Conflicts Happen?

main:     echo "hello" > greet.txt
feature:  echo "hi" > greet.txt

Both branches changed greet.txt differently. When you merge, Git doesn't know which version you want.

What a Conflict Looks Like

Git marks the conflicting sections in the file:

<<<<<<< HEAD
hello
=======
hi
>>>>>>> feature
  • Above =======: Your current branch's version (HEAD/main)
  • Below =======: The incoming branch's version (feature)

How to Resolve

  1. Edit the file to choose what you want (remove the markers)
  2. Stage the resolved file: git add greet.txt
  3. Complete the merge: git commit -m "Resolve conflict"

Try it!

Create a conflict and resolve it:

  1. Create a file and commit: echo "hello" > greet.txt then git add greet.txt then git commit -m "Add greeting"
  2. Create a branch: git checkout -b feature
  3. Change the file: echo "hi there" > greet.txt then git add greet.txt then git commit -m "Change greeting on feature"
  4. Go back to main: git checkout main
  5. Make a different change: echo "hey world" > greet.txt then git add greet.txt then git commit -m "Change greeting on main"
  6. Try to merge: git merge feature — conflict!
  7. Resolve: echo "hello world" > greet.txt then git add greet.txt then git commit -m "Resolve merge conflict"

Tips

  • Don't panic — conflicts are normal and expected
  • Always run git status during a conflict to see what needs resolving
  • The merge commit message should explain how you resolved the conflict
🎯

Goal

Create a merge conflict between two branches, then resolve it

Terminal
$

No commits yet. Run `git init` and create your first commit.