Sub topic 10
Remote-tracking branches
This chapter assumes you can already create, switch, and merge local branches, and that you have a repository with at least one remote named origin. You should also know that git clone sets up that remote for you.
What a remote-tracking branch is
A remote-tracking branch is a local, read-only bookmark that records where a branch on a remote repository pointed the last time you communicated with it. It lives in your repository, so reading it needs no network access.
Names take the form <remote>/<branch>, for example origin/main. Git stores them under refs/remotes/, separate from your local branches in refs/heads/.
git branch -r
git branch -a
git show-ref origin/maingit branch -rlists only remote-tracking branches.git branch -alists local and remote-tracking branches together.git show-ref origin/mainprints the commit hash thatorigin/mainpoints to, along with its full ref name.
The word "tracking" is easy to misread. A remote-tracking branch does not follow the remote live. It is a snapshot of what you last saw, and it can be out of date.
Who moves the pointer
You never move a remote-tracking branch with a commit. Git moves it only when it talks to the remote:
git fetchdownloads new commits and updates every remote-tracking branch to match the remote.git pullrunsgit fetchand then merges or rebases.git pushupdates the remote branch and also updates your matching remote-tracking branch.git clonecreates the initial set.
The diagram shows where each pointer lives and which commands move data between them.
Because fetch only moves the remote-tracking branch, it never changes your working files or your local branches. That makes it safe to run at any time.
Reading the state: ahead and behind
After a fetch, origin/main and main can differ in three ways:
- They point to the same commit, so you are up to date.
- Local
mainhas commits thatorigin/mainlacks, so you are ahead. origin/mainhas commits that localmainlacks, so you are behind.- Each has commits the other lacks, so the branches have diverged.
Git can show this at several levels of detail. Start with the commands from the spine for a feature branch:
git push -u origin feature/login
git branch -vv
git fetch origin
git log --oneline main..origin/mainHere is what each line does:
git push -u origin feature/loginpublishes the branch and recordsorigin/feature/loginas its upstream. The chapter on pushing covers-uin detail.git branch -vvprints each local branch with its tip commit and its upstream, for example[origin/feature/login]. When the two differ it adds a count such asahead 1, behind 2.git fetch originrefreshes allorigin/*pointers.git log --oneline main..origin/mainlists commits reachable fromorigin/mainbut not frommain. These are the commits you would receive if you integrated now.
Reverse the range to see what you have not pushed:
git log --oneline origin/main..mainTo compare content rather than commit lists, use a diff against the remote-tracking branch:
git diff main origin/main
git diff main...origin/mainThe two-dot form compares the two tips directly. The three-dot form compares origin/main against the common ancestor of both, so it shows only what changed on the remote side since the branches split. Use the three-dot form when you want to preview incoming changes.
git status reports the same information in words, such as "Your branch is behind 'origin/main' by 2 commits". It reads the stored origin/main, so it is only as fresh as your last fetch.
Local branches that track remote ones
A local branch can name a remote-tracking branch as its upstream. That relationship is what lets git status report ahead and behind, and lets a bare git pull or git push know where to go. It is stored in your config:
git config --get branch.feature/login.remote
git config --get branch.feature/login.mergeThe first prints origin. The second prints refs/heads/feature/login, the branch name on the remote.
When you clone, Git sets this up for main automatically. For a branch that exists only on the remote, switching to it by name creates a local branch and sets its upstream in one step, as long as exactly one remote has a branch with that name:
git fetch origin
git switch feature/loginIf you have two remotes that both have feature/login, Git cannot choose and the command fails. Name the source explicitly:
git switch --track -c feature/login origin/feature/loginYou can also change the upstream of an existing branch:
git branch --set-upstream-to=origin/feature/loginChecking out a remote-tracking branch
You cannot commit onto origin/main. If you run git switch origin/main, Git refuses and suggests --detach, because the ref is not a local branch. With git switch --detach origin/main or the older git checkout origin/main, you land in a detached HEAD, which is fine for inspecting code but not for starting work. To work on it, create a local branch from it:
git switch -c fix-typo origin/mainStale remote-tracking branches
When someone deletes a branch on the remote, your origin/<branch> pointer stays. Git does not remove it on a plain fetch, so git branch -r keeps listing branches that no longer exist.
git fetch --prune
git branch -vv--prune deletes remote-tracking branches whose remote counterparts are gone. Afterward, a local branch whose upstream disappeared shows : gone] in the git branch -vv output. The local branch itself is not deleted, so you clean it up yourself with git branch -d.
To prune on every fetch, set it once:
git config --global fetch.prune trueCommon errors
| Symptom | Cause | Fix |
|---|---|---|
| `origin/main` lacks recent commits a teammate pushed | You have not fetched | Run `git fetch` |
| `git status` says "up to date" but the server has changes | `status` compares against the stored pointer, not the live remote | Fetch first, then check again |
| Deleted branches still appear in `git branch -r` | A plain fetch does not prune | Run `git fetch --prune` |
| `fatal: invalid reference: origin/feature` when switching | The branch name was never fetched or has a typo | Run `git fetch origin`, then `git branch -r` to check the name |
| `ahead 1, behind 1` on a branch | The local and remote histories diverged | Merge or rebase, covered in later chapters |
Exercises
Exercise
You run git status and it says "Your branch is up to date with 'origin/main'". A colleague pushed a commit to origin/main ten minutes ago. Why does Git say you are up to date, and which command shows you the commit without changing your local main?
Show answer
git status compares local main with the stored remote-tracking branch origin/main, which only changes when you fetch, pull, or push. Your copy of origin/main does not yet include the colleague's commit. Run git fetch origin, which updates origin/main without touching local main or your working files. Then git log --oneline main..origin/main lists the new commit.
Exercise
What is the difference between git diff main origin/main and git diff main...origin/main, and which one previews only the changes the remote has added since your branches split?
Show answer
The two-dot form compares the tips of the two branches directly, so it also shows your own local changes in reverse. The three-dot form compares origin/main with the merge base of the two branches, so it shows only what changed on the remote side. Use git diff main...origin/main to preview incoming changes.