Basics of Git

Introduction to version control for collaborative development

University of Helsinki

Contents

  • What is Git?
  • Why to use Git?
  • Who should use Git?
  • When to use Git?
  • Where to use Git?
  • How to use Git?
    • Hands on exercises

What is Git?

  • Is a software that acts as a version control system used to track changes in files.
  • Helps in collaboration and maintaining the history of project files.
  • Allows you to revert to previous versions if needed.

Why to use Git?

  • Track changes over time.
  • Facilitates collaboration.
  • Enables branching for independent development.
  • Increases security by enabling back-up and redundancy.

Who should use Git?

  • Developers
  • Data specialists
  • Field and lab technicians
  • Researchers

When to use Git?

  • Starting a new project
  • Collaborating
  • Also when working alone!
  • Experimenting
  • Maintaining history
  • Managing
    • code
    • data pipelines
    • reports

Where to use Git?

  • local computer
  • GitHub
  • GitLab
  • Bitbucket

Where to use Git?

Git = version control tool

GitHub/GitLab/Bitbucket = hosting platform

How to use Git?

  • Requirements
  • Important commands
  • Exercises

Requirements to Have Git Running

  • Operating system agnostic

    • Linux
    • Mac
    • Windows

Requirements to Have Git Running

  • Installation is required
    • UH computers might have it installed by default
  • Installing git in different operating systems:
    • Linux: Install via package manager (e.g., sudo apt-get install git).
    • macOS: Install via Homebrew (brew install git) or Xcode Command Line Tools.
    • Windows: Download installer.
  • Git website: https://git-scm.com/

Using Git

Configure User Identity

  • Set your name and email (used in commits):
git config --global user.name "Your Name"
git config --global user.email "you@example.com"
  • HTTPS

    • Use your username and password when prompted.
  • SSH

    • Instructions to set up the SSH can be found in the following links:
  • You can also access the contents by the ZIP file

    • Download Zip file

Git Hosting Services

Accessing the information from a GitHub repository

  • Select the option you are willing to work with (HTTPS, SSH, GitHub CLI).

  • Go to the directory where you want to work in your computer, and clone the repository (make a local copy of it)
git clone git@github.com:haapaniemi/KVJ.git

Creating a GitHub Repository

  • Go to github.com and log in to your account (if you don’t have it, sign in).

  • Create a New Repository

    Click the + icon in the top-right corner.

    • Select New repository from the dropdown menu.

Creating a GitHub Repository

  • Repository Details

    • Add a name for your repository,

    • Add a brief description of your repository.

    • Visibility

      • Choose Public or Private.

Git most common workflow and commands

  • Update Your Local Repository

  • Make Changes

    • Create, modify, or delete files in your working directory.
  • Stage Changes

    • Add new or modified files to the staging area

Git most common workflow and commands

  • Commit Changes

    • Record your staged changes with a descriptive message

  • Share Changes

  • Check Repository Status

Git basic commands

  • git init - Create an empty Git repository or reinitialize an existing one.

  • git clone - Clone a repository into a new directory.

  • git add - Add file contents to the index.

  • git commit-m "message" - Record changes to the repository with an associated message.

  • git push - Update remote refs along with associated objects.

  • git pull - Fetch from and integrate with another repository or a local branch.

Git basic commands

Exercises

  1. Clone a repository
  2. Create a repository on GitLab
  3. Create a new branch with your name initials
  4. Change to your branch and return to the main branch
  5. Add a CSV file into the repository
  6. Make a commit with a clear message
  7. Make a push

Collaboration work (groups of 2 to 3)

  1. Add a dataset (person A)
  2. Make a simple script to modify the dataset (person B)
  3. Make a figure with the updated data (person C)
  4. Synchronize the work (persons A, B and C)

GitLab

University of Helsinki solution

University of Helsinki Version Control System

Loging in with HAKA

GitLab

Creating a new repository

Go to the + symbol and click on New project/repository

GitLab

  • Different options
    • Create blank project
    • Create from template
    • Import project

GitLab

  • Project name
    • Automatically taken as your Project slug
  • Project URL
    • Can be your username in the system (dropdown)
  • Project slug
    • Specific name of the repository
  • Visibility level
    • Private, Internal or Public
  • Project configuration

GitLab

Repository created

Exercise #1

  • Log in to UH’s GitLab using HAKA
  • Create a new repository in GitLab
    • Provide an understandable and clear name for your repository

You have 5 minutes!

GitLab

Cloning the repository

  • Different options
    • Clone with SSH
    • Clone with HTTPS
    • Download source code
      • Not cloning!

GitLab

Cloning

In your computer terminal go to the folder you want to clone the repository.

  • My example:
cd Documents/teaching-UH/git/test-cloning/

GitLab

Cloning

In your computer terminal write the git clone command followed by the repository URL (HTTPS).

  • My example:
git clone https://version.helsinki.fi/allansou/git-course.git

Exercise #2

  • Clone this repository:
https://version.helsinki.fi/allansou/git-course.git
  • Verify if you have the folder and files in your local computer

You have 3 minutes!

GitLab

Branches

  • Branches allow you to safely work in isolation without affecting the main project
  • The default branch in Git is called main
  • Empowers parallel development without conflicts between versions
  • Branches can be created for the work of a new feature or bug fix, for instance.

GitLab

Branches

Verifying the existing branches in the repository:

  • On the folder containing a git project, run:
git branch
  • This will return the branches in the project

GitLab

Branches

Creating a branch is easy, you just need to use the command git branch followed by the name of your new branch.

git branch test

You can confirm the existence of the newly created branch by using the git branch command again:

git branch

1. Introduction — “Why Git?” (5 min)

  • Main ideas
  • Version control
  • Collaboration
  • History and recovery
  • Experimentation without fear
  • Key message: Git is a time machine for your code.

Introduction

Simple examples

  • “Who changed this file?”
  • “Can we go back?”
  • “Can two people work together safely?”
  • Suggested slide
  • Git timeline diagram
  • “Before Git vs After Git”

Inspired by: “About Version Control” in Pro Git

2. Core Git Concepts (10 min)

This is the MOST important section.

If students understand this, Git becomes much easier.

Explain the 3 areas

Use a visual diagram:

  • Working Directory -> Staging Area -> Repository

2. Core Git Concepts (10 min)

Explain:

  • Working directory = your files
  • Staging area = “what will go into the next commit”
  • Repository = saved history
  • Commands
    • git add
    • git commit
    • git status

2. Core Git Concepts (10 min)

Analogy - Working directory = draft - Staging area = selected changes - Commit = saved snapshot

Very important

  • Explain:
  • A commit is NOT “saving a file”.
  • A commit is saving a snapshot of the project.

3. Basic Workflow Demo (15 min)

This should be LIVE if possible.

  • Demo flow
  • Create repo
  • git init
  • Check status
  • git status

Add file - git add hello.txt - Commit - git commit -m “Add hello file”

Modify file

Then:

git diff git add . git commit -m “Update hello file” Show history git log –oneline Important concepts commits history snapshots commit messages Teach GOOD commit messages

Example:

Add login form validation

Avoid:

fix stuff changes Inspired by “Adding a file / Committing a file” “Making presentations / Setting up a repository”

4. Remote Repositories (10 min)

Students MUST understand:

local repo remote repo GitHub/GitLab/Bitbucket Explain visually My Computer <—–> GitHub Core commands Clone git clone Push git push Pull git pull Explain: push = send commits pull = get commits clone = copy repository Mention GitHub

But avoid deep platform-specific details.

Inspired by “Working with Remotes” “Working in a Team Using Git”

5. Branches (10 min)

DO NOT go deep.

Beginners mainly need:

what branches are why they exist basic workflow Explain visually main
feature-login Explain:

Branches allow isolated work.

Basic commands git branch feature-login git checkout feature-login

or modern:

git switch -c feature-login Explain merge conceptually

No need for advanced merge strategies.

Very important mental model

Branches are cheap and safe.

Inspired by “Branches in a Nutshell” “Working with branches”

6. Common Mistakes & Survival Commands (5 min)

This section reduces fear.

Teach: git status

The “panic button”.

Also: git restore git log

Optional:

git stash Key message

Git is hard mainly because people panic.

7. Final Best Practices (5 min)

Very lightweight.

Good practices Commit often Write meaningful commit messages Pull before pushing Use branches Never work directly on main Mention:

Git is learned by usage, not memorization.