Header

How to resolve merge conflicts

Resolving merge conflicts in your Git repository

When working on a Git repository, you may run into an issue where you try to pull from the remote, but the code cannot be automatically merged into your working copy, due to conflicts between the two versions.

Identify affected files

Firstly, you'll need to identify the files that are affected by the conflict. Navigate to your repository, then type git status:

$ cd projects/learngit/shop
$ git status
> # On branch my-new-branch
> # You have unmerged paths.
> #   (fix conflicts and run "git commit")
> #
> # Unmerged paths:
> #   (use "git add ..." to mark resolution)
> #
> # both modified:      assets/style.css
> #
> no changes added to commit (use "git add" and/or "git commit -a")\

Resolving conflicts

You'll then need to open any files in your chosen text editor, and check where the merge conflicts actually are.

Git will automatically modify any files with conflicts, and add markers before and after the offending code to allow you to easily identify it.

Search for <<<<<<<< to see the beginning of the conflict, then you'll see <<<<<<< HEAD which will be directly above the changes from the HEAD of the branch, followed by =======, then your code, finishing with >>>>>>> my-new-branch which is the conflicting branch. It'll look something like this:

body {
  background: #1a1a1a;
  <<<<<<< HEAD
  color: #ffffff;
  =======
  color: #000000;
  >>>>>>> my-new-branch
  font: 400 18px/1.5 "PT Serif", sans-serif;
  margin: 0;
}

You'll simply need to remove the line you don't require, along with the >>>>>>> and ======= markers, and commit your change.

$ git add assets/style.css
$ git commit -m "Resolve merge conflict"

You'll now be able to merge the branches locally, and push your changes to the remote repository.