Session 3: Setup & Configuration of Git

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, 15th of January 2025, 15:30

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.

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 & Configuration of Git

Learning objectives

💡 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

Reading

“Setup” (and “Installation”, if needed)

Tasks

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

  1. Reading: Read the chapter(s) “Setup” (and “Installation”, if needed) 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 configured Git.

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

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

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

Exercises

Configure Git

  1. If needed, navigate into the project 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.

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.