Upstream tracking branches

Intermediate · 16 min

How a local branch is linked to a remote branch, what git push -u really writes, and how to read ahead, behind and gone in git status.

Before thisFetch, pull and pushCreating and switching branches

Cameron McKenzie · 16 min

Upstream tracking branches

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Cameron McKenzieReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • An upstream is a pair of config entries, `branch.<name>.remote` and `branch.<name>.merge`, that tell a branch which remote branch it follows.
  • `git push -u origin <branch>` pushes and writes that upstream, so a later bare `git push` or `git pull` knows where to go.
  • `git branch -vv` and `git status -sb` show ahead and behind counts against the upstream, and `gone` means the remote branch was deleted.
  • `git fetch --prune` deletes remote-tracking branches whose remote branch no longer exists, which is how `gone` branches get cleaned up.
  • `push.autoSetupRemote true` sets the upstream automatically on the first push of a new branch.

The question a bare git push has to answer

When you type git push with no arguments, Git must decide two things: which remote, and which branch on it. You never told it. Something recorded the answer earlier, and that something is the upstream.

Two names are easy to mix up:

  • A remote-tracking branch such as origin/main is a read-only local bookmark of where the remote’s branch was at your last fetch.
  • An upstream is the relationship from a local branch to one of those remote branches: “search-command follows origin/search-command”.

No upstream, no default

I made a branch in the jot repository and pushed it without saying where:

$ git switch -c search-command
$ git push
fatal: The current branch search-command has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin search-command

Git prints the fix. -u is the short form of --set-upstream:

$ git push -u origin search-command
To ../hub.git
 * [new branch]      search-command -> search-command
branch 'search-command' set up to track 'origin/search-command'.
$ git branch -vv
  main           c1a3b1d [origin/main] Expand README
* search-command 1a31a80 [origin/search-command] Start search command

(All hashes in this lesson will differ on your machine.) The bracket after the hash is the upstream. And the entire relationship is two lines of config:

$ git config --get-regexp '^branch\.search'
branch.search-command.remote origin
branch.search-command.merge refs/heads/search-command

Nothing more. remote says which remote, merge says which branch on it. git pull uses this pair as its source, and git push uses it as its destination.

You can ask Git for the upstream by name, which is handy in scripts:

$ git rev-parse --abbrev-ref @{upstream}
origin/search-command

Setting it once, for every new branch

Typing -u on the first push of each branch gets old. One setting removes it:

$ git config push.autoSetupRemote true
$ git switch -c export
$ git commit --allow-empty -m "Start export"
$ git push
To ../hub.git
 * [new branch]      export -> export
branch 'export' set up to track 'origin/export'.

The bare git push worked on a brand new branch. Add --global to make it a default everywhere.

To point an existing branch at a different upstream, use --set-upstream-to. Here a branch with no upstream follows origin/main and the counts appear:

$ git branch --set-upstream-to=origin/main
branch 'wip' set up to track 'origin/main'.
$ git status -sb
## wip...origin/main [ahead 3]

ahead 3 means three of my commits aren’t on origin/main. behind is the reverse. Both appear when histories diverged, as in fetch, pull and push. These numbers only update when you fetch.

When the remote branch disappears

Someone merges a pull request and deletes the branch. Your clone doesn’t hear about it until you fetch, and even then, a plain fetch leaves the stale bookmark alone:

$ git fetch
$ git branch -r
  origin/HEAD -> origin/main
  origin/export
  origin/main
$ git fetch --prune
From ../hub
 - [deleted]         (none)     -> origin/export
$ git branch -vv | grep export
  export 8cc7da9 [origin/export: gone] Start export

(On the hub I had deleted export through a different clone. In the first listing origin/export was still there after a plain fetch.) --prune removes dead bookmarks, and your local branch now shows gone. The local branch and its commits are untouched. Delete it when you’ve confirmed it was merged. git config --global fetch.prune true makes pruning automatic.

One more observation from the run: git push origin --delete search-command removes the bookmark immediately, because you did the deleting. Only deletions made by other people need --prune.

What the video covers

Cameron McKenzie’s “Git Set Upstream Example | Learn Branch Management in Git” is about sixteen minutes and was published around 2024. From its title it demonstrates setting an upstream, the first half of this lesson. It is long for the topic. I judge English from the title, description and channel only, and I could not read a transcript. I can’t tell whether it covers gone branches or fetch --prune, which come from my own runs. Two explanations of remote-tracking branches are linked.

Where this fits

You know how a branch finds its remote. Next, what to do when you have rewritten history and a plain push is refused: force pushing safely.

Check yourself

  1. 01A new branch has no upstream and you run `git push`. What happens, and how do you fix it?

    Git refuses and prints the exact command, `git push --set-upstream origin <branch>`. Running it pushes and records the upstream.

  2. 02What do `[ahead 2, behind 1]` mean in `git status -sb`?

    Your branch has two commits the upstream lacks and the upstream has one commit you lack, as of your last fetch.

  3. 03Where does Git store a branch's upstream?

    In `.git/config`, under `[branch "<name>"]`, as `remote` and `merge` entries.

  4. 04`git branch -vv` shows `[origin/old-feature: gone]`. What happened and what do you do?

    The remote branch was deleted. Your local branch is untouched. After checking you don't need it, delete it locally.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark