After the last session, you should now be able to answer the following questions / do the following:
đź’ˇ You can navigate directories using the command line. đź’ˇ You can use shortcuts like the tilde or dots to navigate your file system. đź’ˇ You can explain the difference between absolute and relative paths. đź’ˇ You can use arguments and flags to modify command-line commands. đź’ˇ You understand the concept of wild cards (*) and can use it for system navigation.
Last session: recipes project
At the end of this session, you should have accomplished the following:
You used the command line to create a folder on your computer called recipes.
You used the command line to create a file called recipes.txt inside the recipes folder.
Please keep the recipes folder! We will continue to use it in the following sessions.
đź’ˇ You know how to set up Git for the first time đź’ˇ You have set up Git on your computer đź’ˇ You understand the difference between the three Git configuration levels đź’ˇ You know how to configure your username and email address in Git đź’ˇ You have set up your preferred text editor when working with Git đź’ˇ You can escape the command-line text editor Vim
First steps with Git
đź’ˇ You can initialize a Git repository đź’ˇ You can stage and commit changes
🚀 Optional: Git essentials
đź’ˇ You know how to explore the commit history đź’ˇ You can compare different commits đź’ˇ You know how to use and create a .gitignore file đź’ˇ You can discuss which files can (not) be tracked well with Git and why đź’ˇ You know how to track empty folders in Git repositories
Cheatsheet
Configuration
Command
Description
git config
Get an overview of Git config commands
git config --global "user.name"
Sets Git username
git config --global "user.email"
Sets Git email address
git config --global core.editor "editorname"
Sets Git text editor
git config --global init.defaultBranch main
Sets default branch name to main
git config --list
Views set Git configurations
Git Basics
Command
Description
git init
Initializes a folder as a Git repository
git status
Shows Git tracking status of files in the repository
git add
Adds file(s) to the staging area
git commit
Commits staged files
git commit -m "commit message"
Commits staged files with a commit message
Important note: Git repository in user folder?
Please make sure that your recipes folder is in a suitable place (for example, in the Desktop, Documents folders or where you keep your course-related files, …) and not in your user directory!
Also: Always remember to cd into your repository before you execute Git commands!
If your recipes folder is in your user directory, please tell Lennart and we can fix it.
Tip: To prevent ever creating a Git repository inside your user folder, you can use:
Code
touch ~/.git
Running git init in the user directory now results in a (desired) error:
Optional: Use cd recipes (or a similar path) to navigate into the recipes subfolder.
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.
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.
Change the default name of the initial branch to main using git config --global init.defaultBranch main.
Optional: Change your default text editor. In this example, the default text editor is changed to Vim using git config --global core.editor "vim".
List the Git configuration.
Solutions: First steps with Git
Code
#!/bin/bashcd recipes # <1>git init # <2>touch recipes.txt # <3>cat> recipes.txt <<- EOM# <4>Pasta1. 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.EOMgit add recipes.txt # <5>git commit -m"Add pasta recipe to recipes.txt"# <6>
If needed, navigate into the recipes subfolder using cd recipes (or a similar path).
Initialize a new Git repository in the recipes folder using git init.
Create a new file called recipes.txt using touch. Note that you can also use a regular text editor to do this.
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.
Stage the new recipes.txt file using git add.
Commit the changes in recipes.txt with a descriptive commit message using git commit.
Optional: Navigate into the recipes repository using cd recipes (or a similar path).
Make an additional change to the recipes.txt file. In this example, echo is used to append text to recipes.txt. Note that you can also use a regular text editor to do this.
Stage the changes using git add.
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.
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.
Solutions: Create a .gitignore file
Code
#!/bin/bashcd recipes # <1>wget-nv-O pasta.jpg https://upload.wikimedia.org/wikipedia/commons/thumb/8/84/Rigatoni.jpg/320px-Rigatoni.jpg # <2>git status # <3>touch .gitignore # <4>echo"*.jpg"> .gitignore # <5>git status # <6>git add --all# <7>git commit -m"Add .gitignore and ignore all files that end with .jpg"# <8>echo".DS_Store">> .gitignore # <9>git add .gitignore # <9>git commit -m"Ignore .DS_Store"# <9>
Navigate into the recipes repository using cd recipes (or a similar path).
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.
Check the state of your repository using git status. Git will notice the new file and list it as untracked.
Create a .gitignore file using touch. Note that you can also use a regular text editor to do this.
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 recipes.txt. 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.
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.
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.
Commit the changes in .gitignore with a descriptive commit message using git commit.
Add .DS_Store to the .gitignore file to ignore it. Again, echo is used to append text to recipes.txt. Note that you can also use a regular text editor to do this. Stage and commit your changes using a descriptive commit message.
3 Appendix
Staging and Committing
Staging:
Command: git add
Purpose: Preparing and organizing files before they are recorded in the repository’s history.
Committing:
Command: git commit -m "commit message"
Purpose: Saving the changes in the staged files to the repository’s history, creating a snapshot.
git log
git log
e.g:
commit 3f6db14ed93d6e92a207a9a3ac5f8b8c5c5c5c34 (HEAD-> main, origin/master, origin/HEAD)Author: Jane Doe <jane@example.com>Date: Tue Apr 24 14:24:48 2024 -0700Fix the widget rendering issue in the dashboardcommit a4324f44d3e85723a4d91cb9e07132b7115e4941Author: John Smith <john@example.com>Date: Mon Apr 23 16:17:59 2024 -0700Update dependencies to newer versionscommit fa204b9145bf7fc7ff226a26b49a567fc2eb1b94Author: Alice Johnson <alice@example.com>Date: Sun Apr 22 15:08:43 2024 -0700Initial commit of project files
commit b9690b287bdfec6e17af39b7337b84e9ebf6f046Author: Lennart Wittkuhn <lennart.wittkuhn@tutanota.com>Date: Fri Mar 22 15:19:43 2024 +0100move illustration of bad git commits (xkcd comic)and edit sentencecommit d8d770dd84cd19086f41d8d38752b223c8130859Author: konradpa <konrad@pagenstedt.de>Date: Wed Mar 6 13:51:13 2024 +0100add image to setup chaptercommit 074c9f6e12dd5fc8cc61de9f31efbdbce41a7583Author: konradpa <konrad@pagenstedt.de>Date: Wed Mar 6 13:51:07 2024 +0100add image to rewriting history chapter
Try to keep commit messages short (less than 72 characters)
Use present tense and start with an imperative verb to indicate the purpose of the commit, for example “add”, “fix”, “improve” (as if you are giving orders to the codebase to change its behavior)
If applied, this commit will … [your commit message]
Try to describe why a change is being made
Link specific issues that are addressed by your commit
Use the description for more explanation and context