Exercises

Here is a collection of exercises to complement the Version Control book. These exercises can be used flexibly throughout a course about Git.

Command Line

Create files and folders

  1. Within your chosen folder, create a new subfolder and name it recipes.
  2. Navigate into the recipes folder.
  3. Create a new file and name it recipes.txt.
  4. Confirm the creation of the file by listing the contents of the recipes folder.

Which commands did you use?

  1. Create a folder called recipes:
Code
mkdir recipes
  1. Navigate into the recipes folder:
Code
cd recipes
  1. Create a file and name it recipes.txt:
Code
touch recipes.txt
  1. List the contents of the recipes folder:
Code
ls
  1. In your home directory, create a new directory called projects.
  2. Navigate into the projects directory.
  3. Create three subdirectories: project1, project2, and project3.
  4. List all directories in the projects directory.

Write and opening files

Add the title “My Favorite Recipes” to recipes.txt. Which command or method did you use?

I added the title to recipes.txt by using the following command:

Code
echo "My Favorite Recipes" > recipes.txt

This command writes “My Favorite Recipes” to the file, overwriting any existing content.

Create multiple files

Go back to your main course-related folder. Using the command line, create three new files: notes.txt, assignments.txt, and schedule.txt. Provide the command you used.

Code
touch notes.txt assignments.txt schedule.txt

Use wildcards

Use a wildcard pattern to list all .txt files in your folder. Which command did you use?

Code
ls *.txt

Bonus exercise

Imagine, that you are in a folder containing multiple text files with various extensions, including .txt, .md, and .docx. You want to move all the .txt files to a subfolder named “text_files” while keeping the other file types in the current directory. Write a series of command-line commands to accomplish this task, including creating the “text_files” subdirectory. Explain each step in your solution.

  1. Creating the “text_files” subfolder:
Code
mkdir text_files
  1. Moving all the .txt files to the “text_files” subfolder:
Code
mv *.txt text_files

Setup

Configure Git

  1. Open your terminal.
  2. Set your Git username.
  3. Set your Git email address.
  4. Change the default name of the initial branch to main
  5. 🚀 Optional: Change your default text editor.

First steps with Git

Initialize a Git repository

  1. Create a folder called recipes.
  2. Navigate to the recipes folder in your terminal.
  3. Initialize a new Git repository in the recipes folder.

Add content and commit changes

  1. Create a new file called recipes.txt.
  2. Add a short recipe to recipes.txt (any favorite or an intriguing AI-generated one).
  3. Stage the new recipes.txt file.
  4. Commit the changes in recipes.txt with a descriptive commit message.
  5. 🚀 Optional: Commit at least three additional changes in recipes.txt.

Amend a commit

  1. Make additional changes to your recipes.txt file.
  2. Stage the changes.
  3. Amend the previous commit to include the new changes.

For example, add a recipe without a title first, commit, then add a title and amend the previous commit to add the title change to the same commit.

Create a .gitignore file

  1. Create a .gitignore file.
  2. Create a random file that you want to ignore, for example random-file.docx.
  3. Add the random file to the .gitignore file, stage and commit your changes.
  4. Save, stage and commit the .gitignore file.
  5. 🚀 All macOS users: Let your repository ignore .DS_Store.

Branches

Create and merge a new branch

  1. Create a new branch called feature.
  2. Switch to your new branch.
  3. Add a new recipe to your recipes.txt file.
  4. Stage and commit the changes to your recipes.txt file on the feature branch.
  5. Switch back to the default branch (main or master).
  6. Merge the feature branch with your default branch.
  7. Delete the feature branch.

Create and resolve a merge conflict

  1. Deliberately create a merge conflict by editing the same section of a file on two separate branches and attempting to merge them. An example can be found in the branches chapter.
  2. Resolve the merge conflict.
  3. Delete the feature branch afterwards.

GitHub: Intro

Connect to GitHub

  1. Generate a personal access token (PAT) on GitHub.
  2. Use the PAT for GitHub authentication.

Upload your local repository to GitHub

  1. Create an empty repository on GitHub (do not initialize the repository with any files).
  2. Set the remote URL of your local repository to your GitHub repository.
  3. Push the changes on your main branch to GitHub.

Create a README.md file on GitHub

  1. Navigate to your repository on GitHub, click “Add file”, and select “Create new file”.
  2. Name the file README.md, add a brief description, and provide a commit message.
  3. 🚀 Optional: Play around with Markdown syntax.
  4. Click the green “Commit new file” button to save the README.md file to the repository.
  5. Pull the changes to your local repository.

Fetch and merge your changes

  1. Run git fetch to retrieve the latest changes from the remote repository.
  2. Run git merge origin/main to merge the fetched changes into your local main branch.
  3. Verify that your local repository now includes the changes made on GitHub.

“Private” collaboration with pull requests (using “GitHub Flow”)

  1. Add your partner (desk neighbor) as a collaborator to your recipes repository.
  2. Clone your partner’s repository.
  3. Create a new branch in your partner’s repository.
  4. Add a recipe to your partner’s recipe.txt file.
  5. Add and commit the changes.
  6. Push the changes on the new branch to GitHub.
  7. Create a Pull Request.
  8. Review the PR your partner made in your repository.
  9. Merge the PR into your repository.

Manage branches

  1. Push another branch (other than the default branch) with changes to GitHub.
  2. Delete any unused or merged branches.

Clean up your repository on GitHub

  1. Delete unnecessary files or add a new recipe directly on GitHub.
  2. Commit changes on GitHub with a message.
  3. Pull the changes to your local repository.

Clone and sync your repo

  1. Clone your repository from GitHub to a different location on your computer.
  2. Stage and commit changes in the new location (consider using a new branch).
  3. Push these new changes to GitHub.
  4. Pull the changes to the repository in the original location.
  5. Delete your newly cloned repository.

GitHub: Advanced

“Public” collaboration with pull requests (using a fork and GitHub flow)

  1. Find out what forking is
  2. Fork the recipes repository of another course participant (who is not your partner from the previous exercise)
  3. Create an issue, suggesting a missing recipe
  4. Create an issue in your partner’s repository (maybe their repo is missing a great recipe?)
  5. Repeat the steps from the previous exercise using the forked repository:
    1. Clone the forked repository into a sensible location on your computer
    2. Create a new branch and create one or multiple commits “fixing” the issue that you opened
    3. If available, follow the contributing guide of your partner’s repository
    4. Push your changes to GitHub
    5. Create a pull request with your changes (hint: from the fork to the original repo) and refer to the issue in your pull request

Review pull requests in your repository

  1. View any pull requests that are created in your recipes repository.
  2. Review the changes made by the contributor in the pull request.
  3. If needed, discuss additional changes with the contributor in the pull request.
  4. Close the pull request by merging the proposed changes.

Practice collaborative workflows on GitHub

  1. Repeat the entire forking workflow (see task 1 above) with Lennart’s recipes repository.

Protect your branch

  1. Go to your repository’s “Settings” and select “Branches”.
  2. Under “Branch protection rules”, click “Add rule” and enter “main” in the “Branch name pattern” field.
  3. Enable the desired protection options (for example, require pull request reviews) and click “Create” to save the rule.

Tags and Releases

Create a tag

  1. Create a lightweight or an annotated tag named v1.0.0.

Create a GitHub Release

  1. Go to your repository on GitHub.
  2. Click on “Releases” then “Draft a new release”.
  3. Choose the tag v1.0.0 from the list or create a new tag.
  4. Fill in the release title and description.
  5. Click “Publish release”.

Graphical User Interfaces

Install a Git GUI

  1. Install a Git GUI
  2. Login to the client using your GitHub account.
  3. View your recipes repository in the Git GUI.

Open a Git repository in RStudio

  1. Open your recipes repository in RStudio.
  2. Edit the recipes.txt or recipes.qmd file by adding or modifying a recipe.
  3. Commit your changes using RStudio’s Git interface.

Stashing and Co.

Stashing

  1. Create a new branch called feature/stash-exercise.
  2. Make changes to two different files in your project directory.
  3. Stash your changes without adding a message.
  4. Use git status to verify that your working directory is clean.
  5. Apply the stash to your working directory and verify that your changes are restored.

Reverting

  1. Create a new branch called feature/revert-exercise.
  2. Make changes and commit these to your file.
  3. Use git revert to revert the most recent commit, specifying its hash.

Rebasing

  1. Create a new branch called feature/rebase-exercise.
  2. Make three commits with minor changes on the feature/rebase-exercise branch.
  3. Switch to the main branch and make additional changes.
  4. Rebase the feature/rebase-example branch onto the main branch using git rebase main.
  5. Check the commit history with git log to see the updated order of commits.