Sub topic 1
Remote-tracking branches: what origin/feature really records
Prerequisites
This chapter assumes you can commit, create branches, push, and pull. It also assumes you know that a remote such as origin is another copy of the repository, usually on a server. You don't need to have used git rebase yet. Later chapters rely on the idea explained here, so read this one first.
Three things called "feature"
When you work on a branch called feature/login, three separate pointers are involved:
| Name | Where it lives | Who moves it |
|---|---|---|
| `feature/login` | Your repository, under `refs/heads/` | You, with commits, rebases, and resets |
| `origin/feature/login` | Your repository, under `refs/remotes/origin/` | Git, when you fetch or push |
| `feature/login` on the server | The remote repository | Anyone who pushes to it |
The middle one is a remote-tracking branch. It is a local reference that records where a branch on the remote was the last time your Git talked to that remote. You can't commit onto it directly. Git updates it for you.
It is a snapshot, not a live view
origin/feature/login does not query the server. It holds a commit hash, and that hash was correct at some past moment. You can read it like this:
git rev-parse origin/feature/login
git for-each-ref refs/remotes/origin/feature/loginThe first command prints the full hash. The second prints the hash, the object type, and the full ref name, refs/remotes/origin/feature/login. Both commands work offline, which shows that no network lookup is involved.
Now suppose a teammate pushes a new commit to feature/login on the server. Nothing changes in your repository. Your origin/feature/login still points at the old commit, and Git still reports the old state. Only a network operation can refresh it.
What updates a remote-tracking branch
Two operations move origin/feature/login:
git fetch origindownloads new commits and sets each remote-tracking branch to the server's current value.git push origin feature/loginsetsorigin/feature/loginto the commit you just pushed, once the server accepts it.
git pull runs a fetch first, so it updates the pointer too. Plain git status and git log never do.
git fetch origin
git log --oneline feature/login..origin/feature/loginThe log command lists commits that the remote branch has and your local branch lacks. The two-dot range means "reachable from the right side but not from the left."
The diagram below shows how the pointer changes.
Reading what Git tells you
The line Your branch is up to date with 'origin/feature/login' in git status compares your local branch with the remote-tracking branch only. It does not mean the server has nothing new. It means your branch matches your last known copy of the server.
On branch feature/login
Your branch is up to date with 'origin/feature/login'.Treat this as "up to date as of the last fetch or push." The age of that fetch is not shown.
You can see which remote-tracking branches exist, and which local branch tracks which, with:
git branch -r
git branch -vvgit branch -r lists remote-tracking branches. git branch -vv shows each local branch, its upstream, and how many commits you are ahead or behind that upstream. Ahead and behind counts use the stored pointer, so they carry the same staleness.
Common mistakes
- Assuming the pointer is live. If you check
origin/feature/loginand conclude nobody has pushed, you have only learned what was true at your last fetch. - Trying to commit on
origin/feature/login. Checking it out gives a detached HEAD. Create a local branch from it instead, for examplegit switch -c fix origin/feature/login. - Confusing the name with the branch.
origin/feature/loginis a ref in your repository. The branch on the server isfeature/login. Commands that take a remote branch name, such asgit push origin feature/login, use the server-side name. - Forgetting that fetches happen in the background. Some IDEs and tools run
git fetchon a timer. They move the pointer without you noticing. A later chapter shows why this matters when you overwrite remote history.
Why this matters for force pushing
A forced push replaces the server's branch with yours. Whether that is safe depends on what the server held at that moment, and your only record of that is origin/feature/login. If the record is stale, you can't judge safely. The safer force-push options compare the server against this stored pointer, so what the pointer records, and when it was last updated, is the foundation for everything that follows.
Exercise
A teammate pushed a new commit to feature/login ten minutes ago. You have not fetched since. You run git status on your feature/login branch and it says "Your branch is up to date with 'origin/feature/login'". Is the server's branch identical to yours? Why or why not, and what command would give you an accurate answer?
Show answer
Not necessarily. git status compares your local branch with the stored remote-tracking branch origin/feature/login, which only changes on fetch or push. Your teammate's commit is on the server but not yet in your stored pointer. Run git fetch origin, then git log --oneline feature/login..origin/feature/login to list the commits you are missing.
Exercise
Which two operations update origin/feature/login, and which commonly used commands do not?
Show answer
git fetch (and git pull, which runs a fetch) and a successful git push update it. git status, git log, and git branch -vv only read the stored value and never contact the server.