Git - Dépôts en ligne
Git - Dépôts en Ligne (par ex., GitHub)
Section titled “Git - Dépôts en Ligne (par ex., GitHub)”Des plateformes comme GitHub, GitLab et Bitbucket fournissent un hébergement web pour les dépôts Git, ainsi que des fonctionnalités pour la collaboration, le suivi des problèmes, et plus encore. Cette section se concentre sur l’interaction avec un dépôt GitHub en utilisant la ligne de commande.
Créer un Dépôt GitHub
Section titled “Créer un Dépôt GitHub”- Allez sur GitHub.
- Connectez-vous à votre compte ou créez-en un nouveau.
- Créez un nouveau dépôt en cliquant sur le bouton “New” ou l’icône ’+’ dans la barre de navigation. Suivez les instructions à l’écran, en donnant un nom à votre dépôt (par exemple,
mon_premier_depot). Vous pouvez choisir de l’initialiser avec un README, un .gitignore et une licence.
Pousser (Push) un Dépôt Local Existant vers GitHub
Section titled “Pousser (Push) un Dépôt Local Existant vers GitHub”Tom a un projet Git local existant et veut l’héberger sur GitHub. D’abord, il crée un nouveau dépôt vide sur GitHub (par exemple, nommé hello_world_project) sans l’initialiser avec un README, un .gitignore ou une licence, car son projet local a déjà son propre historique.
Tom a un projet local avec un fichier hello.c :
[tom@devbox ~]$ mkdir hello_world_project[tom@devbox ~]$ cd hello_world_project/
[tom@devbox hello_world_project]$ echo '#include <stdio.h>int main() { printf("Hello, Modern Git!\n"); return 0;}' > hello.c
[tom@devbox hello_world_project]$ gcc hello.c -o hello[tom@devbox hello_world_project]$ ./helloLa commande ci-dessus produira le résultat suivant :
Hello, Modern Git!Après avoir vérifié son code, il initialise un dépôt Git localement, ajoute et valide son fichier :
[tom@devbox hello_world_project]$ git init -b mainInitialized empty Git repository in /home/tom/hello_world_project/.git/
[tom@devbox hello_world_project]$ git status -s?? hello.c
[tom@devbox hello_world_project]$ git add hello.c
[tom@devbox hello_world_project]$ git status -sA hello.c
[tom@devbox hello_world_project]$ git commit -m "Initial commit with hello.c"Ensuite, il ajoute l’URL du dépôt GitHub (fournie par GitHub lorsque vous créez un nouveau dépôt, par ex., https://github.com/votrenomdutilisateur/hello_world_project.git) comme un remote nommé origin et pousse sa branche main :
[tom@devbox hello_world_project]$ git remote add origin https://github.com/yourusername/hello_world_project.git
[tom@devbox hello_world_project]$ git push -u origin mainAuthentification : Lorsque vous poussez vers GitHub via HTTPS, Git vous demandera vos identifiants. Au lieu de votre mot de passe, vous devez utiliser un Jeton d’Accès Personnel (Personal Access Token ou PAT). Vous pouvez générer un PAT depuis les paramètres Développeur de votre compte GitHub. Pour les URL SSH (par ex., git@github.com:votrenomdutilisateur/hello_world_project.git), vous utiliseriez des clés SSH. Pour plus de détails, consultez la documentation de GitHub sur les PATs.
Après une authentification réussie, la sortie sera similaire à :
Enumerating objects: 3, done.Counting objects: 100% (3/3), done.Writing objects: 100% (3/3), 250 bytes | 250.00 KiB/s, done.Total 3 (delta 0), reused 0 (delta 0), pack-reused 0To https://github.com/yourusername/hello_world_project.git * [new branch] main -> mainBranch 'main' set up to track remote branch 'main' from 'origin'.Tom peut maintenant pousser toute modification ultérieure vers ce dépôt GitHub en utilisant git push.
Cloner un Dépôt depuis GitHub (Opération Pull)
Section titled “Cloner un Dépôt depuis GitHub (Opération Pull)”Tom a poussé avec succès son projet sur GitHub. Maintenant, un autre développeur, Jerry, peut obtenir une copie de ce projet.
Jerry crée un répertoire et clone le dépôt GitHub en utilisant son URL HTTPS :
[jerry@devbox ~]$ mkdir jerry_projects[jerry@devbox ~]$ cd jerry_projects/
[jerry@devbox jerry_projects]$ git clone https://github.com/yourusername/hello_world_project.gitLa commande ci-dessus produit une sortie comme celle-ci :
Cloning into 'hello_world_project'...remote: Enumerating objects: 3, done.remote: Counting objects: 100% (3/3), done.remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0Unpacking objects: 100% (3/3), 250 bytes | 125.00 KiB/s, done.Il vérifie le contenu du répertoire :
[jerry@devbox jerry_projects]$ lshello_world_project
[jerry@devbox jerry_projects]$ cd hello_world_project/[jerry@devbox hello_world_project]$ lshello.cJerry a maintenant une copie locale et peut commencer à contribuer. Pour obtenir les futures mises à jour de Tom, Jerry utiliserait git pull à l’intérieur de son répertoire hello_world_project.