Remote Branches in Git
Table of contents
- Understanding remote branches
- Viewing remote branches
- Tracking branches
- Working with remote branches
- Deleting remote branches
- Common scenarios
1 - Understanding remote branches
Three types of branches
| Type | Example | Description |
|---|---|---|
| Local | main | On your machine |
| Remote-tracking | origin/main | Local copy of the remote state |
| Remote | On GitHub | On the server |
Visualization
Votre machine GitHub
───────────── ──────
main main
│ │
│ origin/main │
│ │ │
└────────┘───────────────────────┘
↑ ↑
git fetch git push
origin/main vs main
main: Your local branchorigin/main: The last known version of the main branch on GitHub
After a git fetch, origin/main is updated but not main.
🔝 Back to table of contents
2 - Viewing remote branches
List the branches
# Branches locales
git branch
# Branches distantes
git branch -r
# Toutes les branches
git branch -a
Sample output
$ git branch -a
* main
feature/login
remotes/origin/main
remotes/origin/develop
remotes/origin/feature/api
View the details of a remote
git remote show origin
Displays:
- Remote URL
- Tracked branches
- Local branches configured for push/pull
- Deleted remote branches
Update the list of remote branches
# Récupérer les nouvelles branches
git fetch origin
# Supprimer les références aux branches supprimées
git fetch --prune
# ou
git remote prune origin
🔝 Back to table of contents
3 - Tracking branches
What is a tracking branch?
A local branch that "follows" a remote branch.
# Voir les branches avec leur tracking
git branch -vv
Result:
* main abc1234 [origin/main] Dernier commit
feature/login def5678 [origin/feature/login: ahead 2] WIP
Configure tracking
# Lors de la création
git checkout -b feature/new origin/feature/new
# Ou après
git branch --set-upstream-to=origin/feature/new feature/new
git branch -u origin/feature/new # raccourci
First push with tracking
git push -u origin feature/new
# Équivalent à
git push --set-upstream origin feature/new
Benefits of tracking
# Sans tracking
git pull origin main
git push origin main
# Avec tracking
git pull
git push
🔝 Back to table of contents
4 - Working with remote branches
Retrieve a remote branch
# Méthode 1 : Checkout direct (crée une tracking branch)
git checkout feature/api
# Git détecte origin/feature/api et crée une branche locale
# Méthode 2 : Explicite
git checkout -b feature/api origin/feature/api
# Méthode 3 : Fetch puis checkout
git fetch origin feature/api
git checkout feature/api
See the differences with the remote
# Commits locaux non poussés
git log origin/main..main
# Commits distants non récupérés
git log main..origin/main
# Après un fetch
git diff main origin/main
Ahead / Behind
git status
# Your branch is ahead of 'origin/main' by 2 commits.
# Your branch is behind 'origin/main' by 3 commits.
| Status | Meaning | Action |
|---|---|---|
| ahead | Local commits to push | git push |
| behind | Remote commits to retrieve | git pull |
| diverged | Both | git pull then git push |
🔝 Back to table of contents
5 - Deleting remote branches
Delete on the remote
git push origin --delete feature/old
# ou
git push origin :feature/old
Clean up local references
After deletion on GitHub, the origin/... references remain locally:
# Supprimer les références obsolètes
git fetch --prune
# Ou configurer pour toujours pruner
git config --global fetch.prune true
Delete a local and remote branch
# 1. Supprimer localement
git branch -d feature/done
# 2. Supprimer sur le remote
git push origin --delete feature/done
Cleanup script
# Supprimer toutes les branches mergées (sauf main)
git branch --merged | grep -v "main" | xargs git branch -d
# Nettoyer les remote-tracking obsolètes
git remote prune origin
🔝 Back to table of contents
6 - Common scenarios
Scenario 1: A colleague created a new branch
# 1. Récupérer les branches
git fetch origin
# 2. Voir les nouvelles branches
git branch -r
# 3. Checkout la branche
git checkout feature/colleague-feature
Scenario 2: Your branch is behind
$ git status
Your branch is behind 'origin/main' by 5 commits.
# Solution
git pull origin main
Scenario 3: Diverged branches
$ git status
Your branch and 'origin/main' have diverged.
# Solution 1 : Merge
git pull origin main
# Solution 2 : Rebase
git pull --rebase origin main
Scenario 4: Branch deleted on GitHub but still visible locally
# Les anciennes références traînent
git branch -r
# origin/feature/old-deleted
# Nettoyer
git fetch --prune
Command summary
| Command | Description |
|---|---|
git branch -r | List remote branches |
git fetch origin | Update the references |
git fetch --prune | Clean up deleted branches |
git checkout -b local origin/remote | Create a tracking branch |
git push origin --delete branch | Delete a remote branch |
git branch -vv | See branch tracking |