Skip to main content

GitKit (2nd ed.): Learn git and GitHub in Context

Section 5.5 Resolving a Merge Conflict

Figure 5.5.1 shows the main branch has committed changes that conflict with what the local branch wants to commit.
Image illustrating changes merged into the upstream that conflict with a local branch.
Figure 5.5.1. Resolving a Merge Conflict
This section will walk you through the process of resolving the merge conflict. In Section 5.4 Merging the main Branch into a Feature Branch you merged the main branch into your feature branch. This created a merge conflict. In this section you will edit the raw merge conflict information to resolve the conflict and then you will stage and commit the merged changes.

Subsection 5.5.1 Raw Merge Conflict Information

After completing Section 5.4 the README.md file should be open in your editor and the raw merge conflict information should be displayed.
If you are not seeing the README.md file open it from the EXPLORER pane. If you are not seeing the merge conflict information, scroll through the README.md file to the location where you made your change. The raw merge conflict information should appear there. If it does not, return to Section 5.4 and try to correct the issue.

Exercises

1.
The editor window for the README.md file displays the raw conflict information that has resulted from the merge. The following tasks ask you some question about this information.
(a)
(b)

Subsection 5.5.2 Resolving the Merge Conflict

Recall that the merge conflict that you are seeing arose because your feature branch and the upstream main branch contain changes to the same part of the file. In this situation, Git is not able to automatically decide which changes to keep or how to combine the changes. This decision must be made by a developer, in this case... by you!
It is possible to resolve merge conflicts by editing the raw merge information, including removing the chevrons and dividers. However, most development environments, including the one you are using, provide special tools to help with the resolution of merge conflicts.
The basic merge tool in your development environment provides four options to assist with resolving merge conflicts. These options appear in the editor just above the merge conflict information as shown in Figure 5.5.2.
Image illustrating the basic merge tool options of "Accept Current Change", "Accept Incoming Change", "Accept Both Changes", "Compare Changes".
Figure 5.5.2. The basic merge tool options.

Exercises

1.
Hint.
Compare the terms used in the merge tool options to the information displayed in the first and last lines of the merge conflict information.
2.
    Click the option that will accept the changes in your feature branch.
    Choose the description below that best describes What happens?
  • Only the changes from the feature branch remain.
  • Correct.
  • Only the changes from the main branch remain.
  • Check the editor contents after you click "Accept Current Change".
  • The changes from the main branch and the chevrons remain.
  • Check the editor contents after you click "Accept Current Change".
  • The changes from the feature branch and the chevrons remain.
  • Check the editor contents after you click "Accept Current Change".
Hint.
Click "Accept Current Change" and check the editor contents.
If you would like to try out the other merge options you can undo your changes by opening the Hamburger menu (☰), clicking on "Edit" and choosing "Undo".
This basic merge tool is helpful for resolving small uncomplicated merge conflicts. Most development environments also provide more advanced merge tools for working with more complex conflicts. The use of those tools is beyond the goals of this chapter.

Subsection 5.5.3 Committing the Merged Changes

Exercises

At this point you have resolved the merge conflict, but the resulting changes have not been committed to your feature branch. In this section you will commit those changes to your feature branch.
1.
Run the git status command in the terminal and use its output to answer the following questions.
(a)
    Which of the following appear in the output of the git status command and indicate that your merged changes have not yet been committed to your feature branch?
  • You have unmerged paths.
  • Correct.
  • both modified: README.md
  • Correct.
  • no changes added to commit
  • Correct.
  • All conflicts fixed
  • Check the output again.
  • Your branch is behind ’origin’
  • Check the output again.
Hint.
Run the git status command and read the output carefully.
(b)
    Imagine that after you completed the merge you realize that you didn’t merge the changes the way you had intended. Which Git command could you use to undo your merge?
  • git merge --abort
  • Correct.
  • git merge --undo
  • Close. Check the output again.
  • git undo merge
  • Check the output again.
  • git merge revert
  • Check the output again.
Hint.
Run the git status command and read the output carefully.
Feel free to try out the git merge --abort command. After you run it, the raw merge conflict information will reappear in the README.md file. Be sure to re-merge the changes from your feature branch before moving on.
2.
From Task 5.5.3.1.a you can see that you now have uncommitted changes. Those changes are the changes you made when resolving the merge conflict. The next step is to commit your changes.
Choose and order the commands that you would use to commit your changes to your local repository. Note: Not all commands will be used.
Hint.
See Section 3.5 and Section 3.6 for a review.
3.
    Run the first command from Exercise 5.5.3.2 in the terminal. Then run the git status command and examine its output.
    What does git status tell you to do to conclude the merge?
  • use "git commit" to conclude merge
  • Correct. You already knew that, but its good to know that git status provides helpful tips. Use it often!
  • use "git pull" to conclude merge
  • git pull performs a different function and will not conclude the merge.
  • use "git push" to conclude merge
  • git push performs a different function and will not conclude the merge.
  • use "git restore" to conclude merge
  • git restore performs a different function and will not conclude the merge.
Hint.
Run the git status command and read the output carefully.
4.
    Run the second command from Exercise 5.5.3.2 in the terminal. Be sure to replace the <message> placeholder with a meaningful commit message. Then use git status and examine the output.
    What does git status tell you that indicates that your changes have been committed?
  • nothing to commit, working tree clean
  • Correct.
  • merge complete
  • True, but that’s not the message that is displayed.
  • changes successfully merged
  • True, but that’s not the message that is displayed
Hint.
Run the git status command and read the output carefully.
You have attempted 1 of 10 activities on this page.