Howto git

Guillaume Gay, CENTURI multi-engineering

Outline

Git as a single user

  1. Getting started

  2. Git as an archive

  3. How to revert mistakes

  4. Backing up to github

Github and Gitlab, multi-user collaboration

  1. Branches and forks

  2. pull requests

  3. Issues and bug reports

  4. introduction to Continuous Integration

git as a single user

Getting started

1. Install git

or brew install git

2. Install a GUI (optional)

3. Configure your name and email

In a terminal (“git bash” on Windows)

git config --global user.name "Guillaume Gay"
git config --global user.email "guillaume.gay.1@univ-amu.fr"

4. Create your first project

  • Create a directory (called e.g. Documents/GitTuto)
  • in the terminal cd to that directory:
cd Documents/GitTuto
  • The directory is empty:
ls -la
  • Tell git to start tracking this dir
git init
  • Now the directory isn’t empty
ls -la
tree .git/

You can do this on an existing directory

The .git directory contains all git needs to track your project

  • !! .git might contain lots of small files

  • !! Delete .git → lose history

  • You can move the whole directory (with the .git subdir.)

status

To know what is going on at any time, type:

git status

git as an archive manager

add

  • Create a (text) file, e.g. README.md
  • Write “This is a git tutorial” in it.
git status
git add README.md
git status

Now git knows about your file

commit

git commit -am "My first commit"

The message is mendatory

git status

Git registered your file

diff

Edit README.md (add some text) and

git status

You can see there are untracked changes

git diff shows what changed.

git diff
git commit -am "My second commit"
git status

Your changes were registered (wash, rince, repeat)

log

  • log: what happened before now
git log

.gitignore

  • Create a file called tmp_file.txt
git status
  • Create a file called .gitignore and write tmp_* in it.

Only .gitignore is listed as untracked

It’s good to have a direct read of the state of your code (in your editor or terminal)

how to cancel and revert mistakes

There are two commands depending on your git freshness

older git ( before 2.23 )

git checkout README.md

newer git ( after 2.23 )

git restore README.md

Those undo changes made since latest commit

You can restore older versions of the file.

For exemple to restore a file at a certain commit, you can reference this commit by its hash:

git restore -s ae2fd12 README.md

You can find a commit’s hash with git log

There are plenty of more powerfull things you can do, but I don’t know / need them!

Backing up to github

  1. Create an account

  2. Manage SSH

git communicates with public / private keys. To make it easier, we register a key on our github account.

ssh-keygen # creates a key

On github, go to settings > SSH and GPG Keys > New SSH key

Copy the content of .ssh/id_rsa.pub there.

Local first

remote add

  • Create a new (empty) “GitTuto” repository on github

  • Copy the repo URL

  • Declare it as a remote

git remote add origin git@github.com/glyg/GitTuto.git
  • Change your principal branch from master to main
git branch -M main

push

  • Publish the local files to github
git push -u origin main

Make new changes, commit and push

git commit -am "changed README.md"
git push

pull

  • Make a change on README.md on github & commit there
git pull
git log

Distant first

clone

  • In another place on your computer (or on another computer):
git clone git@github.com/glyg/GitTuto.git
cd GitTuto
git status
git remote show origin

Summary

Branches and how to merge them (2nd session)

branches

branches are cheap, use them!

Collaboration with Github and Gitlab

One user, several repositories

Two repos

One user, several repositories

And a remote

Two users, several repositories

Enters Alice

Two users, several repositories

Let’s share!

Two users, several repositories

Merge requests

Issues

A word on Continuous Integration

  • Automates tests

  • No more ‘but it works on my machine :/’

  • Can be tricky to setup

Questions, comments, suggestions:

github.com/centuri-engineering/git_tuto

Slack Channel:

centuri-livingsystems.org/multi-engineering-platform

// reveal.js plugins