Exercises - Traversing the commit tree¶
In order to do these exercises, you need to download the exercises zip file. 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 4.commits
- 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.commits
- git clone
You are now in a directory with 7 subdirectories, one for each exercise.
1. Investigating the commit history¶
Note
The goal of this exercise is to learn to use the git log
command.
Found under git_materials/4.commits/1.log
there is a directory named repository
. Enter it and perform the following steps:
- Investigate the history. How many commit does the
main
branch contain? - Can you make the output of the command cleaner?
- Are there any other branches? How many? What are they called?
- Try to filter the output so that only commits that contain the word “abandon” in the commit message are shown.
- Does the reference log contain anything interesting? Can you see where the
HEAD
was 6 steps ago?
Hints for this exercise
- oneline
- graph
- reflog
2. Recovering the HEAD¶
Note
The goal of this exercise is to learn to identify a “detached HEAD” situation and recover from it.
Enter the repository/
directory under git_materials/4.commits/2.recover_head
and perform the following steps:
- Enter the
repository/
directory and check whether the HEAD is detached. - If the HEAD is detached, return the
HEAD
back to the tip of the branch. - Confirm that that the
HEAD
indeed points to the tip of the branch`
3. Stashing uncommitted changes¶
Note
The goal of this exercise is to learn to checkout a different commit and stash uncommited changes, if necessary.
Enter the repository/
directory under git_materials/4.commits/3.stash
and perform the following steps:
- Identify the first commit (
This is going to be a cake recipe
). - Try to checkout the commit in order to identify the initial title of the recipe. If necessary, stash the changes. What was the initial title of the recipe?
- Restore the
HEAD
and pop the changes from the stash. - Confirm that the changes were applied correctly and that the stash is empty.
4. Discarding the latest commit¶
Note
The goal of this exercise is to learn to discard the latest commit.
Enter the repository/
directory under git_materials/4.commits/4.discard
and perform the following steps:
- Discard the latest commit. Also discard the changes in the working tree.
- Confirm that the commit is no longer reachable from
master
. Confirm that therecipe.txt
no longer contains the section “Other ideas and hints”. - Try to restore the discarded commit. (optional)
5. Amending previous commit¶
Note
The goal of this exercise is to learn to amend the latest commit.
Enter the repository/
directory under git_materials/4.commits/5.amend` and perform the following steps:
- The ingredient list in the
repository/recipe.txt
file is missing an ingredient. Add two eggs to the ingredient list. - Stage the changes and amend the previous commit (
Add remaining ingredients and directions
). - Validate the operation. The log (
git log
) should contain three commits and the most recent commit (Add remaining ingredients and directions
) should contain the new added ingredient (git show <commit>
).
6. Revert the latest commit¶
Note
The goal of this exercise is to learn to revert the latest commit.
Enter the repository/
directory under git_materials/4.commits/6.revert
and perform the following steps:
- Revert the latest commit.
- Confirm that the commit tree indeed contains the revert commit. Confirm that the
recipe.txt
file no longer contains the section “Other ideas and hints”.
7. Proper commit workflow¶
Note
This exercise is meant to demonstrate the proper workflow for making multiple commits from a single set of edits.
The overall goal is to make sure that each commit produces a valid revision. This type of workflow is beneficial when working with source code since each revision should both compile and function correctly.
The workflow is not that beneficial with the the example below but it is used as an example only.
Enter the repository/
directory git_materials/4.commits/7.workflow
and perform the following steps:
- You can confirm (
git diff HEAD
) that therepository/recipe.txt
file contains uncommitted changes. That is, the recipe have been converted to metric system. - You can also confirm (
git diff --cached
) that some of these changes have been already staged.
As you can probably guess, the first commit we are creating is going to convert the measurements to metric.
The changes that are related to the pan size and the oven temperature (8th step the the directions) are going to be committed separately. - Store the the unstaged changes to the stash:
The --keep-index
options tells Git to keep the staged changes. Otherwise Git would stash them.
4. Now, investigate the content of the repository/recipe.txt
file. You can see that the 7th step in the directions contains an unconverted measurement.
This means that whoever created this exercise forgot to stage this line.
This would correspond to a situation where the source code does not compile or does not function correctly.
5. Pop the unstaged changes from the stash:
Remember to do partial staging (Lecture 2: Basic commands).
Verify the repository/recipe.txt
file.
7. Commit changes.
8. Pop the unstaged changes, and stage and commit changes that are related to the pan size and the oven temperature.
This is going to be the second commit.
Confirm that the two commits you created indeed contain the desired changes.