Session 6: Branches

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

1 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

I need help!

Help yourself

  • Look at the solutions on the exercises page in the Version Control Book (or on each session’s slides)
  • Start fresh with a “prepared” repository: Download from the exercises page in the Version Control Book

We help you

  • Let us know! Ask questions!
  • Send an email with questions!
  • Ask for an office hour!

Fellow students help you

  • This is a team exercise: We want everyone to keep up!
  • Don’t be shy and ask for help from your session partner.
  • If you notice that your session partner needs help, please help them first (you will also deepen your knowledge by helping others).

Questions from last session

“How do we submit the exercises?”

  • During later sessions, you will learn how to share your city-guide repository with us via GitHub.
  • How you implemented the exercises should be registered as Git commits in your repository.
  • Therefore, we can - retrospectively - check if and how you implemented the exercises (yay, version control!).

“How can I make changes in a text (.txt) file?”

  • Remember: It’s just a regular text file on your computer.
  • Option 1: Use a regular text editor (e.g., TextEdit on macOS, Notepad++ on Windows).
  • Option 2: Use vim directly from the command line (see chapter / session on “Setup”).
  • Option 3: Use command line commands like echo "My text" >> city-guide.txt.

“I ran git commit but nothing happened?”

  • Maybe you forgot to run git add before?

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

2 Last session: Git Essentials

Last session: Git Essentials

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

Last sessions’s 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

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.

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

3 This session: Branches

This session: Branches

Figure 1: Image from Chapter “Git Branches” of the “The Turing Way handbook to reproducible, ethical and collaborative data science”, used under a Creative Commons Attribution 4.0 License.

Reading

“Branches”

Learning objectives

Branches

💡 You understand the purpose and benefits of using branches in Git.
💡 You can create and switch between branches.
💡 You can merge branches and resolve merge conflicts.
💡 You can name at least three best practices when working with branches.

Tasks

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

  1. Reading: Read the chapter(s) “Branches” 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 created a new branch and merged changes into your default branch (main or master).
  2. You created and resolved a merge conflict.

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 activity involving water.
  • 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 an animals scale?

Exercises 1

Create and merge a new branch

  1. If needed, navigate to the project repository using the command line.
  2. Create a new branch called feature.
  3. Switch to the new branch.
  4. Add a new entry to your project text file.
  5. Stage and commit the changes to the project text file on the feature branch.
  6. View the contents of project text file to verify your changes.
  7. Switch back to the default branch (main or master).
  8. View the contents of the project text file again to confirm that the previous changes do not exist on the default branch.
  9. Merge the feature branch into your default branch.
  10. Delete the feature branch.
  11. View the contents of project text file yet again to confirm that the previous changes have been merged into the default branch.

Exercises 2

Create and resolve a merge conflict

  1. Deliberately create a merge conflict by editing the same section of a file on two separate branches and attempting to merge them. An example can be found in the branches chapter.
  2. Resolve the merge conflict.
  3. Delete the merged branch afterwards.

Solutions: Create and merge a new branch

Code
#!/bin/bash

cd city-guide # <1>
git branch feature # <2>
git switch feature # <3>
cat >> city-guide.txt <<- EOM # <4>

Paddle tour of the Alster canals

- If you're not afraid of the water and would like to enjoy a really super day whilst at the same time doing a bit of upper body exercise, then why not pick up a paddle and work your way down the Alster canals in a canoe.
- Just go to any one of the countless boat and canoe hire points on the Alster to begin your adventure.

taken from https://www.hamburg-travel.com/see-explore/sports-recreation/sports-physical-activity/paddle-tour-of-the-alster-canals/
EOM
git add city-guide.txt # <5>
git commit -m "Add paddle tour of the Alster canals to city-guide.txt" # <5>
cat city-guide.txt # <6>
git checkout main # <7>
cat city-guide.txt # <8>
git merge feature # <9>
git branch -d feature # <10> 
cat city-guide.txt # <11>
  1. Optional: Navigate into the project repository using cd (or a similar path).
  2. Create a new branch called feature using git branch feature.
  3. Switch to the new branch using git switch feature. You can also create and switch the branch in one step using git checkout -b feature.
  4. Add a new entry to your project text file file. You can use your regular text editor. Here, we add a new entry from the command line using cat.
  5. Stage and commit the changes to your project text file using git add and git commit.
  6. View the contents of your project text file to verify your changes. Here, we use the cat command again.
  7. Switch back to the default branch (main in this example). Here, we use git checkout main but you can also use git switch main.
  8. View the contents of your project text file again to confirm that the previous changes do not exist on the main branch.
  9. Merge the changes feature branch into the main branch.
  10. Delete the merged feature branch using git branch -d feature.
  11. View the contents of your project text file yet again to confirm that the previous changes have been merged into the main branch.