Exercises - Branches, merges, and conflicts¶
In order to do these exercises, you need to download the exercises zip file (if you already did so for the previous exercise, you do not need to do so again, of course).
You can do that either by cloning the repository or by just getting the zip file with wget
.
- Do ONE of the following:
- git clone
git clone https://github.com/hpc2n/course-intro-git.git
cd course-intro-git
unzip git_materials.zip
cd git_materials
cd 5.branches
- Fetch with wget
wget https://github.com/hpc2n/course-intro-git/raw/refs/heads/main/git_materials.zip
unzip git_materials.zip
cd git_materials
cd 4.branches
- git clone
You are now in a directory with 5 subdirectories, one for each exercise.
1. Merging two local branches¶
Note
The purpose of this exercise is to test the command git merge
and see that the merge goes well in this case.
Situation: The ingredient list in branch “master” has an error in the ingredients which is fixed in the branch “fixed-recipe”.
Start by making sure you are in the directory git_materials/5.branches/1.merge-ok/recipes
.
- First do
git status
to look at the status. You can also rungit log
so you can compare before and after merging. - Now try to merge the two branches. You will see that it merges with a “fast-forward” merge.
NOTE: Remember to check that you are on the right branch! Usegit branch
to check.
Merging the branch “fixed-recipe” to the “master” branch: - See that the merge goes well, and that git reports using “fast-forward” merge.
- Do
git log
andgit status
after the merge and compare what you got before. - Think about why git could merge the two braches automatically and why it used “fast-forward” merge.
2. Merging two local branches, recursive¶
Note
In this exercise you will again try the command git merge
and it should again go well. However, this time git will do a recursive merge, or in newer version an “ort” merge.
Situation: The ingredient list in branch “master” has an error in the ingredients which is fixed in the branch “fixed-recipes”. After that fix, a small change was made to the recipe in the “master” branch.
Start by making sure you are in the directory git_materials/5.branches/2.merge-ok-recursive/recipes
.
- First do
git status
to look at the status. Also rungit log
and see the commits that have been made and to which branches. - Now try to merge the two branches. You will see that the merge happens with “recursive” merge.
NOTE: Remember to check you are on the right branch before you try to merge!
Merge the branch “fixed-recipe” to the “master” branch using thegit merge
command. - Notice that the merge goes well and that git reports using “recursive” merge.
- Do
git log
andgit status
after the merge and compare with what you got before.
- Why did git use “recursive” merge?
3. Merging two local branches resulting in a merge conflict¶
Note
This exercise will again feature the command git merge
, but this time the merge will fail and git will give a merge conflict.
Situation: In the branch “metric” we change the recipe to use the metric system for measurements. Then we change back to the “master” branch and add some coffee to that version of the recipe.
Start by making sure you are in the directory git_materials/5.branches/3.merge-bad/recipes
.
- Do a
git status
first and note the result. Rungit log
. You could also look at the output from the longer command:
or with the alias commandgit graph
NOTE: Remember to change to the subdirectory “recipes” first! - Now try to merge the two branches with the
git merge
command and see that a conflict happens.
NOTE: Check withgit branch
to find out if you are on the right branch before trying to merge.
You will get an error similar to this:
$ git merge metric Auto-merging cakerecipe.txt CONFLICT (content): Merge conflict in cakerecipe.txt Automatic merge failed; fix conflicts and then commit the result.
- Use
git log
(including with the above mentioned flags) andgit status
to see where the problems are and see if you can fix the conflict and then reattempt the merge.
4. Rebase two local branches¶
Note
In this exercise you will try to use the command git rebase
. You will see that it succeeds.
Situation: The ingredient list in branch “master” has an error in the ingredients which is fixed in the branch “fixed-recipe”. Then we decide to make a small change to the recipe, and we do it in the “master” branch.
Start by making sure you are in the directory git_materials/5.branches/4.rebase-ok/recipes
.
- First do a
git status
and agit log
and look at the results. Also look at the output at the longer command (git graph):
And save the result somewhere for later comparison. - Now try to rebase the “master” with the new branch, “fixed-recipe”. You will see that the rebase succeeds.
NOTE: Remember to check withgit branch
that you are on the right branch before you try to rebase! - You should now validate the operation with
git log
andgit status
.
Also rungit graph
orgit log
with the following flags: