Managing Merge Conflicts | Bondar Academy
Course: Git and GitHub Fundamentals
Module: Working with Git and GitHub
Instructor: Artem Bondar
Lesson Summary
Merge conflicts are a common challenge when working with Git . They occur when Git cannot determine the correct version of code from different branches. This lesson demonstrates how to resolve merge conflicts using Visual Studio Code . Understanding Merge Conflicts A merge conflict arises when multiple contributors make changes to the same line of code in different branches. In this example: Artem creates a branch called Artem's change and modifies a line of code. A second user creates a branch called third line and makes a conflicting change to the same line. Resolving Merge Conflicts When attempting to merge the second user's branch into the master branch, Git identifies a conflict: Artem's pull request merges successfully. The second user's pull request fails with a message indicating a conflict. Steps to Resolve the Conflict: Run the command: git merge origin master Open the merge editor in Visual Studio Code to view the conflict. Choose one of the following options to resolve the conflict: Accept current changes (keep changes from the working branch). Accept incoming changes (keep changes from the master branch). Manual edit to combine changes from both contributors. Complete the merge and synchronize changes. After resolving the conflict, the remote repository will reflect the new commit, and the master branch will be updated without any conflicts. Visual Studio Code's merge conflict editor simplifies this process, allowing users to easily choose the desired code version.
Video Transcript
Merge conflict is the most annoying part of working with a Git, but unfortunately, all of us time to time face with this situation. So merge conflict is when Git cannot decide which is the ultimate source of truth, which code from which branch has the ultimate source of truth, and ask us to resolve this conflict. In this lesson, I will show you how to use Visual Studio Code to resolve such conflicts. Let's jump into it. All right. So let me show you one more example how to resolve merge conflicts. For this demonstration, I added myself to our test project as a second contributor. Then I'm contributing a different change to the test file into this project. So I have created Artem's change branch. I have modified third line of code with this change, and push this change to the remote repository. Let me show you this in remote repository. This is our project. We have two branches, and there is a second branch which was pushed by me. So view branch activity. You see that Bonder Artem created this branch. Then our second test user is working on the same application, and doesn't know anything that Artem has changed this line, and also have plans to change this line. So let's create a new branch, create a new branch, and let's call it third line, for example. And now my test user wants to update this line as well. This is update from the test user. And this user creates a commit, third line updated, and push those changes to remote repository. So let's go back and validate that all changes are on a remote repository. Refresh. We have three branches so far, and here we go. So the branch of the test user is this one. Artem's branch is this one. And now we want to create a pull request to merge those changes into the master branch. Since Artem completed his job first, he creates a pull request. Create pull request. Pull request. Git validates that there are no merge conflicts, merge pull request, and confirm merge. And this merge was successful. If we navigate back to the text file, we see the update from Artem's is here. But now if we will try to perform a pull request for the second branch that was updated by another user. The second branch. I click create pull request. And we can see a message can't automatically merge, because Git already identified that there is something going on. I click create pull request. And Git says this branch has conflict, you must resolve. I click on resolve conflicts. And Git just showing us the difference what this conflict is about. Third line branch has this change. And master branch already have this change. And because Git cannot identify which of those lines should be used as the ultimate source of truth, Git asking us to resolve this merge conflict manually. And this is what we're going to do. So going back and going back to our project. So in order to resolve this merge conflict, we need to merge the origin master branch into our working branch. And I type command git merge origin master and hit enter. And also Visual Studio Code immediately showing us that there is a merge conflict. I open the resolve in merge editor. And now editor shows us what this merge conflict is about. So incoming change is something which is coming from the origin master branch. And origin master branch has this change. Artem changed this line. Our current branch that we are working on, which is third line branch, has this code. And we have a few options. Option number one is accept the current changes. And then this change will be accepted as a resolution. Option number two is accept incoming change. So the version from the master branch will be accepted as an ultimate resolution. And the option number three, we can manually provide some other third change over here. How do we want this phrase should look like? And just write the code over here to resolve this merge conflict. So let's say I want to accept the change from the master branch and resolve this merge conflict from the Artem changed this line. Accept. But I would like to add extra modification. Artem changed this line and test user. Something like this. That two people contributed to this change. And I click complete merge. That's it. Also Visual Studio Code created for us automatically a commit message. So we just click commit and synchronize those changes. That's it. Now going back to remote repository and what we see, we don't have a merge conflict anymore. And we have a new commit created, which is merge remote tracking branch origin into the third line branch. And we can see how this commit was resolved. Artem changed this line and test user. This is the ultimate resolution. Now we can safely go back and perform merge of pull request. Merge conflict was successfully resolved. Opening test file. Here we go. Master branch was updated. Now we can go back to the project. Switch to the master branch. Update. And our master branch on the local computer synchronized with the remote master branch. So that's it, guys. This is how you resolve merge conflicts. Visual Studio Code provides pretty good merge conflict editor. And you can easily figure out what is the latest version of the code you want to choose in your commit. All right. That's it, guys. And see you in the next lesson.