Session 4: Basic Git workflow

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

1 Admin

When do I have to “Take the quiz”?

  • Many chapters contain a “Take the quiz!” button
  • These are the same quizzes that we assign to your as part of the sessions’ learning objectives.
  • Take the quiz when you feel ready, i.e., when you believe that you reached the learning objectives.
  • Alternative: We’ll let you know during the course when we think it’s a good time to take the quiz.
  • Remember: The quizzes are part of your mandatory course work!

Will we also learn about Python or R?

  • No, not directly, but you are free to use Git to track Python or R files that you use for other projects
  • We will have a Quarto workshop in a later session:

More 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:

“What should my city-guide folder look like?”

At least, a folder with city-guide.txt inside with at least one commit.

Even more questions from last session

“How can I check if my folder is a Git repository?”

  • Use ls -a (to also show hidden files) and look for the .git folder

“The command line does not give feedback!”

  • Yes, I’m sorry …
  • Example: After mkdir city-guide, use ls to check if the /city-guide folder was created.

“Why can’t I see my repository on my GitHub account?”

  • You need to explicitly connect the repository on your computer to GitHub.
  • We will cover this topic in the sessions about GitHub (see schedule).

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

Tip if you feel lost: Solutions

Caveats

  • The solutions are just one way to implement the exercises.
  • As always: Copy-pasting solutions might not help your understanding.

2 Last session: Setup and first steps with Git

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: Setup and first steps with Git

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

Last and (partly) this session’s learning objectives

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

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

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: Basic Git workflow

Reading

“First steps with Git” and “Git Essentials”

Learning objectives

First steps with Git

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

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) “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 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 café.
  • 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 paintings scale?

Exercises 1

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