61

Introduction à git

Embed Size (px)

DESCRIPTION

Une introduction à git, présentée par Yann Sionneau aux membres de l'association MiNET et du club INTech. La qualité du lecteur PDF sur slideshare laissant à désirer, je vous invite à télécharger le PDF plutôt que de le lire sur le site.

Citation preview

Page 1: Introduction à git

1/61

Introduction à git

Yann Sionneau

int lab;

16 juin 2011

Page 2: Introduction à git

2/61

Commandes basiques

Usages un peu plus avancés

Usages experts

Page 3: Introduction à git

3/61

Plan

Commandes basiquesgit initgit addgit commitgit add le retourgit loggit rmgit checkoutEn résumé

Usages un peu plus avancésgit clonegit branchgit checkout le retourgit stashgit mergegit pushgit pullEn résumé

Usages expertsgit add la résuréctiongit rebasegit remotegit bisect

Page 4: Introduction à git

4/61

git init

I On créé le dépot git

Page 5: Introduction à git

5/61

git init

yann@obiwan : /git-tuto$ git initInitialized empty Git repository in /home/yann/git-tuto/.git/

Page 6: Introduction à git

6/61

git add

I On rajoute des �chiers dans une zone tampon (ou index)

I Le contenu de cette zone sera inclu dans le prochain commit

I Git traquera dorénavent les modi�cations sur ces �chiers

Page 7: Introduction à git

7/61

git add

yann@obiwan : /git-tuto$ echo �ceci est le README� > READMEyann@obiwan : /git-tuto$ echo �apprendre git� > TODO

Page 8: Introduction à git

8/61

git add

yann@obiwan : /git-tuto$ git status# On branch master## Initial commit## Untracked �les :# (use �git add <�le>...� to include in what will be committed)## README# TODO#nothing added to commit but untracked �les present (use �git add�to track)

Page 9: Introduction à git

9/61

git add

yann@obiwan : /git-tuto$ git add TODO README

Page 10: Introduction à git

10/61

git add

yann@obiwan : /git-tuto$ git status# On branch master## Initial commit## Changes to be committed :# (use �git rm --cached <�le>...� to unstage)## new �le : README# new �le : TODO#

Page 11: Introduction à git

11/61

git commit

I Sauvegarde le contenu de la zone tampon (ou index oustagging area)

I Elle est insérée dans l'historique de la branche

I Chaque commit a un identi�ant (hash SHA1)

Page 12: Introduction à git

12/61

git commit

yann@obiwan : /git-tuto$ git commit -m �commit initial�[master (root-commit) 6d52464] commit initial2 �les changed, 2 insertions(+), 0 deletions(-)create mode 100644 READMEcreate mode 100644 TODO

Page 13: Introduction à git

13/61

git add le retour

I git add met les �chiers dans le tampon (l'index)

I On choisi ainsi les �chiers à inclure dans le prochain commit

Page 14: Introduction à git

14/61

git add le retour

yann@obiwan : /git-tuto$ echo �modif� � TODOyann@obiwan : /git-tuto$ echo �modif� � README

Page 15: Introduction à git

15/61

git add le retour

yann@obiwan : /git-tuto$ git status# On branch master# Changes not staged for commit :# (use �git add <�le>...� to update what will be committed)# (use �git checkout -- <�le>...� to discard changes in workingdirectory)## modi�ed : README# modi�ed : TODO#no changes added to commit (use �git add� and/or �git commit -a�)

Page 16: Introduction à git

16/61

git add le retour

yann@obiwan : /git-tuto$ git add TODOyann@obiwan : /git-tuto$ git status# On branch master# Changes to be committed :# (use �git reset HEAD <�le>...� to unstage)## modi�ed : TODO## Changes not staged for commit :# (use �git add <�le>...� to update what will be committed)# (use �git checkout -- <�le>...� to discard changes in workingdirectory)## modi�ed : README#

Page 17: Introduction à git

17/61

git add le retour

yann@obiwan : /git-tuto$ git commit -m �modi�cation du TODO�[master 4345e06] modi�cation du TODO1 �les changed, 1 insertions(+), 0 deletions(-)

Page 18: Introduction à git

18/61

git add le retour

yann@obiwan : /git-tuto$ git status# On branch master# Changes not staged for commit :# (use �git add <�le>...� to update what will be committed)# (use �git checkout -- <�le>...� to discard changes in workingdirectory)## modi�ed : README#no changes added to commit (use �git add� and/or �git commit -a�)

Page 19: Introduction à git

19/61

git log

I A�che l'historique des commits de la branche courante

I Il donne les hash, auteurs, descriptions, dates ...

Page 20: Introduction à git

20/61

git log

yann@obiwan : /git-tuto$ git logcommit 4345e065bac5941f54e521cec9338c136c3583b8Author : Yann Sionneau <[email protected]>Date : Fri Jun 10 13 :58 :19 2011 +0200

modi�cation du TODO

commit 6d5246480668c1bb87e9bf764b7605e6e3b33060Author : Yann Sionneau <[email protected]>Date : Fri Jun 10 12 :06 :50 2011 +0200

commit initial

Page 21: Introduction à git

21/61

git rm

I Supprime un �chier du dossier courant

I Le �chier n'est plus suivi par git

I Le prochain commit contiendra l'instruction de suppression

Page 22: Introduction à git

22/61

git checkout

I Permet de modi�er l'état du répertoire de travail

I On peut demander qu'un ou plusieurs �chiers reviennent à uneversion antérieure

I On peut se placer sur le dernier commit (HEAD) d'une branche

Page 23: Introduction à git

23/61

git checkout

yann@obiwan : /git-tuto$ cat TODOapprendre gitmodifyann@obiwan : /git-tuto$ git checkout 6d52464806 TODOyann@obiwan : /git-tuto$ cat TODOapprendre git

Page 24: Introduction à git

24/61

En résumé

I git add/commit work�ow

Page 25: Introduction à git

25/61

En résumé

I git checkout work�ow

Page 26: Introduction à git

26/61

git clone

I Créé un dépot local

I Récupère la branche master d'un dépot distant

I L'enregistre en tant que branche master locale

I Récupère tout l'historique des commits de cette branche

I Checkout la tête (HEAD) de la branche master

I Créé un (dépot) remote nommé origin avec l'adresse du clone

I Con�gure la branche master locale pour suivre la branchemaster du remote origin

Page 27: Introduction à git

27/61

git branch

I Créé une branche qui dérive de la tête de la branche courante.

I Utile pour développer une nouvelle fonctionnalité

I Utile pour générer des patchs

I Utile pour récupérer les changements d'une personne...I avant de les merger (ou pas) dans sa branche personnelle

Page 28: Introduction à git

28/61

git branch

yann@obiwan : /git-tuto$ git branch testingyann@obiwan : /git-tuto$ git branch* mastertestingyann@obiwan : /git-tuto$ git branch -D testingDeleted branch testing (was 4345e06).

Page 29: Introduction à git

29/61

git branch

I git branch liste les branches (une * est devant la brancheactuelle)

I git branch -D supprime une branche

I git branch NOM créé la branche NOM a partir de la tête de labranche courante

I git checkout NOM permet de se placer sur la branche NOMI Ne fonctionne que si le répertoire de travail est propreI Il ne doit contenir aucun changement non commitéI Utiliser git stash si vous voulez mettre de côté temporairement

vos modi�cations

Page 30: Introduction à git

30/61

git checkout le retour

I git checkout permet aussi de changer de branche

Page 31: Introduction à git

31/61

git checkout le retour

yann@obiwan : /git-tuto$ git branch testingyann@obiwan : /git-tuto$ git branch* mastertestingyann@obiwan : /git-tuto$ git checkout testingSwitched to branch 'testing'yann@obiwan : /git-tuto$ git branchmaster* testing

Page 32: Introduction à git

32/61

git stash

I C'est la solution au problème suivant :

yann@obiwan : /git-tuto$ git checkout mastererror : Your local changes to the following �les would beoverwritten by checkout :testPlease, commit your changes or stash them before you can switchbranches.Aborting

Page 33: Introduction à git

33/61

git stash

I Il sauvegarde les changements du répertoire de travail

I Il sauvegarde aussi les choses qui sont dans l'index

I Tout ca est sauvegardé dans une pileI git stash [save]I git stash (apply | drop)I git stash listI git stash showI git stash clear

Page 34: Introduction à git

34/61

git stash

yann@obiwan : /git-tuto$ git stashSaved working directory and index state WIP on testing : d5e11f6testHEAD is now at d5e11f6 testyann@obiwan : /git-tuto$ git checkout masterSwitched to branch 'master'

Page 35: Introduction à git

35/61

git stash

yann@obiwan : /git-tuto$ git checkout testingSwitched to branch 'testing'yann@obiwan : /git-tuto$ git stash apply# On branch testing# Changes not staged for commit :# (use�git add <�le>...� to update what will be committed)# (use �git checkout -- <�le>...� to discard changes in workingdirectory)## modi�ed : test#no changes added to commit (use �git add� and/or �git commit -a�)

Page 36: Introduction à git

36/61

git stash

yann@obiwan : /git-tuto$ git stash liststash@{0} : WIP on testing : d5e11f6 testyann@obiwan : /git-tuto$ git stash dropDropped refs/stash@{0}(3d469�d192651230f39f88610838fbf5071a041)

Page 37: Introduction à git

37/61

git merge

I Fusionne une branche avec la branche courante

I ATTENTION : peut générer des con�its

Page 38: Introduction à git

38/61

git merge

yann@obiwan : /git-tuto$ git branch testingyann@obiwan : /git-tuto$ git checkout testingSwitched to branch 'testing'yann@obiwan : /git-tuto$ echo �experimentation 1� � READMEyann@obiwan : /git-tuto$ echo �experimentation 2� � TODOyann@obiwan : /git-tuto$ echo �experimentation� >nouveau_�chier

Page 39: Introduction à git

39/61

git merge

yann@obiwan : /git-tuto$ git add nouveau_�chieryann@obiwan : /git-tuto$ git commit -a -m �experimentation dansla branche de test�[testing aae119d] experimentation dans la branche de test3 �les changed, 3 insertions(+), 0 deletions(-)create mode 100644 nouveau_�chieryann@obiwan : /git-tuto$ git checkout masterSwitched to branch 'master'yann@obiwan : /git-tuto$ cat nouveau_�chiercat : nouveau_�chier : No such �le or directory

Page 40: Introduction à git

40/61

git merge

yann@obiwan : /git-tuto$ git merge testingUpdating 4345e06..aae119dFast-forwardREADME | 1 +TODO | 1 +nouveau_�chier | 1 +3 �les changed, 3 insertions(+), 0 deletions(-)create mode 100644 nouveau_�chieryann@obiwan : /git-tuto$ cat nouveau_�chierexperimentation

Page 41: Introduction à git

41/61

git push

I git push <dépot_distant> src[ :dst]

I Envoie des commits dans l'historique d'une branche d'un dépotdistant

I En bref : on envoie nos changements à quelqu'un d'autre

I Le dépot distant doit être �bare�

I Dans le monde de kernel.org on pull plutôt que de push

Page 42: Introduction à git

42/61

git push

yann@obiwan : $ git clone --bare /home/yann/git-tuto/copie-centraleCloning into bare repository copie-centrale...done.yann@obiwan : $ git clone copie-centrale/ copieCloning into copie...done.yann@obiwan : $ cd copie

Page 43: Introduction à git

43/61

git push

yann@obiwan : /copie$ git branch -a* masterremotes/origin/HEAD -> origin/masterremotes/origin/masterremotes/origin/testingyann@obiwan : /copie$ git remote -v showorigin /home/yann/copie-centrale/ (fetch)origin /home/yann/copie-centrale/ (push)

Page 44: Introduction à git

44/61

git push

yann@obiwan : /copie$ echo �lolilol� � TODOyann@obiwan : /copie$ git commit -a -m �ahah�[master 5984a87] ahah1 �les changed, 1 insertions(+), 0 deletions(-)yann@obiwan : /copie$ git pushCounting objects : 5, done.Delta compression using up to 16 threads.Compressing objects : 100% (2/2), done.Writing objects : 100% (3/3), 349 bytes, done.Total 3 (delta 0), reused 0 (delta 0)Unpacking objects : 100% (3/3), done.To /home/yann/copie-centrale/aae119d..5984a87 master -> master

Page 45: Introduction à git

45/61

git pull

I git pull <dépot_distant> <branche_distante>

I Equivalent á git fetch suivi de git merge

I Télécharge l'historique d'une branche distante

I Puis e�ectue le merge avec la branche courante

I Peut donc générer des con�its

Page 46: Introduction à git

46/61

git pull

yann@obiwan : /copie$ cd ../git-tuto/yann@obiwan : /git-tuto$ git remote add origin ../copie-centraleyann@obiwan : /git-tuto$ git pull origin masterremote : Counting objects : 5, done.remote : Compressing objects : 100% (2/2), done.remote : Total 3 (delta 0), reused 0 (delta 0)Unpacking objects : 100% (3/3), done.From ../copie-centrale* branch master -> FETCH_HEADUpdating aae119d..5984a87Fast-forwardTODO | 1 +1 �les changed, 1 insertions(+), 0 deletions(-)

Page 47: Introduction à git

47/61

En résumé

I git stash work�ow

Page 48: Introduction à git

48/61

git add la résuréction

I Git add peut avoir une granularité inférieure au �chier

I On peut choisir de ne rajouter qu'un bout demodi�cation/patch dans l'index

I Pour ca : git add -p <�chier> ou git add �patch <�chier>

Page 49: Introduction à git

49/61

git add la résuréction

yann@obiwan : /git-tuto$ git add -p lol.cdi� �git a/lol.c b/lol.cindex 5e5f109..c5d469b 100644--- a/lol.c+++ b/lol.c@@ -1,3 +1,4 @@+1#include <stdio.h>#include <stdlib.h>#include <string.h>Stage this hunk [y,n,q,a,d,/,j,J,g,e, ?] ? y@@ -13,3 +14,4 @@ int main(void) {

return 0 ;}+2Stage this hunk [y,n,q,a,d,/,K,g,e, ?] ? n

Page 50: Introduction à git

50/61

git rebase

I Git rebase est magique

I Permet de modi�er l'historique de la branche

I Permet de réordonner, découper, fusionner des commits

I Deux use cases pincipaux :I Maintenir nos changement locaux �au dessus`� de la branche

principaleI Fusionner des commits

Page 51: Introduction à git

51/61

git rebase

Use case 1 : Maintenir nos changement locaux �au dessus� de labranche principale

�>

�>

Page 52: Introduction à git

52/61

git rebase

I git rebase <upstream> [branch]

yann@obiwan : /git-tuto$ git rebase master newfeatureFirst, rewinding head to replay your work on top of it...Applying : nouveauteApplying : encore du nouveau

Page 53: Introduction à git

53/61

git rebase

Use case 2 : Fusionner des commits

�>

Page 54: Introduction à git

54/61

git rebase

yann@obiwan : /git-tuto$ git rebase -i HEAD 2[detached HEAD 8389617] nouveaute1 �les changed, 2 insertions(+), 0 deletions(-)create mode 100644 newfeature.cSuccessfully rebased and updated refs/heads/newfeature.

Page 55: Introduction à git

55/61

git remote

I Permet de gérer la liste des dépots distant connus de git

I Ces dépots distant ont un rapport avec notre dépot

I Il s'agit par exemple du dépot d'origine (origin) d'ou on aclôné le notre

I Ou du dépot d'un autre contributeur au projet

I Permet de donner un nom raccourci á l'url d'un dépot distant

I Pour simpli�er les git pull et git push

Page 56: Introduction à git

56/61

git bisect

I Permet d'e�ectuer une recherche dichotomique dans unebranche

I Permet de trouver LE commit qui introduit un bug/uneregression

I Permet d'aller vite et d'automatiser le processus

I Explication par l'exemple ...

Page 57: Introduction à git

57/61

git bisect

yann@obiwan :/git-tuto$ git bisect startyann@obiwan :/git-tuto$ cat �chier.c | grep LOLLOLyann@obiwan :/git-tuto$ git bisect badyann@obiwan :/git-tuto$ git bisect good debutBisecting : 5 revisions left to test after this (roughly 3 steps)[26d702f2f05f3321c9d707bd809ae406b82d0190] commit 7yann@obiwan :/git-tuto$ cat �chier.c | grep LOLLOLyann@obiwan :/git-tuto$ git bisect badBisecting : 2 revisions left to test after this (roughly 2 steps)[5338dca0d46062ea01225092210c729d2a7cb202] commit 4

Page 58: Introduction à git

58/61

git bisect

yann@obiwan :/git-tuto$ cat �chier.c | grep LOLyann@obiwan :/git-tuto$ git bisect goodBisecting : 0 revisions left to test after this (roughly 1 step)[8bd8458ac7509a0f424067fc4582fb1ba74d5328] commit 6yann@obiwan :/git-tuto$ cat �chier.c | grep LOLLOLyann@obiwan :/git-tuto$ git bisect badBisecting : 0 revisions left to test after this (roughly 0 steps)[1f3133cc11782d3d3546f2c2e16d60da359�717] commit 5yann@obiwan :/git-tuto$ cat �chier.c | grep LOLLOL

Page 59: Introduction à git

59/61

git bisect

yann@obiwan :/git-tuto$ git bisect bad1f3133cc11782d3d3546f2c2e16d60da359�717 is the �rst badcommitcommit 1f3133cc11782d3d3546f2c2e16d60da359�717Author : Yann Sionneau <[email protected]>Date : Mon Jun 13 17 :18 :38 2011 +0200

commit 5

:100644 100644 83fb967dd33eda43d453080986f695f2848f7a9955dd01cd5cacb1eba809c00974c8fef8adb4c882 M �chier.c

Page 60: Introduction à git

60/61

git bisect

yann@obiwan :/git-tuto$ git show 1f3133commit 1f3133cc11782d3d3546f2c2e16d60da359�717Author : Yann Sionneau <[email protected]>Date : Mon Jun 13 17 :18 :38 2011 +0200

commit 5

di� --git a/�chier.c b/�chier.cindex 83fb967..55dd01c 100644--- a/�chier.c+++ b/�chier.c@@ -2,3 +2,4 @@ ligneligneligneligne+LOL

Page 61: Introduction à git

61/61

git bisect

yann@obiwan :/git-tuto$ git bisect resetPrevious HEAD position was 1f3133c... commit 5Switched to branch 'master'