Git - Opération de mise à jour

Modifier la fonction existante

Tom effectue l'opération de clonage et trouve une nouvelle chaîne de fichier.c. Il veut savoir qui a ajouté ce fichier au référentiel et dans quel but, alors, il exécute legit log commander.

[[email protected] ~]$ git clone [email protected]:project.git

La commande ci-dessus produira le résultat suivant -

Initialized empty Git repository in /home/tom/project/.git/
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
Receiving objects: 100% (6/6), 726 bytes, done.
remote: Total 6 (delta 0), reused 0 (delta 0)

L'opération de clonage créera un nouveau répertoire dans le répertoire de travail actuel. Il change le répertoire en répertoire nouvellement créé et exécute legit log commander.

[[email protected] ~]$ cd project/

[[email protected] project]$ git log

La commande ci-dessus produira le résultat suivant -

commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 07:32:56 2013 +0530

Initial commit

Après avoir observé le journal, il se rend compte que le fichier string.c a été ajouté par Jerry pour implémenter les opérations de base sur les chaînes. Il est curieux du code de Jerry. Il ouvre donc string.c dans l'éditeur de texte et trouve immédiatement un bogue. Dans la fonction my_strlen, Jerry n'utilise pas de pointeur constant. Alors, il décide de modifier le code de Jerry. Après modification, le code se présente comme suit -

[[email protected] project]$ git diff

La commande ci-dessus produira le résultat suivant -

diff --git a/string.c b/string.c
index 7da2992..32489eb 100644
--- a/string.c
+++ b/string.c
@@ -1,8 +1,8 @@
#include <stdio.h>
-size_t my_strlen(char *s)
+size_t my_strlen(const char *s)
{
   - char *p = s;
   + const char *p = s;
   while (*p)
   ++p;
}

Après le test, il valide sa modification.

[[email protected] project]$ git status -s
M string.c
?? string

[[email protected] project]$ git add string.c

[[email protected] project]$ git commit -m 'Changed char pointer to const char pointer'
[master cea2c00] Changed char pointer to const char pointer
1 files changed, 2 insertions(+), 2 deletions(-)

[[email protected] project]$ git log

La commande ci-dessus produira le résultat suivant -

commit cea2c000f53ba99508c5959e3e12fff493b
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 08:32:07 2013 +0530

Changed char pointer to const char pointer


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit

Tom utilise la commande git push pour pousser ses modifications.

[[email protected] project]$ git push origin master

La commande ci-dessus produira le résultat suivant -

Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 336 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To [email protected]:project.git
d1e19d3..cea2c00 master −> master

Ajouter une nouvelle fonction

Pendant ce temps, Jerry décide de mettre en œuvre string compareFonctionnalité. Il modifie donc string.c. Après modification, le fichier se présente comme suit -

[[email protected] project]$ git diff

La commande ci-dessus produira le résultat suivant -

index 7da2992..bc864ed 100644
--- a/string.c
+++ b/string.c
30Git Tutorials
@@ -9,9 +9,20 @@ size_t my_strlen(char *s)
return (p -s );
}
+char *my_strcpy(char *t, char *s)
+
{
   +
   char *p = t;
   +
   + while (*t++ = *s++)
   + ;
   +
   +
   return p;
   +
}
+
int main(void)
{
   int i; 
   +
   char p1[32];
   char *s[] = 
   {
      "Git tutorials",
      "Tutorials Point"
      @@ -20,5 +31,7 @@ int main(void)
      for (i = 0; i < 2; ++i)
      printf("string lenght of %s = %lu\n", s[i], my_strlen(s[i]));
      +
      printf("%s\n", my_strcpy(p1, "Hello, World !!!"));
      +
      return 0;
   }
}

Après les tests, il est prêt à pousser son changement.

[[email protected] project]$ git status -s
M string.c
?? string

[[email protected] project]$ git add string.c

[[email protected] project]$ git commit -m "Added my_strcpy function"
[master e944e5a] Added my_strcpy function
1 files changed, 13 insertions(+), 0 deletions(-)

Avant l'opération push, il vérifie la validation en affichant les messages du journal.

[[email protected] project]$ git log

La commande ci-dessus produira le résultat suivant -

commit e944e5aab74b26e7447d3281b225309e4e59efcd
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:41:42 2013 +0530

Added my_strcpy function


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 07:32:56 2013 +0530

Initial commit

Jerry est satisfait des changements et il veut pousser ses changements.

[[email protected] project]$ git push origin master

La commande ci-dessus produira le résultat suivant -

To [email protected]:project.git
! [rejected]
master −> master (non-fast-forward)
error: failed to push some refs to '[email protected]:project.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again. See the 'Note about
fast-forwards' section of 'git push --help' for details.

Mais Git ne permet pas à Jerry de pousser ses changements. Parce que Git a identifié que le référentiel distant et le référentiel local de Jerry ne sont pas synchronisés. À cause de cela, il peut perdre l'histoire du projet. Pour éviter ce désordre, Git a échoué cette opération. Maintenant, Jerry doit d'abord mettre à jour le référentiel local et seulement par la suite, il peut pousser ses propres modifications.

Récupérer les dernières modifications

Jerry exécute la commande git pull pour synchroniser son référentiel local avec celui distant.

[[email protected] project]$ git pull

La commande ci-dessus produira le résultat suivant -

remote: Counting objects: 5, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 3 (delta 1), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
From git.server.com:project
d1e19d3..cea2c00 master −> origin/master
First, rewinding head to replay your work on top of it...
Applying: Added my_strcpy function

Après l'opération d'extraction, Jerry vérifie les messages du journal et trouve les détails de la validation de Tom avec l'ID de validation cea2c000f53ba99508c5959e3e12fff493ba6f69

[[email protected] project]$ git log

La commande ci-dessus produira le résultat suivant -

commit e86f0621c2a3f68190bba633a9fe6c57c94f8e4f
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:41:42 2013 +0530

Added my_strcpy function


commit cea2c000f53ba99508c5959e3e12fff493ba6f69
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 08:32:07 2013 +0530

Changed char pointer to const char pointer


commit d1e19d316224cddc437e3ed34ec3c931ad803958
Author: Jerry Mouse <[email protected]>
Date: Wed Sep 11 08:05:26 2013 +0530

Changed return type of my_strlen to size_t


commit 19ae20683fc460db7d127cf201a1429523b0e319
Author: Tom Cat <[email protected]>
Date: Wed Sep 11 07:32:56 2013 +0530
Initial commit

Désormais, le référentiel local de Jerry est entièrement synchronisé avec le référentiel distant. Il peut donc pousser ses changements en toute sécurité.

[[email protected] project]$ git push origin master

La commande ci-dessus produira le résultat suivant -

Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 455 bytes, done.
Total 3 (delta 1), reused 0 (delta 0)
To [email protected]:project.git
cea2c00..e86f062 master −> master