Skip to main content

Remote Branches in Git


Table of contents


  1. Understanding remote branches
  2. Viewing remote branches
  3. Tracking branches
  4. Working with remote branches
  5. Deleting remote branches
  6. Common scenarios


1 - Understanding remote branches



Three types of branches

TypeExampleDescription
LocalmainOn your machine
Remote-trackingorigin/mainLocal copy of the remote state
RemoteOn GitHubOn the server

Visualization

Votre machine                    GitHub
───────────── ──────

main main
│ │
│ origin/main │
│ │ │
└────────┘───────────────────────┘
↑ ↑
git fetch git push

origin/main vs main

  • main: Your local branch
  • origin/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.
StatusMeaningAction
aheadLocal commits to pushgit push
behindRemote commits to retrievegit pull
divergedBothgit 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

CommandDescription
git branch -rList remote branches
git fetch originUpdate the references
git fetch --pruneClean up deleted branches
git checkout -b local origin/remoteCreate a tracking branch
git push origin --delete branchDelete a remote branch
git branch -vvSee branch tracking

🔝 Back to table of contents