Formation Git

DevOps/Version Control Systems · notes

GIT

YASSINE NOURI

UVT/ISI 2021

Réf: Wei Wang: Formation Git

PLAN

Théorie GIT

Les commits

Les branches

Les dépôts distants

@YNOURI DEVOPS UVT/ISI

11/23/2021

2

CODONS À PLUSIEURS !

Versionner son projet avec Git

POURQUOI UTILISER GIT ?

Objectifs :

 travailler à plusieurs sans se marcher dessus : indispensable pour les

projets en équipe

 garder un historique propre de toutes les modifications : on

organise son travail sous forme de “commits” documentés

@YNOURI DEVOPS UVT/ISI

11/23/2021

4

LA THÉORIE DE GIT

3 zones, 3 ambiances

Les modifications sont sauvegardés 3 fois

Working directory

Index: Staging

Local repository

C’est la zone de travail : les

fichiers tout juste

modifiés sont ici

Zone qui permet de stocker

les modifications séléctionées

en vue d’être commitées

Code commité, prêt à être

envoyé sur un serveur

distant

git add

git commit

@YNOURI DEVOPS UVT/ISI

11/23/2021

5

LES COMMITS

 Commit : ensemble de modifications cohérentes du code

Un bon commit est un commit :

C’est quoi concrètement un commit ?

❖ qui ne concerne qu’une seule

fonctionnalité du programme

❖ le plus petit possible tout en restant

cohérent

❖ Idéalement qu’il compile seul

❖ une différence (ajout / suppression de

lignes)

❖ des méta-données (titre, hash, auteur)

@YNOURI DEVOPS UVT/ISI

11/23/2021

6

ALLER JUSQU’AU COMMIT

 Où j’en suis dans mes 3 zones ?

git status

On branch master

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

Publicité

(use "git checkout -- <file>..." to discard changes in working directory)

modified: views/add_commentaire.php

no changes added to commit (use "git add" and/or "git commit -a")

@YNOURI DEVOPS UVT/ISI

11/23/2021

7

ALLER JUSQU’AU COMMIT

 Visualiser les différences entre le working directory et l’index

git diff

diff --git a/views/add_commentaire.php b/views/add_commentaire.php

index e69de29..8cff573 100644

--- a/views/add_commentaire.php

+++ b/views/add_commentaire.php

@@ -0,0 +1,6 @@

<?php include('header.php'); ?>

+<form action="/commentaire/ajouter" method="post">

+ <p>Pseudo : <input type="text" name="pseudo"/></p>

+ <p>Commentaire : <textarea name="commentaire"></textarea></p>

+</form>

@YNOURI DEVOPS UVT/ISI

11/23/2021

8

ALLER JUSQU’AU COMMIT

 Ajouter mes modifications à la zone de staging (index)

git add views/add_commentaire.php

git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: views/add_commentaire.php

@YNOURI DEVOPS UVT/ISI

11/23/2021

9

ALLER JUSQU’AU COMMIT

 Récapitulatif des commandes

affichage

différences

git diff

git diff --staged

working directory

index

git repository

ajouter des

modifications

git add mon_fichier

git add -p (interactif)

git add -A (tout ajouter)

git commit -m “message”

@YNOURI DEVOPS UVT/ISI

11/23/2021

10

REVENIR AU DERNIER COMMIT

git status

On branch master

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git checkout -- <file>..." to discard changes in working directory)

modified: model/commentaires_model.php

no changes added to commit (use "git add" and/or "git commit -a")

@YNOURI DEVOPS UVT/ISI

11/23/2021

11

REVENIR AU DERNIER COMMIT

 Enlever des modifications dans le working directory

Publicité

git checkout -- monfichier

git status

On branch master

nothing to commit, working directory clean

@YNOURI DEVOPS UVT/ISI

11/23/2021

12

DÉSINDEXER DES FICHIERS

git status

On branch master

Changes to be committed:

(use "git reset HEAD <file>..." to unstage)

modified: model/commentaires_model.php

@YNOURI DEVOPS UVT/ISI

11/23/2021

13

DÉSINDEXER DES FICHIERS

git reset HEAD monfichier

git status

On branch master

Changes not staged for commit:

(use "git add <file>..." to update what will be committed)

(use "git checkout -- <file>..." to discard changes in working directory)

modified: model/commentaires_model.php

no changes added to commit (use "git add" and/or "git commit -a")

@YNOURI DEVOPS UVT/ISI

11/23/2021

14

ALLER JUSQU’AU COMMIT

 Récapitulatif des commandes

working directory

index

git repository

git diff

git diff --staged

git add monfichier

git commit -m “titre”

git commit -am “titre” (si le fichier a déjà été indexé)

git checkout -- monfichier

git reset HEAD monfichier

git reset --hard

@YNOURI DEVOPS UVT/ISI

11/23/2021

15

LES BRANCHES

 branche : pointeur vers un commit

 une branche principale : master

 Branche courante : HEAD

master*

C1

C2

C3

fonctionnalité

C4

C5

 En général, une branche par fonctionnalité en cours de développement

@YNOURI DEVOPS UVT/ISI

11/23/2021

16

GESTION DES BRANCHES

 Création et modification de branches

git branch : affichage des branches

git branch ma_branche : créer la branche ma_branche

git checkout ma_branche : déplace HEAD vers ma_branche

Publicité

 ou pour simplifier...

git checkout -b ma_branche : créer la branche

ma_branche et déplace HEAD dessus

@YNOURI DEVOPS UVT/ISI

11/23/2021

17

GESTION DES BRANCHES

 Merge : intégration des modifications d’une branche dans la branche courante

git merge ma_branche: merge ma branche dans la branche

courante

master*

C1

C2

C3

C1

C2

C3

master*

C6

bugFix

C4

C5

bugFix

C4

C5

@YNOURI DEVOPS UVT/ISI

11/23/2021

18

LA ZONE : STASH

 Le stash ça sert à :

 Sauvegarder les modifications du working directory dans une zone tampon pour rendre le working directory

propre.

 Possibilité de rejouer les modifications stashées n’importe où

 Peut être vu comme une zone de brouillons

@YNOURI DEVOPS UVT/ISI

11/23/2021

19

STASH: LES COMMANDES

On stash un ensemble de modifications

git stash

On récupères les modifications stashées

git stash apply

Pour plusieurs stashs :

git stash list

git stash apply stash@{id}

Effacer le contenu du stash

git stash clear

@YNOURI

DEVOPS

UVT/ISI

11/23/2021

20

TODO : JOUER AVEC LES COMMITS ET LES BRANCHES

 Vous pourrez vous entrainer:

http://onlywei.github.io/explain-git-with-d3/

@YNOURI DEVOPS UVT/ISI

11/23/2021

21

LES DÉPÔTS DISTANTS

Centraliser les données sur un dépôt git !

LES DÉPÔTS DISTANTS

 Git directory sur un serveur distant pour le travail collaboratif

➢Github

Publicité

➢Gitlab

 Pourquoi gitlab ?

➢ code review, merge request, interface web, ...

@YNOURI

DEVOPS

UVT/ISI

11/23/2021

23

LES DÉPÔTS DISTANTS

 Cloner un dépot distant : crée un dossier et récupère les fichiers

git clone <url>

 Envoie notre local repository sur le dépôt distant

git push

 Récupère les commits sur le dépôt distant et met à jour le working directory

git pull

@YNOURI DEVOPS UVT/ISI

11/23/2021

24

LE SCHÉMA DE BASE DE GIT

working directory

index

git repository

remote git

repository

git diff

git diff --staged

git add

git commit

git checkout --

git reset

git reset --hard

git pull

git push

git fetch

@YNOURI DEVOPS UVT/ISI

11/23/2021

25

INSTALLATION DE GIT

 Sous windows :

➢ https://git-for-windows.github.io/

➢ http://babun.github.io/ (Emulateur de shell)

 Sous OS X :

➢ http://sourceforge.net/projects/git-osx-installer

 Sous Linux :

➢ <votre package manager> install git

➢ ( apt-get, rpm, …)

@YNOURI DEVOPS UVT/ISI

11/23/2021

26

CONFIGURATION MINIMALE LOCALE

git config --global user.name "Prénom Nom"

git config --global user.email “[email protected]

En option mais c’est mieux :

git config --global color.ui true

git config --global color.diff.meta yellow

@YNOURI

DEVOPS

UVT/ISI

11/23/2021

27

THANK YOU

[email protected]