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 all of the bugs.
Feature Branch Best Common Ancestor main Branch
total=0 tot=0 tot=0
count=0 count=0 count=0
read n read n read n
while count > n: while count > n: while count < n:
    read m     read m     read m
    total=total+m     tot=tot+m     tot=tot+m
    count--     count--     count++
average=total ⁄ count ave=count ⁄ tot ave=tot ⁄ count
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.
Hint.
The statements in the loop are indented, whereas the statement that will happen after the loop has the same indentation as the word while. Think about what is being repeated and how those values are being used after the loop.
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.
Hint.
Refer to the class slides for the definitions of non-conflicting and conflicting. Then go back to Figure 5.1.1 and look at the changes made in both branches.
(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.
Hint.
Refer to the class slides for the definitions of non-conflicting and conflicting. Then go back to Figure 5.1.1 and look at the changes made in both branches.
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 feature branch can be merged automatically because all of the changes are non-conflicting.
  • Are you sure there are no conflicting changes? Refer back to Task 5.1.1.3.b.
  • 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.
  • It is possible to tell by comparing the changes between the feature branch and the main branch.
  • The feature branch cannot be merged automatically because there are changes both in main and in the feature branch.
  • You are close. What type of changes prevent automatic merges?
Hint.
Think about which type of changes can be automatically merged and which cause merge conflicts. Then refer to Figure 5.1.1 to see which type(s) of changes it contains.
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.
Feature Branch Best Common Ancestor main Branch
r=15 r=15 r=15
pi = 3.1415927 pi = 3.14 pi = 3.14
rsq = r**2 rsq = r*2 rsq = r*2
a = pi*rsq a = pi*rsq area = pi*(rsq/2)**2
print a print a print area
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 volume of a sphere.
  • You are close, but that formula would use r**3.
  • Compute the area of a circle.
  • Correct!
  • Find the circumference of a circle.
  • You are close. Notice the use of ** to square the radius r.
  • 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.
  • Refer back to your previous answers and think about whether there are 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.
  • Refer back to your previous answers and think about whether there are any conflicting changes.
  • 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?
Hint.
Think about which type of changes can be automatically merged and which cause merge conflicts. Then refer to Figure 5.1.2 to see which type(s) of changes it contains.
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 correctly computed as r squared instead of r*2.
  • Look closely at how the area will be computed.
Hint.
Merged changes are not always correct. It is important to look closely at the code that is being accepted to determine if the changes fix any problems or introduce new problems.
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
  • Think about the code merged in the previous example.
  • False
  • Correct!
Hint.
Consider what happened with Task 5.1.2.2.a where there were no conflicting changes.
You have attempted of activities on this page.