Git & GitHub ยท Chapter 17 of 42

Resolving Merge Conflicts

A merge conflict happens when Git cannot automatically combine changes because the same lines were edited differently on both branches. Git pauses the merge and marks the conflicting sections for you to resolve manually.

Resolving conflicts means editing the file to keep the correct content, removing Git's conflict markers, then staging and committing the resolved file to complete the merge.

Syntax
<<<<<<< HEAD
your changes
=======
their changes
>>>>>>> branch-name

Recognizing a conflict

Git marks conflicts with `<<<<<<<`, `=======`, and `>>>>>>>` inside the file, showing both versions of the conflicting lines side by side.

Resolving and completing the merge

Edit the file to keep the correct content and delete the conflict markers, then run `git add filename` and `git commit` to finish the merge.

Example 1 (bash)
git merge feature-login
Output
CONFLICT (content): Merge conflict in app.js
Automatic merge failed; fix conflicts and then commit the result.

Git reports a conflict in app.js and pauses the merge for manual resolution.

Example 2 (bash)
git add app.js
git commit -m "Merge feature-login, resolve conflicts"
Output
[main 6e2a1bc] Merge feature-login, resolve conflicts

After manually fixing the file, staging and committing completes the merge.

Key points

  • Merge conflicts occur when the same lines change differently on two branches.
  • Git marks conflicts with <<<<<<<, =======, and >>>>>>>.
  • You must manually edit the file to resolve the conflict.
  • Stage and commit the resolved file to complete the merge.
๐Ÿ’ก Note: Use `git merge --abort` at any time before committing to cancel a conflicted merge and return to the previous state.

๐Ÿ“ Quick Quiz

1. When does a merge conflict happen?

2. What symbols mark the start of a conflict section?

3. How do you cancel an in-progress conflicted merge?