SyntaxStudy
Sign Up
Git Beginner 7 min read

Tracking Branches

A tracking branch is a local branch that has a direct relationship to a remote branch. Git uses this relationship to know where to push and pull from, and to show you how many commits ahead or behind you are relative to the remote.

Example
# Set up tracking when pushing a new branch:
git push -u origin feature/login
# -u is short for --set-upstream

# Set upstream for an existing branch:
git branch --set-upstream-to=origin/main main

# See tracking information for all branches:
git branch -vv
# * main         abc1234 [origin/main: ahead 2] Add login page
#   feature/x    def5678 [origin/feature/x] Work in progress

# "ahead 2"  — you have 2 commits not yet pushed
# "behind 3" — remote has 3 commits not yet pulled

# Fetch all remotes and prune deleted remote branches:
git fetch --all --prune

# List remote-tracking branches:
git branch -r

# Check out a remote branch (creates local tracking branch):
git switch -c feature/x origin/feature/x
# Shortcut (Git 2.23+):
git switch feature/x   # automatically tracks origin/feature/x