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

  1. 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"
  2. Create a new branch called 'conflict-branch'
    git checkout -b conflict-branch
  3. Edit index.html file and change h1 tags to h2
    vi index.html
  4. Add and Commit to conflict-branch branch with a message "Change h1 to h2"
    git add .
    git commit -m "Change h1 to h2"
  5. Checkout master branch
    git checkout master
  6. Edit index.html file and change h2 tags to h3
    vi index.html
  7. Add and Commit to master branch with a message "Change h1 to h3"
    git add .
    git commit -m "Change h1 to h3"
  8. Merge conflict-branch (see: CONFLICT; Automatic merge failed;)
    git merge conflict-branch
  9. Check Git status (see: both modified: index.html)
    git status
  10. Edit index.html file and fix conflicts; change:
    <<<<<<< HEAD
    <h3>Hello World!</h3>
    =======
    <h2>Hello World!</h2>
    >>>>>>> conflict-branch
    To:
    <h2>Hello World!</h2>
  11. Add and Commit to master branch with a message "Fix conflicts"
    git add .
    git commit -m "Fix conflicts"
  12. Check Git log (see: all commits)
    git log --oneline