Working with Merge Conflicts in Git

There are instances while working with a shared repo that your Pull Request will hit a conflict with the master repo and that will prevent your PR from being merged.

The easiest way to resolve this is going to be via your terminal.

Step 1 – Ensure you are working with the master repo (the repo you are trying to push your PR too)

The key is to make sure the origin of your repo is pointing to the right repository. By default, your local repo will probably have it’s origin set to your specific branch. Deconflicting in this instance is not possible.

Fix this by checking out the master.

git branch

This will give you something like this:

server:path$ git branch
* [name]-master
master
[user]-master

Will give you a list of the available repositories.

git checkout -b [name]-master master

Will switch the “origin” to the master repo. Only then, can you really resolve the problem.

Step 2 – Identify and Resolve the Merge Conflict

Step 1 is the most important, from there the steps identified by Github make the process simpler.

Running

git status

Will identify where the merge conflict is:

$ git status
> # On branch branch-b
> # You have unmerged paths.
> #   (fix conflicts and run "git commit")
> #
> # Unmerged paths:
> #   (use "git add ..." to mark resolution)
> #
> # both modified:      styleguide.md
> #
> no changes added to commit (use "git add" and/or "git commit -a")

Open that file, sytleguide.md, and scroll until you see the annotations that signify where the change is:

If you have questions, please
<<<<<<< HEAD
open an issue
=======
ask your question in IRC.
>>>>>>> branch-a

This will appear right in the fie, via your editor. All you have to do is choose which entry to keep. Delete section you don’t want, and be sure to remove the annotations.

It will look like this when you’re done:

If you have questions, please

ask your question in IRC.

Notice I removed the annotations, and the entry I didn’t want. I want everyone to come to IRC. 🙂

Step 3 – Commit the Changes

Once you have identified what you want to keep, and delete proceed with committing the change.

$ git add .

$ git commit -m “Resolved merge conflict by forcing users to contact via IRC.

The final step is to switch back to your branch to avoid making changes directly to the master.

$ git checkout [user]-master

This will make sure that all future changes go back to your branch, and you can go back to your normal development process.

Hope this helps!

Leave a Reply