Merge Conflicts Merge conflicts and how to resolve them

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

How Merge Conflicts Happen

Merge conflicts happen when two commits affect the same line of code at the same time.

  1. Feature branch modifies line 5 and commits.
  2. Master branch modifies line 5 and commits.
  3. Master branch tries to merge feature branch.

Here’s how a merge conflict looks from the command line:

command line
git branch feature
# make some changes
git commit -am "awesome branch stuff"

git checkout master
# make some changes to same code
git commit -am "master branch stuff"

git merge feature
# CONFLICT!

Explore a Merge Conflict

Use git diff to compare the changes in the feature branch and master branch.

command line
git diff

Fix a Merge Conflict

The easiest way to fix the merge conflict is use the editor to choose between the incoming changes (feature) or the existing changes (master). Then create a new merge commit with the changes you want to keep.

command line
# choose preferred code on master branch
git commit -am "resolved merge conflict"

If you’re not sure, you can abort:

command line
git merge --abort

Questions?

Ask questions via GitHub below OR chat on Slack #questions