Skip to main content

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

Section 5.1 Understanding Merge Conflicts

The exercises in this section provide practice in identifying merge conflicts and resolving them.

Subsection 5.1.1 Merge Example 1

Consider Figure 5.1.1. This figure shows a feature branch (in the left panel) that is to be merged into the current main branch (in the right panel). The center panel shows the best common ancestor from which both the feature branch and main branch have diverged.
The current main branch contains changes, merged by the maintainers, to fix several bugs that existed in the best common ancestor. The feature branch has been changed to use more descriptive variable names, but has not fixed the bugs.
Three panels of code (feature branch, best common ancestor, main branch)
Figure 5.1.1. Example merge conflicts with Best Common Ancestor.

Exercises

1.
    Study the program shown in the main branch of Figure 5.1.1. Then choose the statement below that best describes the computation the program is trying to perform. Note: The Feature Branch and the Best Common Ancestor both contain bugs.
  • Find the average of a list of numbers.
  • Correct.
  • Find the total of a list of numbers.
  • Consider the last line in the main branch.
  • Count how many numbers the user enters.
  • What does the program do besides count the numbers?
  • None of these.
  • One of the other answers is correct.
2.
When both the feature branch being merged and the main branch that it is being merged into contain changes with respect to the best common ancestor, those changes can be non-conflicting or conflicting.
(a)
    Select the statement below that best describes a non-conflicting change.
  • A non-conflicting change occurs when a part of the code changes in the feature branch or in the main branch but not in both.
  • Correct.
  • A non-conflicting change occurs when a part of the code changes in both the feature branch and the main branch.
  • Review the definitions of non-conflicting and conflicting changes in the class slides.
  • A non-conflicting change occurs when the best common ancestor is modified.
  • The best common ancestor cannot be changed, all changes are either in the feature branch or the main branch.
  • A non-conflicting change occurs when part of the feature branch contains only one change.
  • You need to consider changes to both the feature branch and the main branch to determine if a change is non-conflicting.
(b)
    Select the statement below that best describes a conflicting change.
  • A conflicting change occurs when a part of the code changes in the feature branch or in the main branch but not in both.
  • Review the definitions of non-conflicting and conflicting changes in the class slides.
  • A conflicting change occurs when a part of the code changes in both the feature branch and the main branch.
  • Correct.
  • A conflicting change occurs when the best common ancestor is modified.
  • The best common ancestor cannot be changed, all changes are either in the feature branch or the main branch.
  • A conflicting change occurs when a part of the feature branch contains multiple changes.
  • You need to consider changes to both the feature branch and the main branch to determine if a change is conflicting.
3.
The questions below will now ask you to identify the non-conflicting and conflicting changes that exist in the merge from Figure 5.1.1.
(a)
Hint.
Non-conflicting changes occur when a part of the code changes in either the feature branch or the main but not both.
(b)
Hint.
Conflicting changes occur when the same part of the code is changed both the feature branch and the main branch.
4.
    Select the statement below that best describes why the feature branch in Figure 5.1.1 would, or would not, be able to be merged automatically by the project maintainers.
  • The feature branch cannot be merged automatically because there is a conflicting change.
  • Correct. The last line contains changes in both main and the feature branch.
  • The feature branch can be merged automatically because all of the changes are non-conflicting.
  • Are you sure there are no conflicting changes?
  • The feature branch cannot be merged automatically because there are non-conflicting changes.
  • Non-conflicting changes can (usually) be merged automatically.
  • It is not possible to tell if the feature branch can be merged automatically.
  • If there are conflicting changes then the feature branch will not be able to be merged automatically.
  • The feature branch cannot be merged automatically because there are changes both in main and in the feature branch.
  • Close. What type of changes prevent automatic merges?
5.
Hint.
Select all of the non-conflicting changes and choose the conflicting change that ensures that the program will work correctly.

Subsection 5.1.2 Merge Example 2

Figure 5.1.2 shows another example of a contributor’s feature branch that is to be merged into the main branch.
As in the previous example, the right-hand panel shows commits that the maintainers have merged into the main branch to fix a bug that existed in the program. The left-hand panel shows the contributor’s feature branch with changes that also fix the bug, but in a different way. The center panel shows the best common ancestor of the feature and main branches.
Three panels of code (feature branch, best common ancestor, main branch)
Figure 5.1.2. Example merge conflicts with Best Common Ancestor.

Exercises

1.
    Study the program in Figure 5.1.2. Then choose the statement below that best describes the computation the program is trying to perform. Note: The ** operator indicates exponentiation, so x**2 computes x squared.
  • Compute the area of a circle.
  • Correct.
  • Find the circumference of a circle.
  • Close. Notice the use of ** to square the radius r.
  • Compute the volume of a sphere.
  • Close, but that formula would use r**3.
  • None of these.
  • One of the other answers is correct.
Hint.
The main branch fixes the bug in a strange way. So focusing on the feature branch may be easier.
2.
The questions below will now ask you to identify the non-conflicting and conflicting changes that exist in the merge from Figure 5.1.2.
(a)
Hint.
Non-conflicting changes occur when a part of the code changes in either the feature branch or the main but not both.
(b)
Hint.
Conflicting changes occur when the same part of the code is changed both the feature branch and the main branch.
3.
    Select the statement below that best describes why the feature branch in Figure 5.1.2 would, or would not, be able to be merged automatically by the project maintainers.
  • The feature branch can be merged automatically because all of the changes are non-conflicting.
  • Correct.
  • The feature branch cannot be merged automatically because there is a conflicting change.
  • Are there any conflicting changes?
  • The feature branch cannot be merged automatically because there are non-conflicting changes.
  • Non-conflicting changes can (usually) be merged automatically.
  • It is not possible to tell if the feature branch can be merged automatically.
  • If there are conflicting changes then the feature branch will not be able to be merged automatically.
  • The feature branch cannot be merged automatically because there are changes both in main and in the feature branch.
  • What type of changes prevent automatic merges?
4.
    Imagine that the non-conflicting changes you identified in Task 5.1.2.2.a are automatically merged. Select the statement below that best describes the result.
  • The merged program will not work correctly because the formula for the area will be incorrect.
  • Correct.
  • The merged program will work correctly because automatic merge’s are guaranteed to produce a correct program.
  • Look closely at the lines that define rsq and area.
  • The merged program will not work correctly because an incorrect value of pi will be used.
  • 3.1415927 is a valid approximation of pi.
  • The merged program will work correctly because rsq is will be correctly computed as r squared instead of r*2.
  • Look closely at how the area will be computed.
5.
    When both the feature branch and the main branch function correctly, and the merge contains no conflicting changes then the result of an automatic merge will also be correct.
  • True.

  • False.

Hint.
Consider what happened with Task 5.1.2.2.a where there were no conflicting changes.
You have attempted 1 of 14 activities on this page.