Ch. 16 · Git & CI/CD

Git Remote-Tracking Branches

Understand remote-tracking refs, the difference between fetch and pull, and how tracking branches stay in sync.

~2 min readintermediateupdated Oct 5, 2026

A local branch and its remote copy are separate refs. origin/main is a remote-tracking ref that records the last known state of the remote; it updates when you fetch, not automatically. Confusing the two is behind many “my branch is wrong” problems.

Before you start

You should be comfortable with branches and remotes. This article covers remote-tracking refs and synchronizing.

Step-by-step walkthrough

Step 1: Distinguish local and remote-tracking refs

main is your local branch; origin/main is a snapshot of the remote’s main from the last fetch. They can differ, and git status tells you how. Comparing against origin/main compares against the last fetch, not the live remote.

Step 2: Fetch updates without changing your work

git fetch downloads new objects and updates remote-tracking refs, leaving your working tree alone. git pull is fetch plus a merge or rebase into your current branch, so it changes your working tree. Fetch-then-inspect is the safer default because it separates download from integration.

Step 3: Use tracking branches

A local branch can track a remote branch, so git status shows ahead and behind counts and git push knows the default target. Setting upstream with -u makes the relationship explicit and avoids repeated remote arguments.

Worked scenario

Fetch updates the ref without touching the working tree.

git fetch origin
git log --oneline origin/main      # inspect what arrived
git status                          # see ahead/behind vs origin/main
git pull --rebase                   # integrate if appropriate
Terminal

Walk through the example

git fetch brings down commits and moves origin/main forward, but your main is unchanged. You inspect what arrived, then decide whether to merge or rebase. git status compares your branch to origin/main, which is the snapshot from the last fetch.

Common mistake

Assuming git status compares to the live remote, when it compares to the last fetched snapshot. Another is running git pull on a branch with local changes and getting an unexpected merge commit.

Verify the behavior

Fetch and confirm origin/main moves while main stays. Compare git rev-parse main and git rev-parse origin/main. Push a commit and confirm git status reports the branch as ahead.

Interview exercise

What is the difference between git fetch and git pull?

Answer and reasoning

git fetch downloads new objects and updates remote-tracking refs without changing your working tree or current branch. git pull is git fetch followed by integrating the fetched changes into your branch, via merge or rebase, which does change your working tree. Fetch separates download from integration; pull combines them.

Continue learning

Compare integration in Merge vs rebase and branches in Branch strategies. Read the Git remote branches documentation and try the Git interview questions.

More in Git & CI/CD

read ✓Git & CI/CD · easy

Git Detached HEAD

What a detached HEAD is, when it happens, and how to keep commits made in that state.

~2 min readread →
read ✓Git & CI/CD · mid

Git Hooks and Local Automation

Automate checks with client and server hooks, share them through a framework, and enforce the same rules in CI where they cannot be bypassed.

~2 min readread →
read ✓Git & CI/CD · hard

Git LFS for Large Files

Track large binaries with Git LFS, keep pointer files in the repository, and plan for LFS storage and access.

~2 min readread →
esc