Session 4: First steps with Git & Git Essentials

Track, organize and share your work: An introduction to Git for research

Course at Zentralinstitut für Seelische Gesundheit

Slides | Source

License: CC BY 4.0 DOI

Wednesday, 22nd of January 2025, 14:00

1 Last session

Last session: The command line & Git setup

Source: Wikimedia Commons (free license)

Last session: Learning objectives

After the last session, you should now be able to answer the following questions / do the following:

Command line

💡 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.

Setup

💡 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

Last session: Command line cheatsheet

Command Description
pwd Displays the path of the current working directory
cd <PATH> Changes the current working directory to <PATH>
cd ~ Changes the current working directory to the user’s home directory
cd .. Moves up one folder
cd ../.. Moves up two folders
clear Clears the contents of your command line window
ls Lists files and folders in the current working directory
ls <PATH> Lists files and folders in <PATH>
ls -a Lists all files (including hidden files) in the current working directory
ls -alh Lists all files in a long format that is easy to read for humans
[Command] --help Displays all possible flags for a specific command (on Windows)
man [Command] Displays all possible flags for a specific command (on macOS)
mkdir <FOLDER> Creates a new folder called <FOLDER>
mkdir <FOLDER1> <FOLDER2> Creates two separate folders called <FOLDER1> and <FOLDER2>
touch <FILE> Creates a new empty file called <FILE>
open <FILE> Opens the file called <FILE> (on macOS)
start <FILE> Opens the file called <FILE> (on Windows)
echo "example text" >> file.txt Writes “example text” into file.txt
cat <FILE> Displays the content of <FILE>
mv FILE.txt <FOLDER> Move FILE.txt into <FOLDER>
mv <FOLDER_OLD> <FOLDER_NEW> Renames <FOLDER_OLD> to <FOLDER_NEW>
ls -alh *.csv Uses a wildcard to list all .csv files in the current working directory
rm -r <FOLDER> Removes the folder <FOLDER>
history Display the command history of the current terminal session
history > history.txt Saves the entire command history to a file named history.txt (overwrites existing content in history.txt)
history >> history.txt Adds the entire command history to the end of the file named history.txt
tree Displays a graphical representation of the directory structure
wget <URL> Downloads a file from the specified to the current directory

Last session: Git setup cheatsheet

Command Description
git config Get an overview of Git config commands
git config --global user.name "user.name" Sets Git username
git config --global user.email "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

Last session: recipes project

Command line

At the end of this session, you should have accomplished the following:

  1. You used the command line to create a folder on your computer called recipes.
  2. 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.

Setup

At the end of this session, you should have accomplished the following:

  1. You set up Git.
  2. You configured Git.

Please keep the recipes folder! We will continue to use it in the following sessions.

Questions from last session

“How can I enable copy-paste in Git Bash?”

  1. Click on the Git Bash icon in the top-left corner.
  2. Choose Options.
  3. Go to Keys menu.
  4. Enable Copy and Paste (Ctrl/Shift + Ins) option.
  5. Click Apply and Save.

“How do I find the Users folder on Windows?”

  • Use ~
  • If pwd is at /: you can still use cd C:

“The command line does not give feedback!”

  • Yes, I know, I’m sorry …
  • You need to check explicitly if the command had the desired effect.
  • Example: After mkdir recipes, use ls to check if the /recipes folder was created.

2 This session: First steps with Git & Git Essentials

Learning objectives

First steps with Git

💡 You can initialize a Git repository.
💡 You can check the status of a Git repository.
💡 You understand the difference between the staging area and a commit.
💡 You can stage and commit changes.
💡 You understand the difference between a commit message and a description.

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.

Reading

“First steps with Git” and “Git Essentials”

Tasks

In this session, you will work on the following tasks:

  1. Reading: Read the chapter(s) “First steps with Git” and “Git Essentials” in the Version Control Book.
  2. Implementation: Try out the commands in the chapter.
  3. Exercises: Work on the exercises for the recipes project.
  4. Quiz: Test your knowledge with the quiz.

As always:

  1. Try out the commands of this session and play around with them.
  2. Check whether you have achieved the learning objectives.
  3. Ask questions!
  4. Let’s git started!

recipes project

First steps with Git

At the end of this session, you should have accomplished the following:

  1. You set up Git.
  2. You initialized your recipes folder as a Git repository.
  3. You committed your first file to the recipes repository.

Please keep the recipes folder! We will continue to use it in the following sessions.

recipes project

Git Essentials

At the end of this session, you should have accomplished the following:

  1. Commit at least three changes in recipes.txt.
  2. Make at least one commit using git commit --amend.
    • For example, add a a new entry without a title first, commit, then add a title and use git commit --amend to add the title change to the same commit.
  3. Create a .gitignore file.
    • Everyone: Create a random file that you want to ignore, for example image.jpg.
    • All macOS users: Let your repository ignore .DS_Store.
  4. 🚀 Optional: Commit .gitkeep in an otherwise empty directory.

Please keep the recipes folder! We will continue to use it in the following sessions.

Cheatsheet

First steps with Git

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

Git essentials

Command Description
git log Views past commits
git diff Views made changes compared to the last commit
git mv Renames or moves files and automatically stages the changes

Exercises: First steps with Git

Note: There are several terms in these instructions that might be unfamiliar to you, such as “repository,” “stage,” or “commit.” Don’t worry - you will learn about these terms during this session.

Initialize a Git repository

  1. If needed, navigate to the project folder using the command line.
  2. Initialize a new Git repository in the project folder.

Add content and commit changes

  1. Create a new text file and name it appropriately.
  2. Add a short entry to the text file (any favorite or an intriguing AI-generated one).
  3. Stage the new file.
  4. Commit the changes in the text file with a descriptive commit message.

🚀 Optional: Commit at least three additional changes in your new file.

Exercises: Git Essentials

Amend a commit

  1. If needed, navigate to the project folder using the command line.
  2. Make additional changes to your project text file.
  3. Stage the changes.
  4. Amend the previous commit to include the new changes.
  5. Check the commit history to verify that the last commit message has not changed.

For example, add a new entry 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. If needed, navigate to the project folder using the command line.
  2. Add a random file to your repository that you want to ignore, for example an image file like image.jpg.
  3. Check the state of your repository to confirm that Git noticed the added file.
  4. Create a .gitignore file.
  5. Add the random file to the .gitignore file.
  6. Check the state of your repository again to confirm that Git now ignores the added file.
  7. Stage the changes in your repository.
  8. Commit the .gitignore file using a descriptive commit message.
  9. 🚀 All macOS users: Let your repository ignore .DS_Store.

Important notes

Make sure that there is no Git repository in your home directory!

  • Your home directory is at /Users/yourname (macOS) or C:\Users\yourname (Windows).
  • There should be no .git folder in your home directory (check with ls -a; see previous slide).
  • If you find a .git folder in your home directory, don’t do anything and talk to us first!
  • We want to make sure that you don’t accidentally track files in Git that you don’t want to track.

After you open a fresh terminal, you always need to navigate to your repository first!

  • Yes, I know, I’m sorry …
  • Use pwd to check your current location and cd to move to another directory.

Use a Git user.name and user.email that you are comfortable with

  • We will eventually practice to share Git repositories publicly on GitHub.
  • Your user.name and user.email will be part of your commit history.
  • A public repository will therefore reveal your user.name and user.email.

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!
  • You can check your current working directory by running pwd in your recipes folder.
  • Also: Always remember to cd into your repository before you execute Git commands!
  • If your recipes folder is in your user directory, please tell us 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:

Output
fatal: invalid gitfile format: /Users/user/.git

Thanks to Eamon Caddigan for the idea!

Solutions: Setup

Code
#!/bin/bash

cd recipes # <1>
git config --global user.name "Your Name" # <2>
git config --global user.email "your.email@example.com" # <3>
git config --global init.defaultBranch main # <4>
git config --global core.editor "vim" # <5>
git config --list # <6>
  1. 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".
  6. List the Git configuration.

Solutions: First steps with Git

Code
#!/bin/bash

cd recipes # <1>
git init # <2>
touch recipes.txt # <3>
cat > recipes.txt <<- EOM # <4>
Pasta

1. 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.
EOM
git add recipes.txt # <5>
git commit -m "Add pasta recipe to recipes.txt" # <6>
  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.

Solutions: Amend a commit

Code
#!/bin/bash

cd recipes # <1>
echo "6. Enjoy!" >> recipes.txt # <2>
git add recipes.txt # <3>
git commit --amend --no-edit # <4>
git log --oneline # <5>
  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.

Solutions: Create a .gitignore file

Code
#!/bin/bash

cd 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>
  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.

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.

Source: git-scm.com

Source: Heidi Seibold

Saving command line history?

Use this:

history > history.txt

https://lennartwittkuhn.com/version-control-book/chapters/command-line.html#saving-command-line-history

Commit .gitignore?

Yes, commit your project-specific .gitignore file.

https://lennartwittkuhn.com/version-control-book/chapters/first-steps-git.html#ignoring-files-and-folders-.gitignore

Best practices for commit messages

  • 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