GitKraken - Resolving Merge Conflicts

Often when you’re working on a project with other people, you’ll be using a system that lets you each individually add files to the project. This is great for collaboration! but sometimes things go wrong.

In some cases, 2 people have made changes to the same binary file from the same commit and then try to merge it back into the project repository - this is a merge conflict.

We’ll talk about handling these sorts of conflicts in a program called GitKraken - a software that visuals our project changes and versioning history.

The Setup

In a hypothetical scenario we have 3 different developers simultaneously adding features. All 3 developers are working off of the same, most recent commit… which so far is a good thing.

  1. The first Developer Added Time of Day and kept all his changes local, and got them merged into Dev. The only changes made were in scripts that no one else has touched. This is good!

  2. The second Developer added Debuff Functionality and made changes to the NumberHelper script.

  3. The third Developer added Modifier Functionality and also made changes to the NumberHelper script.

The Problem

The problem here is that both developers have modified the same file from the same commit… meaning that when they merge their changes back in then our Version Control system won’t know which change we actually want.

This is how the graph looks, before the merge:

First, the developer working on Debuffs will commit their changes back into Dev because they were merging frequently, as they should. They have also deleted their branch, since they’re merged in with no issues and done with the feature.

This is how the graph looks now:

Lastly, the Developer working on DebuffFunctionality will attempt to merge in their changes back into Dev branch as well. Remember… both this developer and the previous have made changes to the same script file, Number Helper.

 

As soon as the merge is attempted, this is what pops up:

We have a Merge Conflict! This is letting us know that one of the files has been changed since the last Commit we were working from.

Viewing the Merge Conflicts

First we need to diagnose the problem. You can view all of the merge conflicts on the right, as a path or a tree. I find ‘Path’ easier to manage.

This is the script that both programmers were working in, so it makes sense that there’s a conflict.

If you click on one of the files in the list, GitKraken will open up a new Window that will show you the line-by-line differences in what has changed between the 2 files. If you get a prompt about GitKraken Pro, you can still close it out.

Blue represents one set of changes, yellow represents another set, and purple represents a potential output if we wanted to attempt a Manual Merge. We’ll look at this option later.

For now, you can close out this window with the X button in the top right, as for now we’re just looking at our options.

Ok. Now that we can look at the issues, we need to figure out how to resolve them before we can complete our merge.

We have a few options… let’s go through each one.

 

Solution 1 – Abort Merge and Communicate

In the first case, we’re a little freaked out and confused at the sight of this Merge Conflict. Never fear!

The first thing you could do is just Abort the Merge, and re-evaluate if there’s some other problem happening. Maybe someone else is working in a file they shouldn’t be, or maybe you are. Either way, we can just take a step back by pressing the ‘Abort Merge’ button at the bottom.

Take note that it took us back in time to right before we tried our merge. Cool. We still need to solve our Merge Conflict, but now that we know that it’s there we can communicate with the necessary people to figure out ‘Why’ – which is often more important.

Solution 2 – Choose a File ‘Version’ to keep

Ok, let’s attempt our merge again.

Suddenly it’s not looking so scary!

One thing we could do, particularly with hard-to-merge files, is just choose which version of the file to keep.  This most commonly happens with Large Binary Files, like Art and Sound.

CAUTION - If you do this option then someone is losing progress, since you’re choosing one person’s changes over another. This can be bad if both modifications where extremely important. In the case where someone has ‘accidentally’ modified a file, or weren’t supposed to modify a file, then you may just want to override their changes.

To choose which file version to use to resolve this conflict, first we need to click on the conflicted file to open up the Merge Conflict window:

This should open up the panel that shows you the changes from both files.

If this was a large binary file, the text may not make sense in this view, so we’d likely just have to choose which side we want to take and take EVERYTHING. With code, we can often pick and choose, but don’t worry about that yet.

To take one file’s changes over the other, just click the checkbox next to the letter. In this case, let’s opt to keep all of the changes on dev, since I have no idea if it will be game-breaking or not (so selfless!).

At this point, if I were to click the ‘Save’ Button I’m opting to overwrite everything I’ve done to this file, in favor of what’s already there on Dev.

Now everything is resolved, and we can commit any of our other changes we’ve made, if we want.

We’re now free to complete the commit if we like.

However, instead of this all-or-nothing approach, since we’re working with scripts we can instead attempt to merge our changes together into a single script file. Remember, that this usually only works with scripts and text files, so you may not be able to use this for art assets.

 

Solution 3 – Manually Merge with Text Editor

When working with text files, we have the ability to merge our changes together line-by-line. This means that if coders have edited a single script, we ‘might’ be able to merge our changes together. In my case, 2 coders have added separate methods… easy to merge! If they were both making changes line by line inside of the same method… it might be even harder, or impossible.

Let’s backtrack back to our Merge Conflict Window when we were trying to merge.

The check-boxes allow us to determine block-by-block what we want to keep. Because we’re using Free version of GitKraken, we can only select blocks… if we were using Pro we could actually edit it directly. We could also just edit it inside of our script file directly if we wanted, but it’s a bit more complicated.

To resolve our merge here, select the checkboxes next to the method on each side.

Technically that will combine all our changes… but oh no! Check out the output:

That’s definitely not right! If we try to commit this script we’ll get all sorts of errors.

In this case, it looks like we’re just going to have to merge by hand. Again, if we were using Pro we could just do it here, but it’s not necessary (just more convenient).

 

First, UNCHECK both A and B to make sure that we’ve undone any potential changes.

And press the X button on the panel window to go back to the Merge Conflicts primary view.

Open up your script (in my case NumberHelper.cs) by right clicking the file in the panel and select ‘Open File in Default Program’. This will open up the file in question.

Note… this will only work with a script or text file!

Now we can see what it looks like in our Text Editor.

We will see these markers that show our changes. It may or may not look more complicated than this. Ultimately, what we want to do is look at this combined code and attempt to make it all ‘correct’ by manually editing.

In this case, because it was a simple method addition, I can just remove the markers and add a few brackets.

Now let’s hop back into GitKraken and since we’ve manually resolve this, mark it as such.

It will now stage our files and we’re ready to commit.

As a last check, let’s click our script again (in my case NumberHelper.cs) and see how it looks.

We can see our final merged changes!

Let’s commit everything, and we’re done. WHEW. Merge Conflicts.

Conclusion

In the end, you should avoid working in the same files if at all necessary. If you get too stuck, it may be worth cross collaborating with more technical team members to help resolve it, especially if you’re worried about it messing up something else important.