Git & GitHub Fundamentals · Module 06 of 12

Merge conflicts and resolution

Resolve conflicting edits methodically by understanding base, ours and theirs instead of choosing a side blindly.

Learning objectives

By the end of this module, you should be able to:

  • Recognize a content conflict.
  • Inspect conflict markers and repository state.
  • Resolve, stage and complete a merge.

A conflict is a request for human intent

Git can automatically combine many changes. A conflict occurs when it cannot safely decide the resulting content, such as incompatible edits to the same lines.

Conflict markers are not final content

<<<<<<<, ======= and >>>>>>> show competing sections in a conflicted file. Edit the file into the intended final form, remove markers, stage the resolution, then complete the merge.

Abort is safer than improvising

If you started the wrong merge or need to reconsider, git merge --abort attempts to restore the pre-merge state. Resolve with evidence rather than automatically accepting ours or theirs.

Worked examples

See the idea in practice.

Resolve a conflict deliberately

git status
git diff
# edit the conflicted file and remove markers
git add app.conf
git commit
  • git status lists unmerged paths.
  • The final commit records the chosen integrated state.
Hands-on lab

Lab — Create and resolve a real conflict

Goal: Make main and a feature branch edit the same line, then resolve the merge.

Before you start

  • Git 2.23 or newer is recommended so the labs can use git switch and git restore.
  • Run labs in a disposable working directory; the included validator creates and removes its own temporary repository.
STEP 1

Create base

mkdir -p ~/gnu-git-labs/module06 && cd ~/gnu-git-labs/module06
git init -b main
git config user.name "GNU Learner"
git config user.email "learner@example.invalid"
printf "mode=base\n" > app.conf
git add app.conf && git commit -m "Add base mode"
STEP 2

Feature edit

git switch -c feature/mode
printf "mode=feature\n" > app.conf
git commit -am "Set feature mode"
STEP 3

Conflicting main edit

git switch main
printf "mode=main\n" > app.conf
git commit -am "Set main mode"
STEP 4

Start merge and inspect conflict

git merge feature/mode || true
git status --short
git diff
STEP 5

Resolve intentionally

printf "mode=integrated\n" > app.conf
git add app.conf
git commit -m "Resolve mode conflict"
git status --short

Verify

  • The merge initially reports a conflict.
  • After resolution there are no conflict markers and status is clean.

Expected outcome

  • The resulting merge commit has two parents and contains mode=integrated.

If it fails

  • If the merge unexpectedly succeeds, confirm both branches edited the same original line differently.
  • Use git merge --abort before resolution if you need to restart the exercise.
Real-world connection

Conflict resolution is a review task: the correct output depends on application intent, not on which branch is newer.

Avoid these traps

Common mistakes

  • Committing files that still contain conflict markers.
  • Choosing ours/theirs without understanding the intended result.
  • Deleting .git to escape a conflict.
Knowledge check

Can you explain it without looking back?

Open each answer only after you have tried to answer the question yourself.

1What does a merge conflict mean?

Git cannot safely determine the combined content automatically.

2After editing a conflicted file, what marks it resolved?

Stage the resolved file with git add before completing the merge.

3How can you abandon an in-progress merge?

git merge --abort, when Git can restore the pre-merge state.