Here is a collection of hands-on exercises to complement the chapters in the Version Control Book. These exercises can be used flexibly throughout a course about Git but also when you are learning on your own.
Instructions
You can view the exercises by clicking on the boxes in the individual sections that correspond to one of the chapters. Try to solve the exercises on your own first before looking at the solutions. Please note that the solutions do not always implement the tasks exactly, but can also use variations to show that different approaches are possible or so it works automatically. If you want to start each exercise with a clean repository, you can download the repository of the previous solution as a .zip file and start the exercises in this repository.
Identify a folder on your computer where you (want to) keep course-related files. If you don’t have one, choose a suitable location in your file system.
Navigate into the chosen location using the command line.
Use the command line to display the path of your chosen folder. What is the output?
Create files and folders
Within your chosen folder, create a new subfolder and give it a relevant name.
Navigate into the newly created subfolder.
Create a new text file and name it appropriately.
Confirm that the files was created by listing the contents of the folder.
Navigate into the chosen location. Replace /path/to/your/chosen/directory with a path on your computer. A good location might be within your /Documents folder or a dedicated /Projects or /University folder. This can help to easily find course-related files on your computer.
Optional: Use cd (in combination with absolute or relative paths) to navigate into the project subfolder.
2
Set your global Git username using git config --global user.name "Your Name". Replace Your Name with your name and don’t remove the quotation marks.
3
Set your global Git email address using git config --global user.email "your.email@example.com". Replace your.email@example.com with your email address and don’t remove the quotation marks.
4
Change the default name of the initial branch to main using git config --global init.defaultBranch main.
5
Optional: Change your default text editor. In this example, the default text editor is changed to Vim using git config --global core.editor "vim".
Optional: Use cd (in combination with absolute or relative paths) to navigate into the project subfolder.
2
Set your global Git username using git config --global user.name "Your Name". Replace Your Name with your name and don’t remove the quotation marks.
3
Set your global Git email address using git config --global user.email "your.email@example.com". Replace your.email@example.com with your email address and don’t remove the quotation marks.
4
Change the default name of the initial branch to main using git config --global init.defaultBranch main.
5
Optional: Change your default text editor. In this example, the default text editor is changed to Vim using git config --global core.editor "vim".
#!/bin/bash1cd recipes2git init3touch recipes.txt4cat> recipes.txt <<- EOMPasta1. Bring water to the boil in a large saucepan.2. Once the water is boiling, add a good pinch of salt.3. Add the pasta.4. Cook the pasta according to packet instructions until it's "al dente".5. Drain the pasta in a colander.EOM5git add recipes.txt6git commit -m"Add pasta recipe to recipes.txt"
1
If needed, navigate into the recipes subfolder using cd recipes (or a similar path).
2
Initialize a new Git repository in the recipes folder using git init.
3
Create a new file called recipes.txt using touch. Note that you can also use a regular text editor to do this.
4
Add a short recipe to recipes.txt. In this example, cat is used to add text to recipes.txt. This command would also create the file if recipes.txt wouldn’t exist yet. Note that you can also use a regular text editor to do this.
5
Stage the new recipes.txt file using git add.
6
Commit the changes in recipes.txt with a descriptive commit message using git commit.
Code
#!/bin/bash1cd city-guide2git init3touch city-guide.txt4cat> city-guide.txt <<- EOMHamburg Planetarium- It is one of the world's oldest, and one of Europe's most visited planetariums.- It is located in the district of Winterhude, Hamburg, Germany.EOM5git add city-guide.txt6git commit -m"Add Hamburg Planetarium to city-guide.txt"
1
If needed, navigate into the city-guide subfolder using cd city-guide (or a similar path).
2
Initialize a new Git repository in the city-guide folder using git init.
3
Create a new file called city-guide.txt using touch. Note that you can also use a regular text editor to do this.
4
Add a short entry to city-guide.txt. In this example, cat is used to add text to city-guide.txt. This command would also create the file if city-guide.txt wouldn’t exist yet. Note that you can also use a regular text editor to do this.
5
Stage the new city-guide.txt file using git add.
6
Commit the changes in city-guide.txt with a descriptive commit message using git commit.
Optional: Navigate into the project repository using cd (in combination with an absolute or relative path).
2
Make an additional change to the project text file. In this example, echo is used to append text to text file. Note that you can also use a regular text editor to do this.
3
Stage the changes using git add.
4
Amend the previous commit to include the new changes using git commit --amend. In this example, the --no-edit flag is used to amend the commit without changing the commit message.
5
Check the commit history using git log to verify that the last commit message has not changed. Here, we add the --oneline flag to show a concise summary of the past commits.
Code
#!/bin/bash1cd city-guide2echo"- It is housed in a former water tower at the center of Hamburg Stadtpark.">> city-guide.txt3git add city-guide.txt4git commit --amend--no-edit5git log --oneline
1
Optional: Navigate into the project repository using cd (in combination with an absolute or relative path).
2
Make an additional change to the project text file. In this example, echo is used to append text to text file. Note that you can also use a regular text editor to do this.
3
Stage the changes using git add.
4
Amend the previous commit to include the new changes using git commit --amend. In this example, the --no-edit flag is used to amend the commit without changing the commit message.
5
Check the commit history using git log to verify that the last commit message has not changed. Here, we add the --oneline flag to show a concise summary of the past commits.
+ cd recipes+ echo 6. Enjoy!+ git add recipes.txt+ git commit --amend--no-edit[main 761edc7] Add pasta recipe to recipes.txtDate: Fri Dec 13 09:12:52 2024 +00001 file changed, 8 insertions(+)create mode 100644 recipes.txt+ git log --oneline761edc7 Add pasta recipe to recipes.txt
Output
+ cd city-guide+ echo - It is housed in a former water tower at the center of Hamburg Stadtpark.+ git add city-guide.txt+ git commit --amend--no-edit[main 99cd761] Add Hamburg Planetarium to city-guide.txtDate: Fri Dec 13 09:12:52 2024 +00001 file changed, 5 insertions(+)create mode 100644 city-guide.txt+ git log --oneline99cd761 Add Hamburg Planetarium to city-guide.txt
#!/bin/bash1cd recipes2wget-nv-O pasta.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Rigatoni.jpg/320px-Rigatoni.jpg3git status4touch .gitignore5echo"*.jpg"> .gitignore6git status7git add --all8git commit -m"Add .gitignore and ignore all files that end with .jpg"9echo".DS_Store">> .gitignoregit add .gitignoregit commit -m"Ignore .DS_Store"
1
Optional: Navigate into the project repository using cd (in combination with an absolute or relative path).
2
Download an image from the internet using wget. This command download a picture of pasta from the internet and saves the output -O as pasta.jpg. The -nv (or --no-verbose) argument makes the command output less text.
3
Check the state of your repository using git status. Git will notice the new file and list it as untracked.
4
Create a .gitignore file using touch. Note that you can also use a regular text editor to do this.
5
Add *.jpg to the .gitignore file to ignore all files that end with the .jpg file extension. In this example, echo is used to append text to the text file. Note that you can also use a regular text editor to do this. This command would also create the file if .gitignore wouldn’t exist yet.
6
Check the state of your repository again using git status. The new file should disappear from the output and is not listed as untracked anymore.
7
Stage all changes using git add --all. This command should generally be avoided as it may lead to staging and committing changes that should not added to the repository’s history. Here, we use it to demonstrate that only the .gitignore will be staged but not the pasta image file because it is ignored.
8
Commit the changes in .gitignore with a descriptive commit message using git commit.
9
Add .DS_Store to the .gitignore file to ignore it. Again, echo is used to append text to the text file. Note that you can also use a regular text editor to do this. Stage and commit your changes using a descriptive commit message.
Code
#!/bin/bash1cd city-guide2wget-nv-O hamburg-planetarium.jpg https://upload.wikimedia.org/wikipedia/commons/3/3a/Hamburg_Planetarium_10881zh.jpg3git status4touch .gitignore5echo"*.jpg"> .gitignoregit statusgit add --allgit commit -m"Add .gitignore and ignore all files that end with .jpg"echo".DS_Store">> .gitignoregit add .gitignoregit commit -m"Ignore .DS_Store"
1
Optional: Navigate into the project repository using cd (in combination with an absolute or relative path).
2
Make an additional change to the project text file. In this example, echo is used to append text to text file. Note that you can also use a regular text editor to do this.
3
Stage the changes using git add.
4
Amend the previous commit to include the new changes using git commit --amend. In this example, the --no-edit flag is used to amend the commit without changing the commit message.
5
Check the commit history using git log to verify that the last commit message has not changed. Here, we add the --oneline flag to show a concise summary of the past commits.
+ cd recipes+ wget -nv-O pasta.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Rigatoni.jpg/320px-Rigatoni.jpg2024-12-13 09:12:53 URL:https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Rigatoni.jpg/320px-Rigatoni.jpg [22640/22640]->"pasta.jpg"[1]+ git statusOn branch mainUntracked files:(use"git add <file>..." to include in what will be committed)pasta.jpgnothing added to commit but untracked files present (use "git add" to track)+ touch .gitignore+ echo *.jpg+ git statusOn branch mainUntracked files: (use "git add <file>..." to include in what will be committed) .gitignorenothing added to commit but untracked files present (use "git add" to track)+ git add --all+ git commit -m Add .gitignore and ignore all files that end with .jpg[main 1f0f1a8] Add .gitignore and ignore all files that end with .jpg 1 file changed, 1 insertion(+) create mode 100644 .gitignore+ echo .DS_Store+ git add .gitignore+ git commit -m Ignore .DS_Store[main 6cb76d3] Ignore .DS_Store 1 file changed, 1 insertion(+)
Output
+ cd city-guide+ wget -nv-O hamburg-planetarium.jpg https://upload.wikimedia.org/wikipedia/commons/3/3a/Hamburg_Planetarium_10881zh.jpg2024-12-13 09:12:53 URL:https://upload.wikimedia.org/wikipedia/commons/3/3a/Hamburg_Planetarium_10881zh.jpg [415048/415048]->"hamburg-planetarium.jpg"[1]+ git statusOn branch mainUntracked files:(use"git add <file>..." to include in what will be committed)hamburg-planetarium.jpgnothing added to commit but untracked files present (use "git add" to track)+ touch .gitignore+ echo *.jpg+ git statusOn branch mainUntracked files: (use "git add <file>..." to include in what will be committed) .gitignorenothing added to commit but untracked files present (use "git add" to track)+ git add --all+ git commit -m Add .gitignore and ignore all files that end with .jpg[main d41f35c] Add .gitignore and ignore all files that end with .jpg 1 file changed, 1 insertion(+) create mode 100644 .gitignore+ echo .DS_Store+ git add .gitignore+ git commit -m Ignore .DS_Store[main 2ee9106] Ignore .DS_Store 1 file changed, 1 insertion(+)
#!/bin/bash1cd recipes2git branch feature3git switch feature4cat>> recipes.txt <<- EOMPotatoes1. Wash the potatoes and cut them into evenly sized chunks.2. Bring water to the boil in a large saucepan.3. Once the water is boiling, add a good pinch of salt.3. Add the potatoes.4. Cook the potatoes for 15 - 20 minutes or until fork-tender.5. Drain the pasta in a colander.EOM5git add recipes.txtgit commit -m"Add potatoes recipe to recipes.txt"6cat recipes.txt7git checkout main8cat recipes.txt9git merge feature10git branch -d feature11cat recipes.txt
1
Optional: Navigate into the project repository using cd (or a similar path).
2
Create a new branch called feature using git branch feature.
3
Switch to the new branch using git switch feature. You can also create and switch the branch in one step using git checkout -b feature.
4
Add a new entry to your project text file file. You can use your regular text editor. Here, we add a new entry from the command line using cat.
5
Stage and commit the changes to your project text file using git add and git commit.
6
View the contents of your project text file to verify your changes. Here, we use the cat command again.
7
Switch back to the default branch (main in this example). Here, we use git checkout main but you can also use git switch main.
8
View the contents of your project text file again to confirm that the previous changes do not exist on the main branch.
9
Merge the changes feature branch into the main branch.
10
Delete the merged feature branch using git branch -d feature.
11
View the contents of your project text file yet again to confirm that the previous changes have been merged into the main branch.
Code
#!/bin/bash1cd city-guide2git branch feature3git switch feature4cat>> city-guide.txt <<- EOMPaddle tour of the Alster canals- If you're not afraid of the water and would like to enjoy a really super day whilst at the same time doing a bit of upper body exercise, then why not pick up a paddle and work your way down the Alster canals in a canoe.- Just go to any one of the countless boat and canoe hire points on the Alster to begin your adventure.taken from https://www.hamburg-travel.com/see-explore/sports-recreation/sports-physical-activity/paddle-tour-of-the-alster-canals/EOM5git add city-guide.txtgit commit -m"Add paddle tour of the Alster canals to city-guide.txt"6cat city-guide.txt7git checkout main8cat city-guide.txt9git merge feature10git branch -d feature11cat city-guide.txt
1
Optional: Navigate into the project repository using cd (or a similar path).
2
Create a new branch called feature using git branch feature.
3
Switch to the new branch using git switch feature. You can also create and switch the branch in one step using git checkout -b feature.
4
Add a new entry to your project text file file. You can use your regular text editor. Here, we add a new entry from the command line using cat.
5
Stage and commit the changes to your project text file using git add and git commit.
6
View the contents of your project text file to verify your changes. Here, we use the cat command again.
7
Switch back to the default branch (main in this example). Here, we use git checkout main but you can also use git switch main.
8
View the contents of your project text file again to confirm that the previous changes do not exist on the main branch.
9
Merge the changes feature branch into the main branch.
10
Delete the merged feature branch using git branch -d feature.
11
View the contents of your project text file yet again to confirm that the previous changes have been merged into the main branch.
+ cd recipes+ git branch feature+ git switch featureSwitched to branch 'feature'+ cat+ git add recipes.txt+ git commit -m Add potatoes recipe to recipes.txt[feature 46ddebd] Add potatoes recipe to recipes.txt1 file changed, 9 insertions(+)+ cat recipes.txtPasta1. Bring water to the boil in a large saucepan.2. Once the water is boiling, add a good pinch of salt.3. Add the pasta.4. Cook the pasta according to packet instructions until it's "al dente".5. Drain the pasta in a colander.6. Enjoy!Potatoes1. Wash the potatoes and cut them into evenly sized chunks.2. Bring water to the boil in a large saucepan.3. Once the water is boiling, add a good pinch of salt.3. Add the potatoes.4. Cook the potatoes for 15 - 20 minutes or until fork-tender.5. Drain the pasta in a colander.+ git checkout mainSwitched to branch 'main'+ cat recipes.txtPasta1. Bring water to the boil in a large saucepan.2. Once the water is boiling, add a good pinch of salt.3. Add the pasta.4. Cook the pasta according to packet instructions until it's "al dente".5. Drain the pasta in a colander.6. Enjoy!+ git merge featureUpdating 6cb76d3..46ddebdFast-forwardrecipes.txt|9 +++++++++1 file changed, 9 insertions(+)+ git branch -d featureDeleted branch feature (was 46ddebd).+ cat recipes.txtPasta1. Bring water to the boil in a large saucepan.2. Once the water is boiling, add a good pinch of salt.3. Add the pasta.4. Cook the pasta according to packet instructions until it's "al dente".5. Drain the pasta in a colander.6. Enjoy!Potatoes1. Wash the potatoes and cut them into evenly sized chunks.2. Bring water to the boil in a large saucepan.3. Once the water is boiling, add a good pinch of salt.3. Add the potatoes.4. Cook the potatoes for 15 - 20 minutes or until fork-tender.5. Drain the pasta in a colander.
Output
+ cd city-guide+ git branch feature+ git switch featureSwitched to branch 'feature'+ cat+ git add city-guide.txt+ git commit -m Add paddle tour of the Alster canals to city-guide.txt[feature b2b7c49] Add paddle tour of the Alster canals to city-guide.txt1 file changed, 7 insertions(+)+ cat city-guide.txtHamburg Planetarium- It is one of the world's oldest, and one of Europe's most visited planetariums.- It is located in the district of Winterhude, Hamburg, Germany.- It is housed in a former water tower at the center of Hamburg Stadtpark.Paddle tour of the Alster canals- If you're not afraid of the water and would like to enjoy a really super day whilst at the same time doing a bit of upper body exercise, then why not pick up a paddle and work your way down the Alster canals in a canoe.- Just go to any one of the countless boat and canoe hire points on the Alster to begin your adventure.taken from https://www.hamburg-travel.com/see-explore/sports-recreation/sports-physical-activity/paddle-tour-of-the-alster-canals/+ git checkout mainSwitched to branch 'main'+ cat city-guide.txtHamburg Planetarium- It is one of the world's oldest, and one of Europe's most visited planetariums.- It is located in the district of Winterhude, Hamburg, Germany.- It is housed in a former water tower at the center of Hamburg Stadtpark.+ git merge featureUpdating 2ee9106..b2b7c49Fast-forward city-guide.txt | 7 +++++++ 1 file changed, 7 insertions(+)+ git branch -d featureDeleted branch feature (was b2b7c49).+ cat city-guide.txtHamburg Planetarium- It is one of the world's oldest, and one of Europe's most visited planetariums.- It is located in the district of Winterhude, Hamburg, Germany.- It is housed in a former water tower at the center of Hamburg Stadtpark.Paddle tour of the Alster canals- If you're not afraid of the water and would like to enjoy a really super day whilst at the same time doing a bit of upper body exercise, then why not pick up a paddle and work your way down the Alster canals in a canoe.- Just go to any one of the countless boat and canoe hire points on the Alster to begin your adventure.taken from https://www.hamburg-travel.com/see-explore/sports-recreation/sports-physical-activity/paddle-tour-of-the-alster-canals/
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.
Resolve the merge conflict.
Delete the merged branch afterwards.
Solutions
Code
#!/bin/bash1cd recipes2git branch feature3git switch feature4cat>> recipes.txt <<- EOMChocolate Cake1. Preheat the oven to 350°F (175°C).2. Mix flour, sugar, cocoa powder, baking powder, and salt in a bowl.3. Add eggs, milk, oil, and vanilla extract, and mix well.4. Pour the batter into a greased baking pan.5. Bake for 30-35 minutes.EOM5git add recipes.txtgit commit -m"Add chocolate cake recipe to recipes.txt"6git checkout main7cat>> recipes.txt <<- EOMVanilla Cake1. Preheat the oven to 350°F (175°C).2. Mix flour, sugar, baking powder, and salt in a bowl.3. Add eggs, milk, oil, and vanilla extract, and mix well.4. Pour the batter into a greased baking pan. 5. Bake for 25-30 minutes.EOM8git add recipes.txtgit commit -m"Add vanilla cake recipe to recipes.txt"9git merge feature10sed-i''-e'/^<<<<<<< /d'-e'/^=======/d'-e'/^>>>>>>> /d' recipes.txt11git add recipes.txt12git commit -m"Resolve merge conflict by adding both chocolate and vanilla cake recipes"13git branch -d feature
1
Navigate into the recipes repository using cd (or a similar path).
2
Create a new branch called feature using git branch feature.
3
Switch to the new branch using git switch feature. You can also create and switch the branch in one step using git checkout -b feature.
4
Add a new recipe to your recipes.txt file using cat.
5
Stage and commit the changes to recipes.txt using git add and git commit.
6
Switch back to the default branch (main in this example) using git checkout main. You can also use git switch main.
7
Make conflicting changes in the main branch to recipes.txt using cat.
8
Stage and commit the conflicting changes to recipes.txt using git add and git commit.
9
Attempt to merge the feature branch with the default branch to create a merge conflict using git merge feature.
10
Resolve the merge conflict by editing recipes.txt. You can use a regular text editor to do this. In this example, we remove the conflict markers that Git added to recipes.txt using sed which results in keeping both recipes. This is not a recommended way to resolve merge conflicts and we only do it here to resolve the merge conflict without manual intervention. Merge conflicts usually always require manual resolution by the user.
11
Stage the resolved changes to recipes.txt using git add.
12
Commit the resolved changes in recipes.txt with a descriptive commit message using git commit.
13
Delete the merged feature branch using git branch -d feature.
Command history
Output
+ cd recipes+ git branch feature+ git switch featureSwitched to branch 'feature'+ cat+ git add recipes.txt+ git commit -m Add chocolate cake recipe to recipes.txt[feature c1ab84b] Add chocolate cake recipe to recipes.txt1 file changed, 9 insertions(+)+ git checkout mainSwitched to branch 'main'+ cat+ git add recipes.txt+ git commit -m Add vanilla cake recipe to recipes.txt[main ff5cdb5] Add vanilla cake recipe to recipes.txt1 file changed, 9 insertions(+)+ git merge featureAuto-merging recipes.txtCONFLICT(content): Merge conflict in recipes.txtAutomatic merge failed;fix conflicts and then commit the result.+ sed -i-e /^<<<<<<< /d -e /^=======/d -e /^>>>>>>> /d recipes.txtsed: can't read : No such file or directory+ git add recipes.txt+ git commit -m Resolve merge conflict by adding both chocolate and vanilla cake recipes[main 63c6947] Resolve merge conflict by adding both chocolate and vanilla cake recipes+ git branch -d featureDeleted branch feature (was c1ab84b).
Add the SSH key to the remote repository (for example, GitHub or GitLab).
Solutions
Code
#!/bin/bash1ssh-keygen-t ed25519 -C"your_email@example.com"2cat ~/.ssh/id_ed25519.pub3# Copy the SSh key to your account
1
In the command line, create a new SSH key. Make sure to change the example email to your email address. Optionally, provide a passphrase.
2
Copy the SSH key to your clipboard. Here, we use cat to print the contents of the SSH key to the command line. Copy the contents displayed in the Terminal to your clipboard.
3
Add the SSH key to your remote repository account.
Connect to GitHub using a PAT
Exercises
Generate a personal access token (PAT) on GitHub.
Use the PAT for GitHub authentication.
Solutions
Go to GitHub.
Click on your profile picture in the upper-right corner and select “Settings”.
In the left sidebar, click “Developer settings”.
In the left sidebar, click “Personal access tokens”.
Click “Generate new token”.
Give your token a descriptive name.
Select the scopes or permissions you’d like to grant this token. For uploading a repository, select the repo scope. 1 .Click Generate token.
Copy the token and store it in a secure place. You won’t be able to see it again.
Use the PAT for GitHub authentication. When prompted for a username and password for GitHub operations (like git push), use your GitHub username as the username and the generated PAT as the password.
Upload your local repository to a remote repository
Exercises
Create an empty repository on the remote repository hosting platform, for example GitHub or GitLab. Make sure to not initialize the repository with any files!
If needed, navigate to your project repository using the command line.
Set the remote URL of your local repository to your remote repository.
Push the changes on your default branch (main or master) to your remote repository.
Solutions
Code
#!/bin/bash1# create an empty remote repository2cd my-project3git remote add origin https://github.com/your-username/your-repo-name.git4git push -u origin main
1
To create an empty repository on GitHub: (1) Go to GitHub and click the + icon in the upper-right corner, then select New repository. (2) Name your repository. (3) Do not select Initialize this repository with a README. (4) Click Create repository.
2
Optional: Navigate into the project repository using cd (or a similar path).
3
Set the remote URL of the local repository to the repository using git remote add origin <URL>. Remember to use the correct <URL> depending on whether you authentication method (typically SSH or PAT).
4
Push the changes on the default branch (here, main) to the remote repository using git push -u origin main.
Add a README.md
Exercises
Find the option to create a new file on your remote repository in the browser.
Name the file README.md, add a brief description, and provide a commit message.
Click the green Commit new file button to save the README.md file to the repository.
Use git pull origin main to pull the changes to your local repository.
“Private” collaboration with pull requests (using GitHub Flow)
Exercises
Add your exercise partner as a collaborator to your project repository on GitHub.
Clone your partner’s repository.
Create a new branch in your collaborator’s repository.
Add a new entry to your collaborator’s project file (e.g., .txt or .qmd (if you are unsure, where to add the entry, ask your collaborator!)
Add and commit the changes.
Push the changes on the new branch to the remote repository.
Create a Pull Request (on GitLab: Merge Request).
Review the Pull Request that your collaborator made in your repository.
🚀 Optional: Add additional changes on the branch pushed by your collaborator.
Merge the pull request into your repository.
Solutions
Code
#!/bin/bash1# Add your exercise partner as a collaborator to your recipes repository2cd ~3git clone https://github.com/partner-username/partner-repo-name.git4git checkout -b new-branch-name5echo"New Recipe">> recipes.txt6git add recipes.txtgit commit -m"Add new recipe to recipes.txt"7git push origin new-branch-name8# Create a Pull / Merge Request.9# Review the PR your partner made in your repository.10# Merge the PR into your repository.
1
Add your exercise partner as a collaborator to your recipes repository: (1) Go to your repository on GitHub. (2) Click on Settings. (2) Click on Manage access in the left sidebar. (3) Click Invite a collaborator and enter your partner’s GitHub username.
2
Move to the location on your computer where you would like to clone your partner’s repository into, using cd in the command line. Here, we cd into the user’s home directory (~).
3
Clone your partner’s repository using git clone. Make sure that you not cloning into an existing repository.
4
Create a new branch in your partner’s repository.
5
Add a recipe to your partner’s recipes.txt file.
6
Add and commit the changes using a descriptive commit message.
7
Push the changes on the new branch to GitHub.
8
Create a Pull Request: (1) Go to your partner’s repository on GitHub. (2) Click Compare & pull request for your branch. (3) Provide a title and description, then click Create pull request.
9
Review the PR your partner made in your repository: (19) Go to your repository on GitHub. (2) Click on the Pull requests tab. (3) Click on the PR made by your partner. (4) Review the changes and provide feedback.
10
Merge the PR into your repository: (1) After reviewing, click the green Merge pull request button. (2) Click Confirm merge.
Clone and sync your repository
Exercises
Move to a location on your computer where you want to clone a repository.
Clone your remote repository to a different location on your computer.
Stage and commit changes in the new location (consider using a new branch).
Push these new changes to GitHub.
Pull the changes to the repository in the original location.
Fork the recipes repository of another course participant (ideally someone who is not your collaborator from the previous exercise).
Create an Issue in your new collaborator’s repository (maybe their repository is missing a great recipe?).
Repeat the steps from the exercise on collaboration with remote repositories using the forked repository:
Clone the forked repository into a sensible location on your computer.
Create a new branch and create one or multiple commits “fixing” the Issue that you opened. If available, follow the contributing guide of your collaborator’s repository.
Push your changes to the remote repository.
Create a pull / merge request with your changes (hint: from the forked to the original repository) and refer to the Issue in your pull / merge request.
Solutions
Forking is a process where you create a copy of someone else’s repository under your own account. It allows you to freely experiment with changes without affecting the original project.
To fork the recipes repository of another course participant: (1) Go to the GitHub repository you want to fork. (2) Click the Fork button at the top-right corner of the repository page. (3) Select your GitHub account to fork the repository.
Create an Issue, suggesting a missing recipe: (1) Go to the Issues tab of your partner’s repository on GitHub. (2) Click New issue. Provide a title and description for the Issue, suggesting a missing recipe. (3) Click Submit new issue.
Clone the forked repository into a sensible location on your computer.
Create a new branch and create one or multiple commits “fixing” the Issue that you opened.
Code
git checkout -b issue-fix-branchecho"New Recipe Content">> recipes.txtgit add recipes.txtgit commit -m"Add new recipe to fix #1"
Push your changes to the remote repository:
Code
git push origin issue-fix-branch
Create a pull request with your changes (from the forked to the original repo) and refer to the issue in your pull request:
Go to your forked repository in your browser.
Click the Compare & pull request button.
Ensure that the base repository is the original and the base branch is main.
Provide a title and description for your pull request.
Refer to the issue by adding Fixes #issue-number in the description.
Click Create pull request.
Review any pull requests in your repository.
Reviewing pull requests
Exercises
View any pull requests that are created in your recipes repository.
Review the changes made by the contributor in the pull request.
If needed, discuss additional changes with the contributor in the pull request.
Close the pull request by merging the proposed changes.
Tags and Releases
Exercises
Create a tag
Create a lightweight or an annotated tag named v1.0.0.
Create a GitHub Release
Go to your repository on GitHub and create a release of the tag v1.0.0.
Link a Zenodo record
Connect your GitHub repository to Zenodo.
Solutions
Create a lightweight or an annotated tag named v1.0.0.
For a lightweight tag:
git tag v1.0.0
For an annotated tag:
git tag -a v1.0.0 -m"Release version 1.0.0"
Push the tag to GitHub:
git push origin v1.0.0
Create a GitHub Release
Go to your repository on GitHub.
Click on “Releases” then “Draft a new release”.
Choose the tag v1.0.0 from the list
Fill in the release title and description.
Click “Publish release”.
Link a Zenodo Record
Connect your GitHub repository to Zenodo.
Go to Zenodo and log in.
Follow Zenodo’s instructions to link your repository:
Go to the “GitHub” tab in your Zenodo settings. Navigate to Settings in Zenodo. Click on the GitHub tab.
Enable the repository for Zenodo integration. Find your GitHub repository in the list and toggle the switch to enable it for Zenodo integration.
Create a new release on GitHub to trigger Zenodo to archive your repository and mint a DOI.
Go back to GitHub and create a new release (as described in the Create a GitHub Release section). This will trigger Zenodo to archive your repository and mint a DOI for the release.
Graphical User Interfaces
Exercises
Install a Git GUI
Install a Git GUI
Login to the client using your GitHub account.
View your recipes repository in the Git GUI.
Open a Git repository in RStudio
Open your recipes repository in RStudio.
Edit the recipes.txt or recipes.qmd file by adding or modifying a recipe.
Commit your changes using RStudio’s Git interface.
Solutions
Open a Git Repository in RStudio
Open your recipes repository in RStudio:
Open RStudio.
Open a Project:
Click on File -> New Project -> Version Control -> Git.
Enter the URL of your recipes repository or choose the local path if already cloned.
Click Create Project.
Edit the recipes.txt or recipes.qmd file by adding or modifying a recipe:
Open the file:
In the RStudio File Explorer, navigate to the recipes repository and open recipes.txt or recipes.qmd.
Edit the file:
Make your desired changes to the file by adding or modifying a recipe.
Save the file:
Click on File -> Save or press Ctrl + S.
Commit your changes using RStudio’s Git interface:
Open the Git pane:
In RStudio, find the Git pane, usually located in the top-right or bottom-right corner.
Stage the changes:
In the Git pane, you should see the modified files listed. Check the box next to the files you want to commit to stage the changes.
Commit the changes:
Click on Commit.
Enter a commit message describing your changes.
Click Commit again to finalize the commit.
Push the changes:
After committing, click on Push to upload your changes to the remote repository on GitHub.
Stashing
Exercises
Navigate to the recipes repository using the command line.
Create a new branch called feature/stash-exercise.
Switch to the new branch.
Make any change to any of the files in your project directory.
Navigate into the recipes repository using cd (or a similar path).
2
Create a new branch called feature/stash-exercise using git branch.
3
Switch to the new branch using git switch. Alternatively, you could have also done the two last steps with one command using git checkout -b feature/stash-exercise.
4
Make any change to any of the files in your project directory. Here, we use the echo command to append text to the end of recipes.txt.
5
Stash your changes without adding a message using git stash.
6
Verify that the working directory is clean using git status. Check that the output indeed indicates a clean working directory.
7
Apply the stash to your working directory using git stash apply.
8
Verify that your changes are restored. Here, we use tail -1 recipes.txt to print the last line of recipes.txt.
Command history
Output
+ cd recipes+ git branch feature/stash-exercise+ git switch feature/stash-exerciseSwitched to branch 'feature/stash-exercise'+ echo This is yummy!+ git stashSaved working directory and index state WIP on feature/stash-exercise: 63c6947 Resolve merge conflict by adding both chocolate and vanilla cake recipes+ git statusOn branch feature/stash-exercisenothing to commit, working tree clean+ git stash applyOn branch feature/stash-exerciseChanges not staged for commit:(use"git add <file>..." to update what will be committed)(use"git restore <file>..." to discard changes in working directory)modified: recipes.txtno changes added to commit (use "git add" and/or "git commit -a")+ tail -1 recipes.txtThis is yummy!
Navigate into the recipes repository using cd (or a similar path).
2
Create a new branch called feature/revert-exercise using git branch.
3
Switch to the new branch using git switch. Alternatively, you could have also done the two last steps with one command using git checkout -b feature/revert-exercise.
4
Make any change to any of the files in your project directory. Here, we use the echo command to append text to the end of recipes.txt.
5
Stage your change using git add <filename>.
6
Commit your changes using git commit -m "Commit message".
7
Retrieve the commit hash of the last commit using git log. Here, we save the commit hash as variable and use this variable in the next step. Normally, you would just run git log and find the hash of the commit that you want to revert. (This code does this differently for automation) You can also use git log -1 to only see your most recent commit.
8
Revert the commit using git revert <commithash>. This will open your editor where you can specify a commit message for the revert. If you use the --no-edit flag, Git will use a default commit message.
Navigate into the recipes repository using cd (or a similar path).
2
Create a new branch called feature/rebase-exercise using git branch.
3
Switch to the new branch using git switch. Alternatively, you could have also done the two last steps with one command using git checkout -b feature/rebase-exercise.
4
Make any change to any of the files in your project directory. Here, we append the text "New feature content" to the file feature.txt.
5
Stage your change using git add <filename>.
6
Commit your changes using git commit -m "commit message".
7
Switch back to the main branch using git switch.
8
Make any change to any of the files in your project directory. Here, we append "Main branch content" to a file named main.txt. This change simulates working on the main branch simultaneously.
9
Stage your changes using git add <filename>.
10
Commit your changes using git commit -m "commit message".
11
Switch back to the feature branch using git switch feature/rebase-exercise.
12
Rebase the commit of the feature/rebase-exercise branch onto your main branch using git rebase main.
13
Switch back to the main branch using git switch main.
14
Verify that the commit from feature/rebase-exercise is now on top of the commits from main using git log.
Command history
Output
+ cd recipes+ git branch feature/rebase-exercise+ git switch feature/rebase-exerciseSwitched to branch 'feature/rebase-exercise'+ echo New feature content+ git add feature.txt+ git commit -m Add feature content[feature/rebase-exercise b3eae00] Add feature content1 file changed, 1 insertion(+)create mode 100644 feature.txt+ git switch mainSwitched to branch 'main'+ echo Main branch content+ git add main.txt+ git commit -m Update main content[main d7d2a38] Update main content1 file changed, 1 insertion(+)create mode 100644 main.txt+ git switch feature/rebase-exerciseSwitched to branch 'feature/rebase-exercise'+ git rebase mainRebasing(1/3)Rebasing(2/3)Rebasing(3/3)Successfully rebased and updated refs/heads/feature/rebase-exercise.+ git switch mainSwitched to branch 'main'+ git log --onelined7d2a38 Update main content63c6947 Resolve merge conflict by adding both chocolate and vanilla cake recipesff5cdb5 Add vanilla cake recipe to recipes.txtc1ab84b Add chocolate cake recipe to recipes.txt46ddebd Add potatoes recipe to recipes.txt6cb76d3 Ignore .DS_Store1f0f1a8 Add .gitignore and ignore all files that end with .jpg761edc7 Add pasta recipe to recipes.txt