Hands on git

Antoine Fontaine

3rd of March, 2022

Hands on git:
Committing, sharing changes and collaborating


What is git?

Summary of basic commands

  • add, commit
  • status, diff, show

Handling branches

  • switch
  • merge

Using git with other people

  • pull
  • push

Handling small mistakes

  • commit --amend
  • reset HEAD^

What is git?

Git is a distributed version control system.

  • distributed
  • version control

Let’s get started!

First we need a repo

Creating a repo

git init

Get an existing repo

git clone https://gitlab.epfl.ch/afontain/test-project.git

Add files to be tracked by git

git add hello-world.py

Visualizing: seeing what is the current state of things

git status
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   hello-world.py

Create a commit

git commit -m "My commit message"

(if you don't use -m, you will be put in a text editor to write the message)

What is a commit?

A commit is when you “save” a version of your files

  • you might think of it as a “savepoint”
  • you might think of it as a “snapshot”
  • you might think of it as a “version”

Some theory about commits

A commit is the unit of change. It roughly contains:

  • the changes of the files
  • the author
  • the identifier (hash) of the parent commit(s)

The value of all these fields will give to your commit its hash, which is used to identify the commit.

Visualizing: seeing the last commit

git show
$ git show
commit 6a195fc8a9ce6059e5db4b51303bde0334473687 (HEAD -> main)
Author: Antoine Fontaine <antoine.fontaine@epfl.ch>
Date:   Sun Feb 20 18:03:47 2022 +0100

    Add hello-world.py

diff --git a/hello-world.py b/hello-world.py
new file mode 100644
index 0000000..e94acca
--- /dev/null
+++ b/hello-world.py
@@ -0,0 +1 @@
+print("hello, world!")

It's time to work on your project and make changes

(here we'll add a line to our file)

Visualizing: seeing the changes made since the last commit

git diff
$ git diff
diff --git a/hello-world.py b/hello-world.py
index e94acca..0672c0c 100644
--- a/hello-world.py
+++ b/hello-world.py
@@ -1 +1,2 @@
 print("hello, world!")
+print("this is a simple python program")

Create a commit, part 2

If you modify a file tracked by git, you can commit it by specifying with the command line

git commit hello-world.py -m "My other message"

A story of trees

blackboard time!

Remember the git logo? that's a tree with branches.

Switching branch or creating one

git switch dev

or, if it doesn't exist already

git switch --create mybranch

Visualizing: listing branches

git branch
$ git branch
* dev

Integrating changes

git merge some-feature

You typically switch to master before doing this

(This will be covered in detail in the next talk)

Visualizing: the killer tool

tig --all
2022-03-02 11:52 +0100 Antoine Fontaine M─┐ [main] Merge branch 'some-feature'
2022-03-02 11:50 +0100 Antoine Fontaine │ o [some-feature] checking the comput
2022-03-02 11:51 +0100 Antoine Fontaine o │ some changes on main
2022-03-02 10:53 +0100 Antoine Fontaine o─┘ [dev] My other message
2022-03-02 10:48 +0100 Antoine Fontaine I Initial commit


Here we'll introduce GitLab as the hosting platform

  • Go to https://gitlab.epfl.ch
  • Login with your EPFL account
  • Create a project for your group

Add your SSH key

Here, I won't post steps, raise your hand and ask for help, or look it up on the internet

You only have to do this once

Get your copy of the repository

Updating a branch to the latest remote version

git pull

You typically use git switch main before running it

Sending your branch to the remote

git push

Recommandation for collaborating

  • Don't commit on main
  • Have one person responsible for main
  • That one person will merge stuff
  • Before working on a new feature, switch to master, pull and create a new branch from that

Intermediate Git Talk

To learn more about Git come to the talk on
Thursday 7th of April

See https://gnugen.ch


Workshop Time!