SlideShare une entreprise Scribd logo
1  sur  61
Télécharger pour lire hors ligne
Introduction à git

   Yann Sionneau


       int lab;




    16 juin 2011




                     1/61
Commandes basiques



Usages un peu plus avancés



Usages experts




                             2/61
Plan

Commandes basiques     Usages un peu plus avancés
   git init               git clone
   git add                git branch
   git commit             git checkout le retour
   git add le retour      git stash
   git log                git merge
   git rm                 git push
   git checkout           git pull
   En résumé              En résumé
                       Usages experts
                          git add la résuréction
                          git rebase
                          git remote
                          git bisect

                                                    3/61
git init




           On créé le dépot git




                                  4/61
git init




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




                                                                    5/61
git add




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

          Le contenu de cette zone sera inclu dans le prochain commit

          Git traquera dorénavent les modications sur ces chiers




                                                                        6/61
git add




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




                                                                 7/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)



                                                                       8/61
git add




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




                                                  9/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
   #




                                                   10/61
git commit




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

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

      Chaque commit a un identiant (hash SHA1)




                                                             11/61
git commit




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




                                                             12/61
git add le retour




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

       On choisi ainsi les chiers à inclure dans le prochain commit




                                                                       13/61
git add le retour




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




                                                     14/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 working
   directory)
   #
   # modied : README
   # modied : TODO
   #
   no changes added to commit (use git add and/or git commit -a)




                                                                       15/61
git add le retour
   yann@obiwan : /git-tuto$ git add TODO
   yann@obiwan : /git-tuto$ git status
   # On branch master
   # Changes to be committed :
   # (use git reset HEAD le... to unstage)
   #
   # modied : TODO
   #
   # Changes not staged for commit :
   # (use git add le... to update what will be committed)
   # (use git checkout -- le... to discard changes in working
   directory)
   #
   # modied : README
   #

                                                                     16/61
git add le retour




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




                                                                  17/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 working
   directory)
   #
   # modied : README
   #
   no changes added to commit (use git add and/or git commit -a)




                                                                       18/61
git log




          Ache l'historique des commits de la branche courante

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




                                                                  19/61
git log

   yann@obiwan : /git-tuto$ git log
   commit 4345e065bac5941f54e521cec9338c136c3583b8
   Author : Yann Sionneau yann@minet.net
   Date : Fri Jun 10 13 :58 :19 2011 +0200


   modication du TODO


   commit 6d5246480668c1bb87e9bf764b7605e6e3b33060
   Author : Yann Sionneau yann@minet.net
   Date : Fri Jun 10 12 :06 :50 2011 +0200


   commit initial




                                                     20/61
git rm




         Supprime un chier du dossier courant

         Le chier n'est plus suivi par git

         Le prochain commit contiendra l'instruction de suppression




                                                                      21/61
git checkout




       Permet de modier l'état du répertoire de travail

       On peut demander qu'un ou plusieurs chiers reviennent à une
       version antérieure

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




                                                                      22/61
git checkout



   yann@obiwan : /git-tuto$ cat TODO
   apprendre git
   modif
   yann@obiwan : /git-tuto$ git checkout 6d52464806 TODO
   yann@obiwan : /git-tuto$ cat TODO
   apprendre git




                                                           23/61
En résumé


      git add/commit workow




                               24/61
En résumé


      git checkout workow




                             25/61
git clone


       Créé un dépot local

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

       L'enregistre en tant que branche master locale

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

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

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

       Congure la branche master locale pour suivre la branche
       master du remote origin




                                                                     26/61
git branch




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

       Utile pour développer une nouvelle fonctionnalité

       Utile pour générer des patchs

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




                                                                        27/61
git branch



   yann@obiwan : /git-tuto$ git branch testing
   yann@obiwan : /git-tuto$ git branch
   * master
   testing
   yann@obiwan : /git-tuto$ git branch -D testing
   Deleted branch testing (was 4345e06).




                                                    28/61
git branch


       git branch liste les branches (une * est devant la branche
       actuelle)

       git branch -D supprime une branche

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

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




                                                                               29/61
git checkout le retour




       git checkout permet aussi de changer de branche




                                                         30/61
git checkout le retour


   yann@obiwan : /git-tuto$ git branch testing
   yann@obiwan : /git-tuto$ git branch
   * master
   testing
   yann@obiwan : /git-tuto$ git checkout testing
   Switched to branch 'testing'
   yann@obiwan : /git-tuto$ git branch
   master
   * testing




                                                   31/61
git stash



          C'est la solution au problème suivant :

   yann@obiwan : /git-tuto$ git checkout master
   error : Your local changes to the following les would be
   overwritten by checkout :
   test
   Please, commit your changes or stash them before you can switch
   branches.
   Aborting




                                                                     32/61
git stash



       Il sauvegarde les changements du répertoire de travail

       Il sauvegarde aussi les choses qui sont dans l'index

       Tout ca est sauvegardé dans une pile
            git stash [save]
            git stash (apply | drop)
            git stash list
            git stash show
            git stash clear




                                                                33/61
git stash



   yann@obiwan : /git-tuto$ git stash
   Saved working directory and index state WIP on testing : d5e11f6
   test
   HEAD is now at d5e11f6 test
   yann@obiwan : /git-tuto$ git checkout master
   Switched to branch 'master'




                                                                      34/61
git stash

   yann@obiwan : /git-tuto$ git checkout testing
   Switched to branch 'testing'
   yann@obiwan : /git-tuto$ git stash apply
   # On branch testing
   # Changes not staged for commit :
   # (usegit add le... to update what will be committed)
   # (use git checkout -- le... to discard changes in working
   directory)
   #
   # modied : test
   #
   no changes added to commit (use git add and/or git commit -a)




                                                                       35/61
git stash




   yann@obiwan : /git-tuto$ git stash list
   stash@{0} : WIP on testing : d5e11f6 test
   yann@obiwan : /git-tuto$ git stash drop
   Dropped refs/stash@{0}
   (3d469d192651230f39f88610838fbf5071a041)




                                               36/61
git merge




      Fusionne une branche avec la branche courante

      ATTENTION : peut générer des conits




                                                      37/61
git merge



   yann@obiwan : /git-tuto$ git branch testing
   yann@obiwan : /git-tuto$ git checkout testing
   Switched to branch 'testing'
   yann@obiwan : /git-tuto$ echo experimentation 1  README
   yann@obiwan : /git-tuto$ echo experimentation 2  TODO
   yann@obiwan : /git-tuto$ echo experimentation 
   nouveau_chier




                                                                38/61
git merge


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




                                                                     39/61
git merge


   yann@obiwan : /git-tuto$ git merge testing
   Updating 4345e06..aae119d
   Fast-forward
   README | 1 +
   TODO | 1 +
   nouveau_chier | 1 +
   3 les changed, 3 insertions(+), 0 deletions(-)
   create mode 100644 nouveau_chier
   yann@obiwan : /git-tuto$ cat nouveau_chier
   experimentation




                                                     40/61
git push



       git push dépot_distant src[ :dst]

       Envoie des commits dans l'historique d'une branche d'un dépot
       distant

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

       Le dépot distant doit être bare

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




                                                                       41/61
git push



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




                                                           42/61
git push



   yann@obiwan : /copie$ git branch -a
   * master
   remotes/origin/HEAD - origin/master
   remotes/origin/master
   remotes/origin/testing
   yann@obiwan : /copie$ git remote -v show
   origin /home/yann/copie-centrale/ (fetch)
   origin /home/yann/copie-centrale/ (push)




                                               43/61
git push

   yann@obiwan : /copie$ echo lolilol  TODO
   yann@obiwan : /copie$ git commit -a -m ahah
   [master 5984a87] ahah
   1 les changed, 1 insertions(+), 0 deletions(-)
   yann@obiwan : /copie$ git push
   Counting 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



                                                     44/61
git pull




           git pull dépot_distant branche_distante

           Equivalent á git fetch suivi de git merge

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

           Puis eectue le merge avec la branche courante

           Peut donc générer des conits




                                                            45/61
git pull

   yann@obiwan : /copie$ cd ../git-tuto/
   yann@obiwan : /git-tuto$ git remote add origin ../copie-centrale
   yann@obiwan : /git-tuto$ git pull origin master
   remote : 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_HEAD
   Updating aae119d..5984a87
   Fast-forward
   TODO | 1 +
   1 les changed, 1 insertions(+), 0 deletions(-)



                                                                      46/61
En résumé

      git stash workow




                          47/61
git add la résuréction




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

       On peut choisir de ne rajouter qu'un bout de
       modication/patch dans l'index

       Pour ca : git add -p chier ou git add patch chier




                                                                  48/61
git add la résuréction
   yann@obiwan : /git-tuto$ git add -p lol.c
   di git a/lol.c b/lol.c
   index 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 ;
   }
   +2
   Stage this hunk [y,n,q,a,d,/,K,g,e, ?] ? n     49/61
git rebase



       Git rebase est magique

       Permet de modier l'historique de la branche

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

       Deux use cases pincipaux :
             Maintenir nos changement locaux au dessus` de la branche
             principale
             Fusionner des commits




                                                                          50/61
git rebase

   Use case 1 : Maintenir nos changement locaux au dessus de la
   branche principale




                              




                              




                                                                    51/61
git rebase




       git rebase upstream [branch]

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




                                                               52/61
git rebase



   Use case 2 : Fusionner des commits




                             




                                        53/61
git rebase




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




                                                             54/61
git remote



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

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

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

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

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

       Pour simplier les git pull et git push




                                                                      55/61
git bisect



       Permet d'eectuer une recherche dichotomique dans une
       branche

       Permet de trouver LE commit qui introduit un bug/une
       regression

       Permet d'aller vite et d'automatiser le processus

       Explication par l'exemple ...




                                                               56/61
git bisect

   yann@obiwan :/git-tuto$ git bisect start
   yann@obiwan :/git-tuto$ cat chier.c | grep LOL
   LOL
   yann@obiwan :/git-tuto$ git bisect bad
   yann@obiwan :/git-tuto$ git bisect good debut
   Bisecting : 5 revisions left to test after this (roughly 3 steps)
   [26d702f2f05f3321c9d707bd809ae406b82d0190] commit 7
   yann@obiwan :/git-tuto$ cat chier.c | grep LOL
   LOL
   yann@obiwan :/git-tuto$ git bisect bad
   Bisecting : 2 revisions left to test after this (roughly 2 steps)
   [5338dca0d46062ea01225092210c729d2a7cb202] commit 4




                                                                       57/61
git bisect


   yann@obiwan :/git-tuto$ cat chier.c | grep LOL
   yann@obiwan :/git-tuto$ git bisect good
   Bisecting : 0 revisions left to test after this (roughly 1 step)
   [8bd8458ac7509a0f424067fc4582fb1ba74d5328] commit 6
   yann@obiwan :/git-tuto$ cat chier.c | grep LOL
   LOL
   yann@obiwan :/git-tuto$ git bisect bad
   Bisecting : 0 revisions left to test after this (roughly 0 steps)
   [1f3133cc11782d3d3546f2c2e16d60da359717] commit 5
   yann@obiwan :/git-tuto$ cat chier.c | grep LOL
   LOL




                                                                       58/61
git bisect


   yann@obiwan :/git-tuto$ git bisect bad
   1f3133cc11782d3d3546f2c2e16d60da359717 is the rst bad
   commit
   commit 1f3133cc11782d3d3546f2c2e16d60da359717
   Author : Yann Sionneau yann@minet.net
   Date : Mon Jun 13 17 :18 :38 2011 +0200


   commit 5


   :100644 100644 83fb967dd33eda43d453080986f695f2848f7a99
   55dd01cd5cacb1eba809c00974c8fef8adb4c882 M chier.c




                                                             59/61
git bisect
   yann@obiwan :/git-tuto$ git show 1f3133
   commit 1f3133cc11782d3d3546f2c2e16d60da359717
   Author : Yann Sionneau yann@minet.net
   Date : Mon Jun 13 17 :18 :38 2011 +0200


   commit 5


   di --git a/chier.c b/chier.c
   index 83fb967..55dd01c 100644
   --- a/chier.c
   +++ b/chier.c
   @@ -2,3 +2,4 @@ ligne
   ligne
   ligne
   ligne
   +LOL
                                                    60/61
git bisect




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




                                                    61/61

Contenu connexe

En vedette

Research fundamentals presentation
Research fundamentals presentationResearch fundamentals presentation
Research fundamentals presentationmsswindle
 
Catalysis Today(1997) Sugioka Et.Al
Catalysis Today(1997) Sugioka Et.AlCatalysis Today(1997) Sugioka Et.Al
Catalysis Today(1997) Sugioka Et.AlLebong Andalaluna
 
Aromaterapia y sus beneficios
Aromaterapia y sus beneficiosAromaterapia y sus beneficios
Aromaterapia y sus beneficiosENTUSIASMA
 
Milkymist System-on-Chip at Open Source Hardware User Group 8
Milkymist System-on-Chip at Open Source Hardware User Group 8Milkymist System-on-Chip at Open Source Hardware User Group 8
Milkymist System-on-Chip at Open Source Hardware User Group 8Yann Sionneau
 
Introduction to Social Media
Introduction to Social MediaIntroduction to Social Media
Introduction to Social Mediaggsmvt
 
LatticeMico32 MMU documentation
LatticeMico32 MMU documentationLatticeMico32 MMU documentation
LatticeMico32 MMU documentationYann Sionneau
 
Social Media Marketing for Voice-Over Artists
Social Media Marketing for Voice-Over ArtistsSocial Media Marketing for Voice-Over Artists
Social Media Marketing for Voice-Over Artistsggsmvt
 
Professional development
Professional developmentProfessional development
Professional developmentbperisic
 

En vedette (8)

Research fundamentals presentation
Research fundamentals presentationResearch fundamentals presentation
Research fundamentals presentation
 
Catalysis Today(1997) Sugioka Et.Al
Catalysis Today(1997) Sugioka Et.AlCatalysis Today(1997) Sugioka Et.Al
Catalysis Today(1997) Sugioka Et.Al
 
Aromaterapia y sus beneficios
Aromaterapia y sus beneficiosAromaterapia y sus beneficios
Aromaterapia y sus beneficios
 
Milkymist System-on-Chip at Open Source Hardware User Group 8
Milkymist System-on-Chip at Open Source Hardware User Group 8Milkymist System-on-Chip at Open Source Hardware User Group 8
Milkymist System-on-Chip at Open Source Hardware User Group 8
 
Introduction to Social Media
Introduction to Social MediaIntroduction to Social Media
Introduction to Social Media
 
LatticeMico32 MMU documentation
LatticeMico32 MMU documentationLatticeMico32 MMU documentation
LatticeMico32 MMU documentation
 
Social Media Marketing for Voice-Over Artists
Social Media Marketing for Voice-Over ArtistsSocial Media Marketing for Voice-Over Artists
Social Media Marketing for Voice-Over Artists
 
Professional development
Professional developmentProfessional development
Professional development
 

Similaire à Introduction à git

Découvrir et utiliser Git : le logiciel de gestion de versions décentralisé
Découvrir et utiliser Git : le logiciel de gestion de versions décentraliséDécouvrir et utiliser Git : le logiciel de gestion de versions décentralisé
Découvrir et utiliser Git : le logiciel de gestion de versions décentraliséECAM Brussels Engineering School
 
Introduction à Git
Introduction à GitIntroduction à Git
Introduction à GitXavier Perez
 
Débuter avec Git & github
Débuter avec Git & githubDébuter avec Git & github
Débuter avec Git & githubMonoem Youneb
 
Initiation à Git, GitHub2.pdf
Initiation à Git, GitHub2.pdfInitiation à Git, GitHub2.pdf
Initiation à Git, GitHub2.pdfmouad55
 
Présentation de git
Présentation de gitPrésentation de git
Présentation de gitJulien Blin
 
Petit Déjeuner Git chez Makina Corpus
Petit Déjeuner Git chez Makina CorpusPetit Déjeuner Git chez Makina Corpus
Petit Déjeuner Git chez Makina Corpusleplatrem
 
Introduction à git.pdf
Introduction à git.pdfIntroduction à git.pdf
Introduction à git.pdfbadrfathallah2
 
les commandes Git que vous devez absolument connaitre!.pdf
les commandes Git que vous devez absolument connaitre!.pdfles commandes Git que vous devez absolument connaitre!.pdf
les commandes Git que vous devez absolument connaitre!.pdfSimpleLearn1
 
Git pour les (pas si) nuls
Git pour les (pas si) nulsGit pour les (pas si) nuls
Git pour les (pas si) nulsMalk Zameth
 
Pourquoi versionner ses githooks.pdf
Pourquoi versionner ses githooks.pdfPourquoi versionner ses githooks.pdf
Pourquoi versionner ses githooks.pdfChris Saez
 
TP Git avancé DevoxxFR 2018 (exercices)
TP Git avancé DevoxxFR 2018 (exercices)TP Git avancé DevoxxFR 2018 (exercices)
TP Git avancé DevoxxFR 2018 (exercices)Jérôme Tamborini
 

Similaire à Introduction à git (20)

3_SCM_Git.pdf
3_SCM_Git.pdf3_SCM_Git.pdf
3_SCM_Git.pdf
 
Découvrir et utiliser Git : le logiciel de gestion de versions décentralisé
Découvrir et utiliser Git : le logiciel de gestion de versions décentraliséDécouvrir et utiliser Git : le logiciel de gestion de versions décentralisé
Découvrir et utiliser Git : le logiciel de gestion de versions décentralisé
 
Introduction à Git
Introduction à GitIntroduction à Git
Introduction à Git
 
Débuter avec Git & github
Débuter avec Git & githubDébuter avec Git & github
Débuter avec Git & github
 
Git and Github.pptx
Git and Github.pptxGit and Github.pptx
Git and Github.pptx
 
Initiation à Git, GitHub2.pdf
Initiation à Git, GitHub2.pdfInitiation à Git, GitHub2.pdf
Initiation à Git, GitHub2.pdf
 
SVN to GitHUb
SVN to GitHUbSVN to GitHUb
SVN to GitHUb
 
Git pratique
Git pratiqueGit pratique
Git pratique
 
Git pratique
Git pratiqueGit pratique
Git pratique
 
Présentation de git
Présentation de gitPrésentation de git
Présentation de git
 
Petit Déjeuner Git chez Makina Corpus
Petit Déjeuner Git chez Makina CorpusPetit Déjeuner Git chez Makina Corpus
Petit Déjeuner Git chez Makina Corpus
 
Introduction à git.pdf
Introduction à git.pdfIntroduction à git.pdf
Introduction à git.pdf
 
les commandes Git que vous devez absolument connaitre!.pdf
les commandes Git que vous devez absolument connaitre!.pdfles commandes Git que vous devez absolument connaitre!.pdf
les commandes Git que vous devez absolument connaitre!.pdf
 
Git pour les (pas si) nuls
Git pour les (pas si) nulsGit pour les (pas si) nuls
Git pour les (pas si) nuls
 
git-cmds-base.pdf
git-cmds-base.pdfgit-cmds-base.pdf
git-cmds-base.pdf
 
Git
GitGit
Git
 
Pourquoi versionner ses githooks.pdf
Pourquoi versionner ses githooks.pdfPourquoi versionner ses githooks.pdf
Pourquoi versionner ses githooks.pdf
 
TP Git avancé DevoxxFR 2018 (exercices)
TP Git avancé DevoxxFR 2018 (exercices)TP Git avancé DevoxxFR 2018 (exercices)
TP Git avancé DevoxxFR 2018 (exercices)
 
Git merge-rebase
Git merge-rebaseGit merge-rebase
Git merge-rebase
 
Outils de gestion de projets
Outils de gestion de projetsOutils de gestion de projets
Outils de gestion de projets
 

Introduction à git

  • 1. Introduction à git Yann Sionneau int lab; 16 juin 2011 1/61
  • 2. Commandes basiques Usages un peu plus avancés Usages experts 2/61
  • 3. Plan Commandes basiques Usages un peu plus avancés git init git clone git add git branch git commit git checkout le retour git add le retour git stash git log git merge git rm git push git checkout git pull En résumé En résumé Usages experts git add la résuréction git rebase git remote git bisect 3/61
  • 4. git init On créé le dépot git 4/61
  • 5. git init yann@obiwan : /git-tuto$ git init Initialized empty Git repository in /home/yann/git-tuto/.git/ 5/61
  • 6. git add On rajoute des chiers dans une zone tampon (ou index) Le contenu de cette zone sera inclu dans le prochain commit Git traquera dorénavent les modications sur ces chiers 6/61
  • 7. git add yann@obiwan : /git-tuto$ echo ceci est le README README yann@obiwan : /git-tuto$ echo apprendre git TODO 7/61
  • 8. 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) 8/61
  • 9. git add yann@obiwan : /git-tuto$ git add TODO README 9/61
  • 10. 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 # 10/61
  • 11. git commit Sauvegarde le contenu de la zone tampon (ou index ou stagging area) Elle est insérée dans l'historique de la branche Chaque commit a un identiant (hash SHA1) 11/61
  • 12. git commit yann@obiwan : /git-tuto$ git commit -m commit initial [master (root-commit) 6d52464] commit initial 2 les changed, 2 insertions(+), 0 deletions(-) create mode 100644 README create mode 100644 TODO 12/61
  • 13. git add le retour git add met les chiers dans le tampon (l'index) On choisi ainsi les chiers à inclure dans le prochain commit 13/61
  • 14. git add le retour yann@obiwan : /git-tuto$ echo modif TODO yann@obiwan : /git-tuto$ echo modif README 14/61
  • 15. 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 working directory) # # modied : README # modied : TODO # no changes added to commit (use git add and/or git commit -a) 15/61
  • 16. git add le retour yann@obiwan : /git-tuto$ git add TODO yann@obiwan : /git-tuto$ git status # On branch master # Changes to be committed : # (use git reset HEAD le... to unstage) # # modied : TODO # # Changes not staged for commit : # (use git add le... to update what will be committed) # (use git checkout -- le... to discard changes in working directory) # # modied : README # 16/61
  • 17. git add le retour yann@obiwan : /git-tuto$ git commit -m modication du TODO [master 4345e06] modication du TODO 1 les changed, 1 insertions(+), 0 deletions(-) 17/61
  • 18. 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 working directory) # # modied : README # no changes added to commit (use git add and/or git commit -a) 18/61
  • 19. git log Ache l'historique des commits de la branche courante Il donne les hash, auteurs, descriptions, dates ... 19/61
  • 20. git log yann@obiwan : /git-tuto$ git log commit 4345e065bac5941f54e521cec9338c136c3583b8 Author : Yann Sionneau yann@minet.net Date : Fri Jun 10 13 :58 :19 2011 +0200 modication du TODO commit 6d5246480668c1bb87e9bf764b7605e6e3b33060 Author : Yann Sionneau yann@minet.net Date : Fri Jun 10 12 :06 :50 2011 +0200 commit initial 20/61
  • 21. git rm Supprime un chier du dossier courant Le chier n'est plus suivi par git Le prochain commit contiendra l'instruction de suppression 21/61
  • 22. git checkout Permet de modier l'état du répertoire de travail On peut demander qu'un ou plusieurs chiers reviennent à une version antérieure On peut se placer sur le dernier commit (HEAD) d'une branche 22/61
  • 23. git checkout yann@obiwan : /git-tuto$ cat TODO apprendre git modif yann@obiwan : /git-tuto$ git checkout 6d52464806 TODO yann@obiwan : /git-tuto$ cat TODO apprendre git 23/61
  • 24. En résumé git add/commit workow 24/61
  • 25. En résumé git checkout workow 25/61
  • 26. git clone Créé un dépot local Récupère la branche master d'un dépot distant L'enregistre en tant que branche master locale Récupère tout l'historique des commits de cette branche Checkout la tête (HEAD) de la branche master Créé un (dépot) remote nommé origin avec l'adresse du clone Congure la branche master locale pour suivre la branche master du remote origin 26/61
  • 27. git branch Créé une branche qui dérive de la tête de la branche courante. Utile pour développer une nouvelle fonctionnalité Utile pour générer des patchs Utile pour récupérer les changements d'une personne... avant de les merger (ou pas) dans sa branche personnelle 27/61
  • 28. git branch yann@obiwan : /git-tuto$ git branch testing yann@obiwan : /git-tuto$ git branch * master testing yann@obiwan : /git-tuto$ git branch -D testing Deleted branch testing (was 4345e06). 28/61
  • 29. git branch git branch liste les branches (une * est devant la branche actuelle) git branch -D supprime une branche git branch NOM créé la branche NOM a partir de la tête de la branche courante git checkout NOM permet de se placer sur la branche NOM Ne fonctionne que si le répertoire de travail est propre Il ne doit contenir aucun changement non commité Utiliser git stash si vous voulez mettre de côté temporairement vos modications 29/61
  • 30. git checkout le retour git checkout permet aussi de changer de branche 30/61
  • 31. git checkout le retour yann@obiwan : /git-tuto$ git branch testing yann@obiwan : /git-tuto$ git branch * master testing yann@obiwan : /git-tuto$ git checkout testing Switched to branch 'testing' yann@obiwan : /git-tuto$ git branch master * testing 31/61
  • 32. git stash C'est la solution au problème suivant : yann@obiwan : /git-tuto$ git checkout master error : Your local changes to the following les would be overwritten by checkout : test Please, commit your changes or stash them before you can switch branches. Aborting 32/61
  • 33. git stash Il sauvegarde les changements du répertoire de travail Il sauvegarde aussi les choses qui sont dans l'index Tout ca est sauvegardé dans une pile git stash [save] git stash (apply | drop) git stash list git stash show git stash clear 33/61
  • 34. git stash yann@obiwan : /git-tuto$ git stash Saved working directory and index state WIP on testing : d5e11f6 test HEAD is now at d5e11f6 test yann@obiwan : /git-tuto$ git checkout master Switched to branch 'master' 34/61
  • 35. git stash yann@obiwan : /git-tuto$ git checkout testing Switched to branch 'testing' yann@obiwan : /git-tuto$ git stash apply # On branch testing # Changes not staged for commit : # (usegit add le... to update what will be committed) # (use git checkout -- le... to discard changes in working directory) # # modied : test # no changes added to commit (use git add and/or git commit -a) 35/61
  • 36. git stash yann@obiwan : /git-tuto$ git stash list stash@{0} : WIP on testing : d5e11f6 test yann@obiwan : /git-tuto$ git stash drop Dropped refs/stash@{0} (3d469d192651230f39f88610838fbf5071a041) 36/61
  • 37. git merge Fusionne une branche avec la branche courante ATTENTION : peut générer des conits 37/61
  • 38. git merge yann@obiwan : /git-tuto$ git branch testing yann@obiwan : /git-tuto$ git checkout testing Switched to branch 'testing' yann@obiwan : /git-tuto$ echo experimentation 1 README yann@obiwan : /git-tuto$ echo experimentation 2 TODO yann@obiwan : /git-tuto$ echo experimentation nouveau_chier 38/61
  • 39. git merge yann@obiwan : /git-tuto$ git add nouveau_chier yann@obiwan : /git-tuto$ git commit -a -m experimentation dans la branche de test [testing aae119d] experimentation dans la branche de test 3 les changed, 3 insertions(+), 0 deletions(-) create mode 100644 nouveau_chier yann@obiwan : /git-tuto$ git checkout master Switched to branch 'master' yann@obiwan : /git-tuto$ cat nouveau_chier cat : nouveau_chier : No such le or directory 39/61
  • 40. git merge yann@obiwan : /git-tuto$ git merge testing Updating 4345e06..aae119d Fast-forward README | 1 + TODO | 1 + nouveau_chier | 1 + 3 les changed, 3 insertions(+), 0 deletions(-) create mode 100644 nouveau_chier yann@obiwan : /git-tuto$ cat nouveau_chier experimentation 40/61
  • 41. git push git push dépot_distant src[ :dst] Envoie des commits dans l'historique d'une branche d'un dépot distant En bref : on envoie nos changements à quelqu'un d'autre Le dépot distant doit être bare Dans le monde de kernel.org on pull plutôt que de push 41/61
  • 42. git push yann@obiwan : $ git clone --bare /home/yann/git-tuto/ copie-centrale Cloning into bare repository copie-centrale... done. yann@obiwan : $ git clone copie-centrale/ copie Cloning into copie... done. yann@obiwan : $ cd copie 42/61
  • 43. git push yann@obiwan : /copie$ git branch -a * master remotes/origin/HEAD - origin/master remotes/origin/master remotes/origin/testing yann@obiwan : /copie$ git remote -v show origin /home/yann/copie-centrale/ (fetch) origin /home/yann/copie-centrale/ (push) 43/61
  • 44. git push yann@obiwan : /copie$ echo lolilol TODO yann@obiwan : /copie$ git commit -a -m ahah [master 5984a87] ahah 1 les changed, 1 insertions(+), 0 deletions(-) yann@obiwan : /copie$ git push Counting 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 44/61
  • 45. git pull git pull dépot_distant branche_distante Equivalent á git fetch suivi de git merge Télécharge l'historique d'une branche distante Puis eectue le merge avec la branche courante Peut donc générer des conits 45/61
  • 46. git pull yann@obiwan : /copie$ cd ../git-tuto/ yann@obiwan : /git-tuto$ git remote add origin ../copie-centrale yann@obiwan : /git-tuto$ git pull origin master remote : 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_HEAD Updating aae119d..5984a87 Fast-forward TODO | 1 + 1 les changed, 1 insertions(+), 0 deletions(-) 46/61
  • 47. En résumé git stash workow 47/61
  • 48. git add la résuréction Git add peut avoir une granularité inférieure au chier On peut choisir de ne rajouter qu'un bout de modication/patch dans l'index Pour ca : git add -p chier ou git add patch chier 48/61
  • 49. git add la résuréction yann@obiwan : /git-tuto$ git add -p lol.c di git a/lol.c b/lol.c index 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 ; } +2 Stage this hunk [y,n,q,a,d,/,K,g,e, ?] ? n 49/61
  • 50. git rebase Git rebase est magique Permet de modier l'historique de la branche Permet de réordonner, découper, fusionner des commits Deux use cases pincipaux : Maintenir nos changement locaux au dessus` de la branche principale Fusionner des commits 50/61
  • 51. git rebase Use case 1 : Maintenir nos changement locaux au dessus de la branche principale 51/61
  • 52. git rebase git rebase upstream [branch] yann@obiwan : /git-tuto$ git rebase master newfeature First, rewinding head to replay your work on top of it... Applying : nouveaute Applying : encore du nouveau 52/61
  • 53. git rebase Use case 2 : Fusionner des commits 53/61
  • 54. git rebase yann@obiwan : /git-tuto$ git rebase -i HEAD 2 [detached HEAD 8389617] nouveaute 1 les changed, 2 insertions(+), 0 deletions(-) create mode 100644 newfeature.c Successfully rebased and updated refs/heads/newfeature. 54/61
  • 55. git remote Permet de gérer la liste des dépots distant connus de git Ces dépots distant ont un rapport avec notre dépot Il s'agit par exemple du dépot d'origine (origin) d'ou on a clôné le notre Ou du dépot d'un autre contributeur au projet Permet de donner un nom raccourci á l'url d'un dépot distant Pour simplier les git pull et git push 55/61
  • 56. git bisect Permet d'eectuer une recherche dichotomique dans une branche Permet de trouver LE commit qui introduit un bug/une regression Permet d'aller vite et d'automatiser le processus Explication par l'exemple ... 56/61
  • 57. git bisect yann@obiwan :/git-tuto$ git bisect start yann@obiwan :/git-tuto$ cat chier.c | grep LOL LOL yann@obiwan :/git-tuto$ git bisect bad yann@obiwan :/git-tuto$ git bisect good debut Bisecting : 5 revisions left to test after this (roughly 3 steps) [26d702f2f05f3321c9d707bd809ae406b82d0190] commit 7 yann@obiwan :/git-tuto$ cat chier.c | grep LOL LOL yann@obiwan :/git-tuto$ git bisect bad Bisecting : 2 revisions left to test after this (roughly 2 steps) [5338dca0d46062ea01225092210c729d2a7cb202] commit 4 57/61
  • 58. git bisect yann@obiwan :/git-tuto$ cat chier.c | grep LOL yann@obiwan :/git-tuto$ git bisect good Bisecting : 0 revisions left to test after this (roughly 1 step) [8bd8458ac7509a0f424067fc4582fb1ba74d5328] commit 6 yann@obiwan :/git-tuto$ cat chier.c | grep LOL LOL yann@obiwan :/git-tuto$ git bisect bad Bisecting : 0 revisions left to test after this (roughly 0 steps) [1f3133cc11782d3d3546f2c2e16d60da359717] commit 5 yann@obiwan :/git-tuto$ cat chier.c | grep LOL LOL 58/61
  • 59. git bisect yann@obiwan :/git-tuto$ git bisect bad 1f3133cc11782d3d3546f2c2e16d60da359717 is the rst bad commit commit 1f3133cc11782d3d3546f2c2e16d60da359717 Author : Yann Sionneau yann@minet.net Date : Mon Jun 13 17 :18 :38 2011 +0200 commit 5 :100644 100644 83fb967dd33eda43d453080986f695f2848f7a99 55dd01cd5cacb1eba809c00974c8fef8adb4c882 M chier.c 59/61
  • 60. git bisect yann@obiwan :/git-tuto$ git show 1f3133 commit 1f3133cc11782d3d3546f2c2e16d60da359717 Author : Yann Sionneau yann@minet.net Date : Mon Jun 13 17 :18 :38 2011 +0200 commit 5 di --git a/chier.c b/chier.c index 83fb967..55dd01c 100644 --- a/chier.c +++ b/chier.c @@ -2,3 +2,4 @@ ligne ligne ligne ligne +LOL 60/61
  • 61. git bisect yann@obiwan :/git-tuto$ git bisect reset Previous HEAD position was 1f3133c... commit 5 Switched to branch 'master' 61/61