Git & GitHub training

SFA team’s training

Julien LEBRANCHU

January 30, 2024

Installation and configuration

Installation and configuration

Installing Git [Windows and Mac]

Installation and configuration

Git configuration: register who your are

When done, open Git Bash, type

git config --global user.name "Firstname Lastname"
git config --global user.email "email@adress.org"

N.B. These two lines identify you in the history of a project.

git config --global --list

Installation and configuration

Git configuration with GitHub Desktop: register who your are

  1. In the menu bar, select GitHub Desktop, then click Preferences*.
  2. In the Preferences window, click Git.

More details on Configuring and customizing GitHub Desktop

Training on solo project

Your Turn

  • Open Github Desktop and create a new repository training-git
  • Choose a local path
  • Click on Create Repository

02:00

First Commit

  • Create a README.md file \(\rightarrow\) README.md is now in Workspace but not in Local
  • When you type git status
$ git status
Sur la branche main
Fichiers non suivis:
  (utilisez "git add <fichier>..." pour inclure dans ce qui sera validé)
    README.md

aucune modification ajoutée à la validation mais des fichiers non suivis sont présents (utilisez "git add" pour les suivre)

First Commit

  • Type git add README.md and git status
$ git add README.md

$ git status
Sur la branche main
Modifications qui seront validées :
  (utilisez "git restore --staged <fichier>..." pour désindexer)
    nouveau fichier : README.md

First Commit

  • Type git commit -m ”First commit”

$ git commit -m "First commit"
[main fde1cd2] First commit
 1 file changed, 0 insertions(+), 0 deletions(-)
 create mode 100644 README.md

fde1cd2 is a short version of the identifier of the commit

  • Type git log
$ git log
commit fde1cd295a8403756d655ec6d1f2be9eb908f649 (HEAD -> main)
Author: Julien Lebranchu <julien.lebranchu@ird.fr>
Date:   Fri Oct 20 14:36:19 2023 +0400

    First commit

commit 25681b3e0212f771a4594abc008d5352436a6244
Author: Julien Lebranchu <julien.lebranchu@ird.fr>
Date:   Fri Oct 20 14:35:11 2023 +0400

    Initial commit

Your Turn

  • Create a README.md file from your explorer file
  • With the GitHub Desktop
    • Add the file
    • Commit the changes
  • Explore the interface, specialy history tab.

05:00

Your Turn

  • Add a content in README.md file and commit the changes

02:00

Creating tags

Git has the ability to tag specific points in a repository’s history as being important. Typically, people use this functionality to mark release points (v1.0, v2.0 and so on).

$ git tag -a v1.0.O -m "my version 1.0.0"

Your Turn

  • Open the README.md file

  • Type ## Version v1.0.0.

  • Commit the changes

  • Create a tag v1.0.0 with right click the last commit

02:00

Ignoring files

It is possible to tell Git to ignore some files by using a .gitignore file.

A gitignore file specifies intentionally untracked files that Git should ignore. Files already tracked by Git are not affected.

Example for R project

# History files
.Rhistory
.Rapp.history

# Session Data files
.RData
.RDataTmp

# User-specific files
.Ruserdata

Your Turn

  • Create an empty output.log file
  • Check the status in GitHub Desktop
  • Now create a .gitignore file
    • write *.log.
  • The output.log file no longer appears as Untracked
  • Commit with ”Fourth commit”

03:00

Moving in the history

  • git checkout v1.0.0 → move to a tag
%%{init: {
  "theme": "base"
}}%%
gitGraph
    commit id: "25681b3"
    commit id: "fde1cd2"
    commit id: "9b0dce3"
    commit id: "017fc9d" tag: "v1.0.0" type: HIGHLIGHT
    commit id: "fe96d29"
  • git checkout fde1cd2 → move to the first commit
%%{init: {
  "theme": "base"
}}%%
gitGraph
    commit id: "25681b3"
    commit id: "fde1cd2" type: HIGHLIGHT
    commit id: "9b0dce3"
    commit id: "017fc9d" tag: "v1.0.0"
    commit id: "fe96d29"
  • git checkout main (master) → move at the latest commit
%%{init: {
  "theme": "base"
}}%%
gitGraph
    commit id: "25681b3"
    commit id: "fde1cd2"
    commit id: "9b0dce3"
    commit id: "017fc9d" tag: "v1.0.0"
    commit id: "fe96d29" type: HIGHLIGHT

N.B. HEAD is a symbolic reference pointing to your location in history

Your Turn / Publishing on GitHub

  • Open Github Desktop
  • Click on Publish repository
  • View the repository on https://www.github.com
    • Go to Repository > View on GitHub
    • Explore the Repository

02:00

Your Turn / Resolve a conflict

  • On GitHub, add x = 1 at the end of the README.md file.
  • On your computer, edit the README.md and add x = 2.
  • Open Github Desktop
    • Add README.md
    • Commit Fifth commit
    • Push the commit
03:00

Resolve a conflict

  • This is due to a diverging history that cannot be solved automatically by Git.

  • origin/main corresponding to the version on the remote server

%%{init: {
  "theme": "base"
}}%%
gitGraph
    commit id: "25681b3"
    commit id: "fde1cd2"
    commit id: "9b0dce3"
    commit id: "017fc9d" tag: "v1.0.0"
    commit id: "fe96d29"
    branch origin/main
      commit id: "4ba5ab5"
    checkout main
    commit id: "5af862d"

Resolve a conflict

If we open the README.md file, you should have

## Version v1.0.0.

<<<<<<< HEAD
x = 2
=======
x = 1
>>>>>>> 4ba5ab5f6ba79f7ac53d6bcf7ffc7e883ab469e2

N.B. These are conflicts markers.

Git doesn’t know whether to chose x = 1 or x = 2. This is your job!!

Your Turn / Resolve a conflict

  • Edit the README.md and replace the 5 lines below by x = 3.
<<<<<<< HEAD
x = 2
=======
x = 1
>>>>>>> 4ba5ab5f6ba79f7ac53d6bcf7ffc7e883ab469e2
  • Commit and push the changes
02:00
%%{init: {
  "theme": "base"
}}%%
gitGraph
    commit id: "25681b3"
    commit id: "fde1cd2"
    commit id: "9b0dce3"
    commit id: "017fc9d" tag: "v1.0.0"
    commit id: "fe96d29"
    branch origin/main
      commit id: "4ba5ab5"
    checkout main
    commit id: "5af862d"
    merge origin/main
    commit id: "aad87f1"

Good practices

  • Pull before any work on the project
  • Commit as frequently as possible
  • Write explicit commit message
  • Push regularly

Training on collaborative project

Collaborating on a project

  • Write together an awesome book : THE BEST BOOK

  • Repos : fuzzy-octo-quarto-book

  • Each one will participate to write a chapter.

Your Turn / Clone the repo

  • Open Github Desktop
  • Clone fuzzy-octo-quarto-book, URL : https://github.com/JulienLebranchu/fuzzy-octo-quarto-book
  • Change the current branch to develop
  • Create a branch from develop with your section and your lastname, ie it-gabriel
05:00

Your Turn / Add a chapter

  • Create a file name with the extension “.qmd” in the directory it or stats
  • Add content by copy/pasting from the resources.qmd file
  • Add your filename in the _quarto.yml
  chapters:
    - ...
    - part: stats.qmd
      chapters: 
      - stats/content.qmd
      - stats/XXX.qmd
    - part: it.qmd
      chapters: 
      - it/content.qmd
      - it/XXX.qmd
05:00

Your Turn

02:00

Go further

GitHub and RStudio

We need to link RStudio and GitHub to work togheter without enter the identifier/password each time.

library(usethis)
usethis::create_github_token(description = "R-GITHUB-RSTUDIO")
  • Fill the field “Expiration” with No expiration
  • Click on Generate token
  • Copy the token key : ghp_XXXXXXXXXXXXXX via
library(gitcreds)
gitcreds_set()
  • Paste the key