Skip to content
Commencer

Collaboration avec GitHub

SlidesPDFPPTX
Niveau : Beginner
Durée : 2h

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

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-&gt;&gt;Cloud: git push origin main Cloud-&gt;&gt;Collab: git fetch / pull Collab-&gt;&gt;Cloud: git push origin feature Cloud-&gt;&gt;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

Terminal window
git remote add origin https://github.com/votre-username/atelier-git.git
git 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.git
git remote get-url origin
git remote show origin
# * remote origin
# Fetch URL: ...
# Push URL: ...
# HEAD branch: main
# Remote branches: main tracked
git remote remove upstream
<pre dir="ltr" class="mermaid">flowchart LR A[Votre fork&lt;br/&gt;origin] &lt;--&gt; B[Local&lt;br/&gt;git remote -v] C[Upstream&lt;br/&gt;hashcode/original] --&gt; B B --&gt; A</pre>

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

Premier push — créer la branche distante

Section titled “Premier push — créer la branche distante”
Terminal window
git switch main
git 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 :

Terminal window
git push # suffit (upstream déjà connu)
git push origin main # explicite
Terminal window
git branch -vv
# * main a1b2c3d [origin/main] feat: initial
# feature e4f5a6b [origin/feature: ahead 2] feat: login
git switch feature
git status
# On branch feature
# Your branch is ahead of 'origin/feature' by 2 commits.
# (use "git push" to publish your local commits)
git switch main
git status
# On branch main
# Your branch is up to date with 'origin/main'.
# nothing to commit, working tree clean
Terminal window
git switch -c feature-contact
git push -u origin feature-contact
# → crée origin/feature-contact
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 :

Terminal window
git remote set-url origin git@github.com:votre-username/atelier-git.git

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

Terminal window
git clone https://github.com/votre-username/nom-du-projet.git
# ou SSH
git clone git@github.com:votre-username/nom-du-projet.git
# ou gh
gh repo clone votre-username/nom-du-projet
cd nom-du-projet
ls -la
# .git README.md ...
git remote -v
# origin https://... (fetch)

Options :

Terminal window
git clone --depth 1 https://github.com/user/repo.git # shallow, rapide
git clone --branch feature-login https://... # une seule branche
gh repo clone joinhashcode/workshops -- --depth 1

Terminal window
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 local
Terminal window
git pull origin main
# = git fetch origin + git merge origin/main
git pull --rebase origin main # variante rebase (avancé)
<pre dir="ltr" class="mermaid">flowchart LR A[origin/main&lt;br/&gt;distant] --&gt;|git fetch| B[origin/main&lt;br/&gt;local copy] B --&gt;|git merge| C[main&lt;br/&gt;votre branche] B -.-&gt;|git pull| C</pre>
  1. git fetch origin — voir ce qui a bougé 2. git status — Your branch is behind 'origin/main' by 3 commits 3. git pull origin main — rapatrier + merger 4. Si CONFLICT → résoudre (git add + git commit) 5. git push origin main

Commandes à exécuter

bash
# Cliquez sur les commandes à gauche pour les exécuter dans le simulateur
hashcode-academy:~$_

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
Terminal window
gh auth status
gh repo view --web
gh repo clone joinhashcode/workshops
gh browse

Question

Différence local vs distant ?

Cliquer pour révéler la réponse
Réponse

Local : .git sur disque, hors ligne. Distant : GitHub, partage, sauvegarde, collaboration.

Cliquer pour voir la question
Question

Que fait git remote add origin <url> ?

Cliquer pour révéler la réponse
Réponse

Crée l'alias origin pointant vers l'URL GitHub distante.

Cliquer pour voir la question
Question

Que fait -u dans git push -u origin main ?

Cliquer pour révéler la réponse
Réponse

--set-upstream : lie la branche locale à origin/main pour les futurs git push/pull sans args.

Cliquer pour voir la question
Question

git fetch vs git pull ?

Cliquer pour révéler la réponse
Réponse

fetch = télécharge origin/main sans merger. pull = fetch + merge dans votre branche.

Cliquer pour voir la question
Question

À quoi sert origin ?

Cliquer pour révéler la réponse
Réponse

Nom par défaut du remote principal. Upstream = dépôt d'origine en cas de fork.

Cliquer pour voir la question
Question

Comment changer HTTPS → SSH ?

Cliquer pour révéler la réponse
Réponse

git remote set-url origin git@github.com:user/repo.git

Cliquer pour voir la question
Question

Que fait git clone ?

Cliquer pour révéler la réponse
Réponse

Copie complète distante : .git + historique + remote origin configuré.

Cliquer pour voir la question
Question

Comment voir le tracking ?

Cliquer pour révéler la réponse
Réponse

git branch -vv ou git status (Your branch is ... with origin/main).

Cliquer pour voir la question

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 ?


Terminal window
# Remotes
git remote -v
git remote add origin https://github.com/user/repo.git
git remote add upstream https://github.com/original/repo.git
git remote rename origin github
git remote set-url origin git@github.com:user/repo.git
git remote show origin
git remote remove upstream
# Push / upstream
git push -u origin main
git branch -vv
git push
git push origin feature-xxx
# Clone
git clone https://github.com/user/repo.git
git clone git@github.com:user/repo.git
gh repo clone user/repo
git clone --depth 1 https://...
# Fetch / Pull
git fetch origin
git log --oneline origin/main --not main
git pull origin main
git pull --rebase origin main
# Auth
ssh -T git@github.com
gh auth login
gh auth status

Votre Progression0%


  • Local = votre disque, distant = GitHub. origin est l’alias du remote principal.
  • git push -u origin main crée l’upstream — ensuite git push suffit.
  • fetch informe, pull fusionne. Toujours fetch avant pull.
  • clone = init + remote + fetch + checkout en une commande.

Vous savez pousser/tirer du code. Apprenons le workflow propre qui protège main : les Pull Requests.

Étape suivante : Le Workflow Pull Request

HashCode Workshops
Part of the JoinHashCode ecosystem

Ctrl + K Rechercher dans les workshops