Merge Conflicts
As you’ll recall from the last post, when we tried to cherry-pick our bug fix from the feature branch into the main, Git wasn’t sure what we wanted the finished file to look like and forced us to tell it ourselves. That shouldn’t have happened in the particular situation we dealth with last time, but today, we’ll be looking at situations where we would actually expect that to occur.
Merge Conflicts
When moving code from one branch of a repository to another, a merge conflict occurs when there have been commits to both branches that change the same line of the same file since the last time the branches were synced with each other. In this situation, Git needs to be told which version of that line you want to keep in the final result.
Log into GitHub.com and create a new private repo initialized with a README file. The README will be initialized with a heading containing the name of your new repo. After creating the repo, click the pencil in the top-right corner of the README to edit it.
Add a few lines of code as shown below, and commit the changes directly to the main branch.
Go back to the main page of the repo, click the branch dropdown, and create a new branch called some-feature.
Edit lines 5 and 9 of the README file in the some-feature branch as seen below and commit directly to the some-feature branch.
Now we will simulate changes made by another developer on the project. Switch back to the main branch and edit lines 5 and 9 of the README again, putting different content than what was in the feature-branch version of the file, and commit directly to the main branch.
Go back to the main page of the repo. At the top of the page, you will see a notice that “some-feature had recent pushes <amount of time> ago.” Click the “Compare & pull request” button next to this notice.
This will take us to the pull request creation form. Notice at the top of the screen there is a warning that the merge can’t be done automatically. Add a short description to the pull request and click “Create pull request.”
After creating the pull request, we will again see the warning that the branch we are attempting to merge has conflicts with the main branch that can’t be resolved automatically. Click the “Resolve conflicts” button on the right-hand side of this notice.
This will take us to the conflict resolution view. The start of the conflicted section is marked by <<<<<<< some-feature. This tells us that the lines directly below this are the version of the conflicted file as it appears on the some-feature branch. The end of that version is marked by =======. The end of the conflicted section is marked by >>>>>>> main. This tells us that the lines directly above that are the version of the conflicted file as it appears on the main branch. Note that GitHub.com treats these as a single conflicted section. Other versions of Git repositories would have marked the changes on line 5 and the changes on line 9 as two separate conflicted sections, leaving the unchanged line 7 unmarked.
Now we can manually edit the file to the final version we want to merge onto the main branch. In this particular case, we’ll keep only the version from the some-feature branch. We could also, if we wanted to, keep only the version from the main branch, keep both versions, or manually create a hybrid of the two versions. Delete the lines we don’t want to keep, then remove the conflict markers <<<<<<< some-feature, =======, and >>>>>>> main. This allows Git to recognize when the conflict has been resolved. Click “Mark as resolved” in the top-right corner of the editor. Then click the green “Commit merge” button that appears above the “Mark as resolved” button.
This takes us back to the pull request, where we can now see that there are no conflicts remaining and the merge is able to proceed. To merge our changes, click “Merge pull request,” and then click the “Confirm merge” button that appears.
Comments
Post a Comment