Sub topic 4
Remote-tracking branches: your local view of the remote
This chapter assumes you can commit, create and switch branches, and read git log. It also assumes you know what a remote is and that origin is the default name for the one you cloned from. The chapter explains the three kinds of branch pointer you will see when working with a remote, and why the middle one matters.
Three things called "branch"
Suppose you cloned a project from origin and you work on main. Three different pointers are involved:
| Name | Where it lives | Who moves it |
|---|---|---|
| `main` | Your repository | You, when you commit, merge, or rebase |
| `origin/main` | Your repository | Git, when you fetch or push |
| `main` on the remote | The server | Whoever pushes to it |
main is a local branch. The branch named main on the server is the real shared branch. origin/main is a remote-tracking branch: a read-only bookmark in your repository that records where main on origin was the last time you talked to it.
Why a local copy of the remote's position exists
Git does not contact the server when you run git log or git status. Without a stored bookmark, it could not tell you whether you are ahead or behind. The remote-tracking branch is that stored answer. It can be out of date, and it only changes when Git communicates with the remote.
You can see these references directly:
git branch -r
git branch -a
git show-ref origin/maingit branch -r lists only remote-tracking branches. git branch -a lists local and remote-tracking branches together. Remote-tracking branches are stored under refs/remotes/<remote>/<branch>.
How they move
You cannot move origin/main by committing. If you run git switch origin/main, Git puts you in detached HEAD state, because it will not let you add commits to a bookmark that belongs to the remote. Only network operations update it:
git fetchdownloads new commits and movesorigin/mainto match the server.git pushmovesorigin/mainforward after the server accepts your commits.git clonecreates it for the first time.
Seeing the gap between pointers
Because both pointers live in your repository, you can compare them offline. After a fetch, they may point at different commits:
git fetch origin
git status
git log --oneline main..origin/main
git log --oneline origin/main..mainThe first git log shows commits on the remote that you lack. The second shows commits you have that the remote lacks. git status summarizes the same comparison in words, such as "Your branch is behind 'origin/main' by 2 commits".
Common errors
- Trusting a stale
origin/main. If you have not fetched recently,git statuscompares against an old bookmark. "Up to date with 'origin/main'" means up to date with your last fetch, not with the server. - Committing on a detached HEAD. Checking out
origin/maindirectly and committing creates commits no branch points to. Create a local branch first withgit switch -c fix origin/main. - Confusing
mainandorigin/mainin commands.git merge mainmerges your local branch. To merge what the server has, you needgit merge origin/main, and only after a fetch.
Exercise
You last ran git fetch yesterday. A teammate pushed to main this morning. What does git status say about your main compared with origin/main, and why?
Show answer
It says your branch is up to date (assuming you have no local commits). git status compares main with the stored origin/main bookmark, which only changes on fetch, push, or clone. Until you run git fetch, Git does not know about the teammate's commits.
Exercise
What command creates a local branch named review that starts at the current position of origin/main and switches to it?
Show answer
git switch -c review origin/main