fbpx

How to Resolve a Merge Conflict When Using Git

OMOTOLA SHOGUNLE

ARTICLE SUMMARY

In this 3rd guide from Omotola’s How to Get Good at Git & GitHub - she shares her guidance on how to resolve a merge conflict, so you can feel more at ease when dealing with them!

Whenever I see the word conflict when using Git, it feels very intimidating and scary. 

Something most developers hate to see and try to avoid like the plague. However, trying to avoid merge conflicts on GitHub is not a way to solve the problem. Taking your time to understand why the merge conflict happened and how to resolve it is a much better solution to the issue.

My intention for this article is to help clarify the process of resolving a merge conflict and why they occur in the first place. 

Hopefully, by the end of this article, you’ll be able to understand the concept of resolving merge conflicts and feel more at ease when dealing with them.

Resolving a Merge Conflict: How did we get here?

A merge conflict happens when Git is unable to automatically resolve differences in code. This can be between two commits or branches. The reason for this is that there are conflicting changes to the same line of code or document. So Git gets confused and does not know which version you might want to keep or discard. In this case, there is a need for human intervention.

A classic scenario of this will be when Developer A makes changes to a file named ‘custom.js’ and when it is time to push the changes to the develop branch, it turns out Developer B made similar changes to the same file which has been committed and pushed to GitHub. Now a merge conflict occurs.

You can always start over if you make a Merge Mess.

It’s important to state that the process of resolving a merge conflict can be undone if you make a mistake and want to start over. In this case, you use:

git merge –abort

So you can’t go wrong when merging a conflict with this under your belt. You can always start over from a clean slate.

What does it look like when a merge conflict occurs? 

In the previous article, we have walked through how to create a PR. Git detects a merge conflict when a developer wants to raise a PR (Pull Request). This is what it looks like:

After creating a PR, Git will highlight the conflicted areas, and ask which code you wish to keep.

This is not the only way a merge conflict can occur. A second scenario where this can occur is when you merge another branch into your working branch. In this case, we are merging the main branch into our develop branch using git merge main.

If there are conflicting files, it will look like this:

We will look at resolving the conflicts in the two scenarios in the next section. 

How to resolve a merge conflict in Git 

Scenario 1: On GitHub when raising a PR request

The resolve conflicts button opens up an interactive editor where you can see Incoming Change and Current Change. Git is smart enough to highlight the points of the conflicts we need to resolve using arrowhead symbols. 

<<<<<<<<<< <name of branch> — Incoming Change: Represents the changes you made in your branch that you would like to merge in. 

>>>>>>>>>> <branch you want to merge into> — Current Change: Has the changes in the current branch you want to merge into.

These two changes are normally separated with `==========`

Accept incoming changes if you would like to keep your changes instead, or current change if you want to discard your changes. There is also a ‘keep both changes’ option if that applies. 

NB: It is always good practice to collaborate with another team member if you are unsure what changes should be kept or discarded.

A few things to note here:

  • In a professional setting, you will normally have to request for your code to be reviewed before you merge your changes into another branch. This is good practice!
  • Merging will automatically close the branch you were working on.
  • Don’t forget to pull on the main branch, so you have up-to-date changes locally.

Scenario 2: When you merge another branch into your working branch

Navigate to the file where the conflicts are highlighted. You will then notice arrowhead symbols in the conflict areas. This looks a little different from what is expected on GitHub. In your IDE (e.g. VsCode) it comes up like this:

<<<<<<<<< HEAD refers to the changes you have made and would like to merge into another branch. While >>>>>>>>> main are changes that currently exist in the branch you want to merge into. 

At this point – to accept changes, delete the arrowheads and leave the code snippets that you want to be the final version. After making those changes in the three steps listed below, you will complete resolving the issue. 

  1. Add all the changes you have made
  2. Commit your recent changes
  3. Push the changes you have made to the current branch – this is an optional step if you want to push your changes immediately.

And that is it! Hopefully, seeing the word “conflict” doesn’t look so scary anymore. If the article has increased your confidence level with resolving Git Merge Conflicts? Why don’t you share the article and tag us? We like it when you smash your programming goals 🙂 

Till next time! In the next article, we will be discussing How to undo a Commit – You really don’t want to miss this one. 

Reminders 

  • Create a bookmark folder in your bookmark bar. Left Click on the Bookmark Bar -> Click Add Folder -> Name it `Git Series` -> Add this article to the folder

  • You can always message me on @mycodinghabits on Instagram or email me [email protected] if you have questions.

Useful Links

Get good at Git with 10 useful commands

How to raise a pull request

RELATED ARTICLES

Becca Cooke, Head of Product, Tradebyte, examines the current state of tech industry as a viable career path for women, reframing the barriers for entry...
Dr Zoë Russell, Co-founder, Rethink Carbon, takes a look into the importance of a work-life balance and gives her tips as a female founder on...
Embracing the tech industry as a female entrepreneur is an exciting journey, and accessing funding can be a key stepping stone. Here's a guide from...
Sarah Gilchriest, Chief People Officer at QA Workforce Learning, a group encompassing QA, Circus Street and Cloud Academy, explores the digital skills that will be...

This website stores cookies on your computer. These cookies are used to improve your website and provide more personalized services to you, both on this website and through other media. To find out more about the cookies we use, see our Privacy Policy.