Un remote est un dépôt Git hébergé ailleurs (GitHub, GitLab, serveur
interne). Le cycle de collaboration repose sur trois opérations :
fetch (récupérer), merge/rebase (intégrer), push (envoyer).
Ce guide détaille chaque étape et la gestion de plusieurs remotes.
Prérequis : Branches distantes et Les bases de Git.
Ce que vous allez apprendre
Section intitulée « Ce que vous allez apprendre »- Configurer un remote : ajouter, renommer, supprimer avec
git remote - Synchroniser votre dépôt : fetch → merge/rebase → push
- Travailler avec plusieurs remotes (origin, upstream, fork)
- Comprendre la différence entre
git fetchetgit pull
Qu’est-ce qu’un remote ?
Section intitulée « Qu’est-ce qu’un remote ? »Un remote est un alias vers l’URL d’un dépôt distant. Quand vous
clonez un dépôt, Git crée automatiquement un remote nommé origin :
git remote -vorigin https://github.com/user/project.git (fetch)origin https://github.com/user/project.git (push)L’URL de fetch et de push peuvent être différentes (rare mais possible).
Gérer les remotes
Section intitulée « Gérer les remotes »Ajouter un remote
Section intitulée « Ajouter un remote »git remote add upstream https://github.com/original/project.gitCas d’usage : vous travaillez sur un fork et ajoutez le dépôt original
comme upstream pour synchroniser.
Lister les remotes
Section intitulée « Lister les remotes »# Noms uniquementgit remote
# Avec les URLsgit remote -v
# Détails complets (branches trackées, HEAD, etc.)git remote show originRenommer un remote
Section intitulée « Renommer un remote »git remote rename origin githubToutes les références (github/main, github/develop) sont
automatiquement mises à jour.
Supprimer un remote
Section intitulée « Supprimer un remote »git remote remove upstreamLes branches de tracking associées (upstream/main, etc.) sont
également supprimées.
Le cycle de collaboration
Section intitulée « Le cycle de collaboration »Le workflow standard suit trois étapes :
Serveur distant Votre machine │ │ │ 1. git fetch │ │ ──────────────────────► │ (télécharge les commits) │ │ │ 2. merge / rebase │ │ │ (intègre localement) │ │ │ 3. git push │ │ ◄────────────────────── │ (envoie vos commits) │ │1. git fetch : récupérer
Section intitulée « 1. git fetch : récupérer »git fetch originTélécharge tous les nouveaux commits, branches et tags depuis origin.
Ne modifie pas vos branches locales.
# Fetch depuis tous les remotesgit fetch --all
# Fetch + suppression des refs périméesgit fetch --prune2. Intégrer : merge ou rebase
Section intitulée « 2. Intégrer : merge ou rebase »Après le fetch, intégrez les changements distants dans votre branche :
# Option merge (conserve l'historique)git merge origin/main
# Option rebase (historique linéaire)git rebase origin/main3. git push : envoyer
Section intitulée « 3. git push : envoyer »# Push de la branche courantegit push
# Premier push (crée la branche distante + tracking)git push -u origin feature/login
# Push de toutes les branchesgit push --all origin
# Push des tagsgit push --tagsgit pull : le raccourci
Section intitulée « git pull : le raccourci »git pull combine fetch + merge (ou fetch + rebase) :
# fetch + merge (par défaut)git pull origin main
# fetch + rebasegit pull --rebase origin main| Commande | Équivalent | Modifie la branche locale ? |
|---|---|---|
git fetch | — | Non |
git pull | git fetch + git merge | Oui |
git pull --rebase | git fetch + git rebase | Oui |
Travailler avec plusieurs remotes
Section intitulée « Travailler avec plusieurs remotes »Cas typique : vous contribuez à un projet open source via un fork.
-
Forkez le projet sur GitHub/GitLab
-
Clonez votre fork :
Fenêtre de terminal git clone https://github.com/vous/project.git -
Ajoutez le dépôt original :
Fenêtre de terminal git remote add upstream https://github.com/original/project.git -
Synchronisez régulièrement :
Fenêtre de terminal git fetch upstreamgit switch maingit merge upstream/maingit push origin main
Vos remotes :
git remote -vorigin https://github.com/vous/project.git (fetch)origin https://github.com/vous/project.git (push)upstream https://github.com/original/project.git (fetch)upstream https://github.com/original/project.git (push)Changer l’URL d’un remote
Section intitulée « Changer l’URL d’un remote »Si le dépôt change d’hébergement ou si vous passez de HTTPS à SSH :
# Voir l'URL actuellegit remote get-url origin
# Changer l'URLgit remote set-url origin git@github.com:user/project.gitDépannage : problèmes courants
Section intitulée « Dépannage : problèmes courants »| Symptôme | Cause probable | Solution |
|---|---|---|
fatal: 'origin' does not appear to be a git repository | Remote mal configuré | git remote -v, vérifiez l’URL |
! [rejected] (non-fast-forward) | Remote en avance sur vos commits | git pull --rebase puis git push |
Permission denied (publickey) | Clé SSH non configurée | Vérifiez ssh -T git@github.com |
remote: Repository not found | URL incorrecte ou accès refusé | Vérifiez l’URL et vos permissions |
| Fetch ne ramène rien | Déjà à jour | git log origin/main..main pour vérifier la diff |
À retenir
Section intitulée « À retenir »originest l’alias par défaut du remote créé au clone- Le cycle : fetch → merge/rebase → push
git fetchrécupère sans modifier —git pullrécupère et intègregit push -ucrée la branche distante et configure le tracking- Plusieurs remotes :
origin(votre fork) +upstream(dépôt original) pour les contributions open source