How Merge Conflicts Happen
Merge conflicts happen when two commits affect the same line of code at the same time.
- Feature branch modifies line 5 and commits.
- Master branch modifies line 5 and commits.
- 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