Session 3: Setup & First steps with Git

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

Course at General Psychology Lab at the University of Hamburg

Slides | Source

License: CC BY 4.0 DOI

10:45

1 Last session: The command line

Last session: The command line

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:

đź’ˇ 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:

  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.

2 This session: Setup & First steps with Git

Reading

https://lennartwittkuhn.com/version-control-book/chapters/first-steps-git.html

Learning objectives

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

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:

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

Thanks to Eamon Caddigan for the idea!

Tasks

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

  1. Reading: Read the chapters “Setup”, “First steps with Git” and, optional, “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

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 recipe to the recipes repository.

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

Exercises

Configure Git

  1. If needed, navigate into the recipes folder using the command line.
  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.
  6. List the Git configuration settings.

Initialize a Git repository

  1. If needed, navigate to the recipes folder using the command line.
  2. 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.

🚀 Optional: Commit at least three additional changes in recipes.txt.

🚀 Bonus exercises

Amend a commit

  1. If needed, navigate into the recipes repository using cd recipes (or a similar path).
  2. Make additional changes to your recipes.txt 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 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. If needed, navigate into the recipes repository using cd recipes (or a similar path).
  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.

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 recipes (or a similar path) to navigate into the recipes 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 recipes repository using cd recipes (or a similar path).
  2. 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.
  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. Navigate into the recipes repository using cd recipes (or a similar 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 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.
  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 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.

Source: git-scm.com

Source: Heidi Seibold

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 -0700

    Fix the widget rendering issue in the dashboard

commit a4324f44d3e85723a4d91cb9e07132b7115e4941
Author: John Smith <john@example.com>
Date:   Mon Apr 23 16:17:59 2024 -0700

    Update dependencies to newer versions

commit fa204b9145bf7fc7ff226a26b49a567fc2eb1b94
Author: Alice Johnson <alice@example.com>
Date:   Sun Apr 22 15:08:43 2024 -0700

    Initial commit of project files

commit b9690b287bdfec6e17af39b7337b84e9ebf6f046
Author: Lennart Wittkuhn <lennart.wittkuhn@tutanota.com>
Date:   Fri Mar 22 15:19:43 2024 +0100

    move illustration of bad git commits (xkcd comic) and edit sentence

commit d8d770dd84cd19086f41d8d38752b223c8130859
Author: konradpa <konrad@pagenstedt.de>
Date:   Wed Mar 6 13:51:13 2024 +0100

    add image to setup chapter

commit 074c9f6e12dd5fc8cc61de9f31efbdbce41a7583
Author: konradpa <konrad@pagenstedt.de>
Date:   Wed Mar 6 13:51:07 2024 +0100

    add image to rewriting history chapter

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