Leave a rating/review
Notes: 07. Resolve Conflicts
The merge changes video will cover happy path. This video will show what changes can look like if multiple people are working in the same file.
Eventually once you start working with more team members on a project, you’re going to have merge conflicts. This can happen when one developer updates the same file you were updating. Let’s simulate that so you can see how to resolve those conflicts.
First, on the main branch, we’ll just make a commit. Now let’s diverge from our recipes book example and create a Swift function so this can better represent a real-world example of two developers changing the same function.
This function is a simple function that takes no parameters and returns a random double.
In the GitHub Desktop app, commit these changes with the commit message, “Generates random double to eventually generate a random recipe of the day.”
Next, create a new branch called “new-random-number-generator.” Update the function to take a parameter of upperValue and replace the hardcoded number here. Commit this change with the message, “Updated random number generator to take an upper value instead of the hardcoded value.” Push these changes to the remote.
Switch to the main branch. You’ll now update the generate random number function again. Replace the double to return an integer instead.
Commit these changes with the message, “Replaced double with int. The generate random number function should have always returned an integer, not a double to return a random recipe of the day.”
Now you have a function that’s modified on two different branches. They were both made by you, but in a real-world project, conflicts will most likely arise when multiple different developers update the same part of the code base. Either way, let’s see how to resolve those conflicts.
In the GitHub Desktop app, go to Branch, merge into current branch. The GitHub Desktop app asks you what branch you’d like to merge into the current branch. Select the branch you pushed to the remote, “new-random-number-generator.”
Git notifies you that you will have conflicts. Select “Create a merge commit.” This is the simplest of the merge options and is what we’ll be going over in this course.
Now, in order for Git to proceed with this commit, it needs you to resolve the conflicts. You can open the file in any editor, but I’ll continue to use VS Code. Your view may look different than mine if you are using VS Code depending on what extensions you have installed.
Now, if you look at the file where the conflict occurred, Git added these conflict markers so you can easily find what you need to resolve. The equals is the divider between the two commits. Above is the current change, the change on your current branch. The bottom is the incoming change, the change from the branch we are choosing to merge into the current branch.
Git doesn’t know what change is correct, so it asks you to resolve it. In this case, we know we actually want both changes, and choosing just one of the changes would be incorrect. Just to show you that the conflict markers are nothing to be scared about, you can just start editing the file how you want. Delete the conflict markers, and update the function to take both the upperValue parameter, and the return type of int.
Now, return to the GitHub Desktop app. Git recognizes that you resolved the conflict. Click “Continue merge.” Lastly push your changes.
This is what we just did in our Git commit tree. This is the commit where you first created the generate random function. This is the commit where you changed the function to take in an upperValue. And this is the commit where you changed double to int. When you resolve the conflicts and make your merge commit, Git will add a new commit to the end of the tree here.