Session 5: Git Essentials

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 15 2024 (10:15 am)

1 Admin

Behind-the-scenes: Interview about our course

https://lecture2go.uni-hamburg.de/l2go/-/get/v/70364

2 Last session: Basic Git workflow

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

Last session: Basic Git workflow

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

Last session’s learning objectives

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

First steps with Git

💡 You can initialize a Git repository
💡 You can stage and commit changes

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.

3 Live demonstrations

4 This session: Git Essentials

Reading

“Git Essentials”

Learning objectives

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

Tasks

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

  1. Reading: Read the chapter(s) “Git Essentials” 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. Commit at least three changes in city-guide.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 city-guide folder! We will continue to use it in the following sessions.

Today’s team topic

  • Interview each other about your favorite outdoor activity.
  • 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’s your hobby?
    • Do you have a pet?
    • What’s your favorite color?
    • Your mood on a baby yoda scale?

Exercises (from last time)

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 2

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.

Exercises 3

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.

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.

Solutions: Amend a commit

Code
#!/bin/bash

cd city-guide # <1>
echo "- It is housed in a former water tower at the center of Hamburg Stadtpark." >> city-guide.txt # <2>
git add city-guide.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 city-guide # <1>
wget -nv -O hamburg-planetarium.jpg https://upload.wikimedia.org/wikipedia/commons/3/3a/Hamburg_Planetarium_10881zh.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.

5 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

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

Terminology: What are “branches”?

lennartwittkuhn.com/version-control-book/chapters/branches.html

Terminology: What is HEAD?

lennartwittkuhn.com/version-control-book/chapters/first-steps-git.html#head