Session 3: First steps with Git

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

Course at University of Hamburg & Erasmus University Rotterdam

Slides | Source

License: CC BY 4.0 DOI

November 01 2024 (10:15 am)

1 Logistics & Admin

Schedule

No Date Title Contents Reading Survey/Quiz
1 2024-10-18 Introduction to version control Organizational matters
Overview of seminar sessions
Introduction to version control
Introduction to Git and its advantages
Intro to version control Course introduction Survey
2 2024-10-25 Command line File Systems
Benefits of the Command Line
Basic Command Line commands
Command Line Command Line Quiz
3 2024-11-01 Setup + Git Fundamentals Installation and configuration of Git
Initializing a Git repository
Basic Git commands
Installation, Setup, First steps with Git Installation Survey, Git Basics Quiz
4 2024-11-08 Basic Git workflow Practicing basic Git commands
Ignoring files with .gitignore
Good commit messages
First steps with Git Git Basics Quiz
5 2024-11-15 Git Essentials (Repetition & Practice) Practicing basic Git commands
Ignoring files with .gitignore
Good commit messages
Git Essentials Git Basics Quiz
6 2024-11-22 Git Branching and Merging Understanding branches in Git
Creating and switching between branches
Merging branches
Resolving merge conflict
Branches Git Branches Quiz
7 2024-11-29 Quarto Workshop Introduction to Quarto
8 2024-11-06 Introduction to GitHub Introduction to remote repositories
Creating a GitHub account
Creating and managing repositories on GitHub
Pushing and pulling changes
GitHub Intro GitHub Quiz
9 2024-12-13 GitHub with collaborators Cloning a remote repository
Branching and merging in a collaborative environment
Pull Requests
GitHub Issues
Graphical User Interfaces (GUIs), e.g., GitKraken
GitHub Intro, GitHub Issues GitHub Quiz
10 2024-12-20 Repetition and Practice Repetition and Practice
11 2025-01-10 GitHub with the world Forking a remote repository
README files
Project Management
GitHub Intro, GitHub Issues GitHub Quiz
12 2025-01-17 Publishing Creating Tags with Git
Creating Releases with GitHub
Using Zenodo for scientific publishing
Licences
Citation Files
Tags and Releases
13 2025-01-24 Graphical User Interfaces Repetition and Practice
Introduction to using GUIs
Graphical User Interfaces
14 2025-01-31 Summary & Wrap-Up Course evaluation
Repetition and Practice
Introduction to using GUIs
Graphical User Interfaces

2 Last session: The command line

Last session: The command line

Source: Wikimedia Commons (free license)

Last session’s 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.

city-guide 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 city-guide.
  2. You used the command line to create a file called city-guide.txt inside the city-guide folder.

Please keep the city-guide folder! We will continue to use it in the following sessions.

3 This session: First steps with Git

Reading

“Setup” and “First steps with Git”

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

Note: These are the learning objectives for this and next week.

Tasks

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

  1. Reading: Read the chapter(s) “Setup” and “First steps with Git” in the Version Control Book.
  2. Implementation: Try out the commands in the chapter.
  3. Exercises: Work on the exercises for the city-guide 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!

city-guide project

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

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

Please keep the city-guide folder! We will continue to use it in the following sessions.

Today’s team topic

  • Interview each other about your favorite cultural place (e.g., museum, art gallery, cultural center)
  • Add the recommendation of your partner to your text file.
  • Help each other in completing the exercises.

Conversation starter

  1. Your name?
  2. Are you based in Hamburg or Rotterdam?
  3. A (fun) fact about you? For example:
    • What did you study before and where?
    • What do you expect from this course?
    • What’s your hobby?
    • Do you have a pet?
    • What’s your favorite color?
    • Your mood on a cat scale?

Mood on a cat scale.

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.

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.

Note: There are several terms in these instructions that might still be unfamiliar to you, for example “repository”, “stage” or “commit”. Don’t worry, you will learn about what these terms mean in the chapters.

Solutions: Setup

Code
#!/bin/bash

cd city-guide # <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 city-guide # <1>
git init # <2>
touch city-guide.txt # <3>
cat > city-guide.txt <<- EOM # <4>
Hamburg 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.
EOM
git add city-guide.txt # <5>
git commit -m "Add Hamburg Planetarium to city-guide.txt" # <6>
  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.