Introduction to Git and GitLab (22.04.2025 - 23.04.2025, 09:00 - 15:00)

Organizational Details

Day 1: Introduction to Git

We have a 15 minutes break very 90 minutes.

Day 2: Introduction to GitLab

We have a 15 minutes break every 90 minutes.

Before the Workshop

During the Workshop

Open Questions

Is it possible to change the commit message later?

How to disable the pager mode when running git log / diff etc.?

You can control the behaviour using the configuration options core.pager. From the Git manual:

core.pager

This setting determines which pager is used when Git pages output such as log and diff. You can set it to more or to your favorite pager (by default, it’s less), or you can turn it off by setting it to a blank string:

$ git config --global core.pager ''

If you run that, Git will print the entire output of all commands, no matter how long they are.

Day 1: Introduction to Git

Welcome and Introduction

Preparing the working environment

Overview

In the following, we prepare our working environment to properly use Git from the shell.

Open a shell

A shell (also known as: terminal, command line, console) is a program which allows you to run commands on your computer and to receive their output. We use Git from the shell because it clearer shows the way how Git works.

Prepare your working environment

In the following, we prepare our working environment and try out other useful shell commands:

:bulb: Additional tips

Key points

At the end you should have prepared the following:

Introduction to version control

Overview

In the following, we provide an overview about the main features of the version control system Git.

“Final”.doc?

Please consider the following example review process:

"Final".doc

“Piled Higher and Deeper” by Jorge Cham www.phdcomics.com

Quite often such a review process results in a number of similar files with inconspicuous changes. In addition, it is hard to figure out the recent version manually. In such scenarios, tools such as Git can be quite helpful.

Key points

Setting up Git

Overview

In the following, we perform the minimal initial Git setup.

Set your default username and email address

At least, we have to tell Git about our identity because this information is later associated with our commits. Git uses the username and email address settings to represent your identity when creating commits:

:bulb: Configure username and email consistently

Your username and email address are part of the version history of your repository. Please make sure that you configured them consistently across your (client) systems. Usually, you do not want to change this information later because changing it potentially breaks the version history for your collaborators.

:bulb: How are configuration settings handled in Git?

Git maintains three levels of configuration settings:

You can specify a setting on multiple levels. In such a case, Git uses the setting defined on the lowest level. For example, you defined your default username you on the global level and you-special for the special Git repository. If you work in the special repository, Git will use the username you-special. In all other repositories, Git uses the username you.

:bulb: Find out more about the Git configuration settings

Key points

Creating a repository

Overview

In the following, we create the Git repository which we use throughout the workshop.

Create the planets Git repository

Imagine we work at Universal Missions. Our famous new clients - Dracula, Wolfman and the Mummy - are looking for potential planets to migrate to. We are here to help them make a good decision. For that reason, we collect relevant information about different planets to support them. We store this information in text files and manage them with Git.

The Software Carpentries, CC-BY 4.0

Now, let us create the planets Git repository:

Key points

Tracking changes

Overview

In the following, we introduce the basic Git change workflow in context of our example.

First commit cycle: Add a new file to the repository

First, we want to start collecting information about planet Mars:

Congratulations - You performed your first commit!

Second commit cycle: Change an existing file of the repository

Now, we want to collect further information about Mars and focus on Wolfman:

We performed the basic Git change workflow for the second time:

Basic Git Change Workflow

The Software Carpentries, CC-BY 4.0

:bulb: Excurse: Writing good commit messages

Third commit cycle: Local vs. staged changes

Finally, we want to add information about the Mummy:

We performed the basic Git change workflow for the third time:

Basic Git Change Workflow

The Software Carpentries, CC-BY 4.0

:bulb: Additional git add tips

Quiz time

:question: Choosing a commit message

Which of the following commit messages would be most appropriate for the last commit made to mars.txt?

  1. “Changes”
  2. “Added line ‘But the Mummy will appreciate the lack of humidity’ to mars.txt”
  3. “Discuss effects of Mars’ climate on the Mummy”

:question: Committing changes to Git

Which command(s) below would save the changes of new-file.txt to the local Git repository?

  1. git commit -m "Add recent changes"
    
  2. git init new-file.txt
    git commit -m "Add recent changes"
    
  3. git add new-file.txt
    git commit -m "Add recent changes"
    
  4. git commit -m new-file.txt "Add recent changes"
    

Key points

Exploring history

Overview

In this episode, we want to work with the version history and explain how you can restore older versions.

Working with the version history

Let us change the mars.txt again and see how we can compare a modification with different committed versions:

HEAD~<NUMBER> allows you to navigate the commit history relatively starting from the most recent commit. In addition, you can use the commit ID directly to reference a specific version:

You can usually use a commit ID or a HEAD based expression, if a Git commands expects a reference to a commit.

Restore older versions

Imagine that we changed our mind about our last change and want to discard it:

Now, we want to restore an even older version of mars.txt:

Finally, we want to restore the last committed version again:

:bulb: Discard local changes

The commands git restore <filename> or git restore -s <commit ID> <filename> might permanently remove your local changes without the chance to undo this.

:bulb: Discard staged changes

You can use git restore -S <file> or git restore --staged <file> to remove changes from the staging area. Afterwards, the changes are still there but no longer staged for the next commit. In the next step, you could discard the changes entirely by using git restore <file>.

Key points

Ignoring things

Overview

Sometimes, you want to make sure that certain files such as temporary files or files containing sensitive data are not added accidently to your Git repository. Git allows you to define .gitignore files for this purpose.

Create some temporary files

First, we create some temporary files which we do not want in our repository:

In such a situation, you can quickly do mistakes and accidently add some of these files to your repository. For example, if you use git add ..

Add a .gitignore file

We can define which files we want to ignore by adding a .gitignore file:

Finally, we add the .gitignore file permanently to our repository:

From now on, everyone who works with your repository already has the proper definition of ignored files.

:bulb: Tips for working with .gitignore files

Key points

Working with branches

Overview

In the following episode, we introduce the concept of branches and how you work with them on the basis of the feature branch workflow.

A branch is a specific series of commits in the repository:

Branching is a general concept of version controls systems such as Git. There are different reasons to use branches, for example, working in isolation on a specific task. Patterns such as the feature branch workflow provide guidance on how to use branches in a meaningful way.

The main ideas of the feature branch workflow are:

The feature branch workflow is used quite often in context of Git. It also provides the basis for features of code collaboration platforms such as “merge requests” (GitLab) and “pull request” (GitHub).

Working with the feature branch workflow

In the following, we contribute more information about the Mummy concerning Mars by using the feature branch workflow. There are a couple of things which we want to prepare before we start.

  1. Check that you are in the right place:
    • pwd - Should indicate that you are in the planets directory.
    • git status - Should indicate that your repository does not contain any modifications.
  2. Rename your branch to main if needed:
    • If the output of git status starts with On branch master, you need change the branch name as follows:
      • git branch -m master main - Manually renames your branch from master to main.
      • git status - Should now indicate that you are On branch main.
  3. Configure some useful details:
    • git config --global alias.graph "log --oneline --graph --all --decorate" - Configures a new Git command git graph which you can use to see a comprehensive summary of the version history of your repository across all branches.
    • git config --global commit.verbose true - Configures Git to show the changes of a commit when Git launches the editor on committing time.

Now we are ready to start! git graph should indicate a version history similar to the following figure:

Initial repository with four commits

Set up the feature branch

We create a new branch named mummy-info:

We have created our feature branch but we still have to switch to it:

Now, we are ready to start working on our actual task.

:bulb: Tips for switching branches

:bulb: How do I know on which branch I am?

There are several options:

Perform the work on the feature branch

We perform two commits on the feature branch:

We are done with our task. Afterwards, git graph should indicate a version history similar to the following figure:

mummy-info branch with 2 additional commits

Simulate ongoing work on the main branch

Imagine that someone else changed mars.txt and integrated the changes back to main. We perform this change directly on main for demonstration purposes.

git graph indicates a split in the commit history because there are two different commits which are based on the same parent commit:

split commit history

Merge the feature branch

Now, we want to integrate our work back into the main branch. There are several ways to do this. In this episode, we use the standard way via git merge which preserves the branching structure.

Let us resolve the merge conflict:

:bulb: Configure another editor

git graph shows that both branches have been merged:

merge commit

The merge operation resulted in a so called merge commit:

Finally, we want to clean up the feature branch:

Key points

Wrap Up

Day 2: Introduction to GitLab

Welcome and Introduction

Introduction to GitLab

Overview

In the following, we start using GitLab and take our first steps.

GitLab is a code collaboration platform built on top of the version control system Git. It is focused on software development and adds features for managing projects, team collaboration, and automating tasks around the core Git features. It is a purely Web-based application which means that you are not forced to install Git. However, it can be practical though depending on the project.

There are different similar systems available:

:bulb: Which GitLab am I using?

While there is also the official GitLab cloud service, companies and research institutions often install GitLab on their own infrastructure. Thus, there many different GitLab instances:

In addition, these instances can vary concerning the available feature set. On the one hand, there are certain features that require substantial infrastructure and special configuration. On the other hand, there are different GitLab tiers. Besides the free, open source tier, GitLab offers different paid tiers as well. These GitLab tiers offer different feature sets.

For that reason, please make sure that you understand which GitLab instance is typically used in your organization!

First steps in GitLab

Now, we want to start working with the GitLab Web interface. In this workshop, we are using the following GitLab instance: Helmholtz GitLab Instance.

:bulb: Some general tips

Key points

Remote Repositories with GitLab

Now, we want to collaborate with others via GitLab.

Publish the local Git Repository in GitLab

Set up a personal access token

In the following, we will set up an access token that you can use to authenticate via your Git client.

Please note that you cannot retrieve the access token after you closed the Web page. But this is actually no problem as you can remove / add new access tokens at any time.

Create a new GitLab Project

Push the local Git Repository to your GitLab Project

Hint: --set-upstream git push --set-upstream origin main Username for ‘https://codebase.helmholtz.cloud’: token Password for ‘https://token@codebase.helmholtz.cloud’: Now enter your access token as the password. This does not appear in the window, do not pay attention, simply press Enter.

Hint: Configure a credential manager

In general, on Windows and Linux (in a window manager) a credential manager ist used to cache your password. If it does not work, you can configure a credential manager.

https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage#_credential_caching

Synchronize the local Git Repository with changed Content

Adapt the mars.txt via the GitLab Web Interface

Synchronize your local Git Repository

Contribution Workflow: Issues and Merge Requests

Why bother?

An Example Contribution Workflow

A contribution workflow regulates how changes to the files of the project get planned, agreed upon, executed and verified.

In this course, a workflow will be presented that is especially suited for GitLab (or GitHub) and can be applied for many types of projects. The following is an overview, the details will be subject of the following episodes.

  1. Open an issue
    • Detail the intended changes
    • Notify potential stakeholders
  2. Discuss the details
    • Clarify open questions.
    • When working face-to-face this can be kept to a minimum. Write down the decisions that were made in meetings or attach a protocol.
  3. Open a new merge request
    • This signals that someone is working on it
    • A merge request automatically generates a Git branch to be used.
  4. Make iterative changes
    • Add commits to the branch
    • When satisfied request a review of the changes made
    • Adapt according to the reviews
  5. Once the reviewers approved the changes, merge the working branch into the main branch of the project (or any other target branch for that matter)

As a rule of thumb:

Issues

Live Coding: Create an Issue

How to Write Good Issues

Writing good issues is a valuable soft-skill for teamwork. A good issue draws attention, has high information density and provides all details to get started with solving it.

When and How to Close Issues

Labels

Example Use Cases for Labels

Live Coding: Create Labels

Other Notable Features

Merge Requests

What are Merge Requests

Creating Merge Requests

Making Changes

Interaction

The interaction principles (commenting, adding files, referencing) are similar to the ones already seen for issues. Particularly, when performing reviews, sticking to a good review practice makes the difference between reviews being a dreaded nuisance or a valuable team experience. We refer to Philpp Hauer’s Blog Post on Code Reviews for further information.

Live Demonstration: Contribute a README

The README is the front page of your project. You can learn more about important files here: https://codebase.helmholtz.cloud/hifis/software/education/hifis-workshops/foundations-of-research-software-publication/workshop-materials/-/blob/master/episodes/03_add-essential-documentation.md

Contribution Workflow: Team Exercise

Now we want to try out the full contribution workflow practically in pairs of two persons!

Task:

General Process

Task and Workflow Description

Grant the Contributor access to your GitLab Project (OWNER)

Contribute Information about Venus (CONTRIBUTOR)

Review the Contribution and Merge it (OWNER)

Update your local Git Repository (CONTRIBUTOR)

Wrap Up

Further readings

License

The workshop material is based on the swcarpentry/git-novice: Software Carpentry: Version Control with Git, June 2019 lesson which has been created by the Software Carpentries and has been further developed by the German Aerospace Center (DLR) and the Helmholtz-Zentrum Dresden-Rossendorf (HZDR).

If not noted otherwise, all content is licensed under CC-BY 4.0. A detailed overview about the copyright holders and licenses can be found in the workshop materials repository.