Git - Part 6: Handling Merge Conflicts
Creating conflict and merging branches
git checkout master
git merge feature-branch
Notice on conflicts
Automatic merge failed; fix conflicts and then commit the result.
Show merge conflicts
git diff path/to/file.ext
Merge conflict example
<<<<<<< HEAD
Content on current branch (master)
=======
Content on merged branch (feature-branch)
>>>>>>> feature-branch
Exercise
- Create Git repository, add and commit file index.html with content <h1>Hello world!</h1>
git init
touch index.html
vi index.html
git add index.html
git commit -m "Initial commit"
- Create a new branch called 'conflict-branch'
git checkout -b conflict-branch
- Edit index.html file and change h1 tags to h2
vi index.html
- Add and Commit to conflict-branch branch with a message "Change h1 to h2"
git add .
git commit -m "Change h1 to h2"
- Checkout master branch
git checkout master
- Edit index.html file and change h2 tags to h3
vi index.html
- Add and Commit to master branch with a message "Change h1 to h3"
git add .
git commit -m "Change h1 to h3"
- Merge conflict-branch (see: CONFLICT; Automatic merge failed;)
git merge conflict-branch
- Check Git status (see: both modified: index.html)
git status
- Edit index.html file and fix conflicts; change:
<<<<<<< HEAD <h3>Hello World!</h3> ======= <h2>Hello World!</h2> >>>>>>> conflict-branch
To:<h2>Hello World!</h2>
- Add and Commit to master branch with a message "Fix conflicts"
git add .
git commit -m "Fix conflicts"
- Check Git log (see: all commits)
git log --oneline