Collaboration avec GitHub
Prérequis
- Maîtriser git init, add, commit, branches, merge (Ateliers 02-06)
- Avoir un compte GitHub et une clé SSH ou gh auth (Atelier 02)
- Savoir faire git status et git log
Objectifs d'apprentissage
- Distinguer dépôt local vs distant et le rôle d'origin
- Gérer les remotes : add, rename, remove, set-url, show
- Pousser avec git push -u et comprendre l'upstream (tracking)
- Cloner, fetcher et puller sans écraser son travail
Dépôt local vs Dépôt distant
Section titled “Dépôt local vs Dépôt distant”Jusqu’ici tout était local. Pour collaborer, on pousse vers un remote hébergé (GitHub).
<pre dir="ltr" class="mermaid">sequenceDiagram participant Local as Votre Machine (Local) participant Cloud as GitHub (Distant) participant Collab as Collègue Local->>Cloud: git push origin main Cloud->>Collab: git fetch / pull Collab->>Cloud: git push origin feature Cloud->>Local: git fetch + merge</pre>| Local | Distant |
|---|---|
.git sur votre disque |
GitHub user/repo.git |
git status, git log |
git push, git pull, UI web |
| Rapide, hors ligne | Partage, sauvegarde, CI |
Gérer les remotes : git remote
Section titled “Gérer les remotes : git remote”Ajouter, voir, renommer, supprimer
Section titled “Ajouter, voir, renommer, supprimer”git remote add origin https://github.com/votre-username/atelier-git.gitgit remote -v# origin https://github.com/... (fetch)# origin https://github.com/... (push)
# Cas du fork : vous avez cloné le dépôt d'origine…git remote rename origin upstream # …il devient « upstream »git remote add origin https://github.com/votre-username/fork.git # votre fork devient « origin »
git remote set-url origin git@github.com:votre-username/fork.gitgit remote get-url origingit remote show origin# * remote origin# Fetch URL: ...# Push URL: ...# HEAD branch: main# Remote branches: main tracked
git remote remove upstreamCommandes à exécuter
Pousser : git push & upstream
Section titled “Pousser : git push & upstream”Premier push — créer la branche distante
Section titled “Premier push — créer la branche distante”git switch maingit push -u origin main# Enumerating objects: 3, done.# * [new branch] main -> main# branch 'main' set up to track 'origin/main'.-u = --set-upstream → lie main local à origin/main distant. Ensuite :
git push # suffit (upstream déjà connu)git push origin main # expliciteVoir le tracking
Section titled “Voir le tracking”git branch -vv# * main a1b2c3d [origin/main] feat: initial# feature e4f5a6b [origin/feature: ahead 2] feat: login
git switch featuregit status# On branch feature# Your branch is ahead of 'origin/feature' by 2 commits.# (use "git push" to publish your local commits)
git switch maingit status# On branch main# Your branch is up to date with 'origin/main'.# nothing to commit, working tree cleanPousser une feature
Section titled “Pousser une feature”git switch -c feature-contactgit push -u origin feature-contact# → crée origin/feature-contactAuth : HTTPS vs SSH
Section titled “Auth : HTTPS vs SSH”| Méthode | URL | Auth |
|---|---|---|
| HTTPS | https://github.com/user/repo.git |
PAT (gh auth login) ou GCM |
| SSH | git@github.com:user/repo.git |
Clé ed25519 + ssh -T git@github.com |
Changer après coup :
git remote set-url origin git@github.com:votre-username/atelier-git.gitCommandes à exécuter
Récupérer un projet : git clone
Section titled “Récupérer un projet : git clone”git clone https://github.com/votre-username/nom-du-projet.git# ou SSHgit clone git@github.com:votre-username/nom-du-projet.git# ou ghgh repo clone votre-username/nom-du-projet
cd nom-du-projetls -la# .git README.md ...git remote -v# origin https://... (fetch)Options :
git clone --depth 1 https://github.com/user/repo.git # shallow, rapidegit clone --branch feature-login https://... # une seule branchegh repo clone joinhashcode/workshops -- --depth 1Rester synchro : git fetch vs git pull
Section titled “Rester synchro : git fetch vs git pull”git fetch — informer sans fusionner
Section titled “git fetch — informer sans fusionner”git fetch origin# Télécharge les nouveaux commits dans origin/main (ne touche PAS vos fichiers)git log --oneline origin/main --not main# montre ce qui est distant mais pas encore localgit pull — fetch + merge
Section titled “git pull — fetch + merge”git pull origin main# = git fetch origin + git merge origin/maingit pull --rebase origin main # variante rebase (avancé)Workflow quotidien
Section titled “Workflow quotidien”git fetch origin— voir ce qui a bougé 2.git status—Your branch is behind 'origin/main' by 3 commits3.git pull origin main— rapatrier + merger 4. SiCONFLICT→ résoudre (git add+git commit) 5.git push origin main
Commandes à exécuter
Auth moderne & GitHub CLI
Section titled “Auth moderne & GitHub CLI”PAT vs SSH vs GCM
Section titled “PAT vs SSH vs GCM”| Méthode | Config | Usage |
|---|---|---|
| GCM (Windows) | git config --global credential.helper manager |
Stocke PAT sécurisé |
| osxkeychain (macOS) | credential.helper osxkeychain |
Keychain |
| libsecret (Linux) | credential.helper libsecret |
Porte-clés |
| SSH | ssh-keygen -t ed25519 |
git@github.com:... |
| gh | gh auth login |
PAT + gh CLI |
gh auth statusgh repo view --webgh repo clone joinhashcode/workshopsgh browseFiches de révision
Section titled “Fiches de révision”Différence local vs distant ?
Cliquer pour révéler la réponseLocal : .git sur disque, hors ligne. Distant : GitHub, partage, sauvegarde, collaboration.
Cliquer pour voir la questionQue fait git remote add origin <url> ?
Cliquer pour révéler la réponseCrée l'alias origin pointant vers l'URL GitHub distante.
Cliquer pour voir la questionQue fait -u dans git push -u origin main ?
Cliquer pour révéler la réponse--set-upstream : lie la branche locale à origin/main pour les futurs git push/pull sans args.
Cliquer pour voir la questiongit fetch vs git pull ?
Cliquer pour révéler la réponsefetch = télécharge origin/main sans merger. pull = fetch + merge dans votre branche.
Cliquer pour voir la questionÀ quoi sert origin ?
Cliquer pour révéler la réponseNom par défaut du remote principal. Upstream = dépôt d'origine en cas de fork.
Cliquer pour voir la questionComment changer HTTPS → SSH ?
Cliquer pour révéler la réponsegit remote set-url origin git@github.com:user/repo.git
Cliquer pour voir la questionQue fait git clone ?
Cliquer pour révéler la réponseCopie complète distante : .git + historique + remote origin configuré.
Cliquer pour voir la questionComment voir le tracking ?
Cliquer pour révéler la réponsegit branch -vv ou git status (Your branch is ... with origin/main).
Cliquer pour voir la questionQuiz Final
Section titled “Quiz Final”1. Quelle commande envoie vos commits locaux vers GitHub ?
2. Différence fetch / pull ?
3. Que fait git clone ?
4. Que fait -u dans push -u origin main ?
Cheat Sheet
Section titled “Cheat Sheet”# Remotesgit remote -vgit remote add origin https://github.com/user/repo.gitgit remote add upstream https://github.com/original/repo.gitgit remote rename origin githubgit remote set-url origin git@github.com:user/repo.gitgit remote show origingit remote remove upstream
# Push / upstreamgit push -u origin maingit branch -vvgit pushgit push origin feature-xxx
# Clonegit clone https://github.com/user/repo.gitgit clone git@github.com:user/repo.gitgh repo clone user/repogit clone --depth 1 https://...
# Fetch / Pullgit fetch origingit log --oneline origin/main --not maingit pull origin maingit pull --rebase origin main
# Authssh -T git@github.comgh auth logingh auth statusMini Challenge
Section titled “Mini Challenge”Carte mentale
Section titled “Carte mentale”À retenir
Section titled “À retenir”- Local = votre disque, distant = GitHub.
originest l’alias du remote principal. git push -u origin maincrée l’upstream — ensuitegit pushsuffit.fetchinforme,pullfusionne. Toujoursfetchavantpull.clone=init+remote+fetch+checkouten une commande.
Prochaine étape
Section titled “Prochaine étape”Vous savez pousser/tirer du code. Apprenons le workflow propre qui protège main : les Pull Requests.
Étape suivante : Le Workflow Pull Request