CH 41 / 75Part 6: Connecting to GitHub

Understanding Tracking Branches (upstream)

What you will learn on this page

  • How to tell apart the three: your local branch, the tracking branch and the remote branch
  • How to explain that origin/main is "a copy of GitHub's main as last seen"
  • What it means to set upstream with git push -u
  • How to read ahead and behind in git branch -vv
On this page8 items

When you pushed for the first time in 39, you added -u: git push -u origin main. In 40, git status told you “behind origin/main by 1 commit.” This page makes clear what that origin/main and -u actually are. Once you understand this, you’ll be able to read everything Git tells you when you push and pull.

There are three “main”s#

In your practice repository, there are actually three things named “main.”

Your computer has the local main you work on and a copy, origin/main (refs/remotes/origin/main). GitHub has the remote main. fetch copies the remote main into the copy, merge brings the copy into your local main, and pull does both in a row. push advances the remote main and moves the copy to the same position. -u links your local main to the copy
Tell apart local, copy and remote
NameWhere it livesWhat it isWhen it moves
mainYour computerYour local branch. This is where you workWhen you commit or merge
origin/mainYour computerTracking branch. A copy of GitHub’s mainWhen you fetch, pull or push
GitHub’s mainGitHubThe remote branchWhen someone pushes

The one in the middle, origin/main, is a copy recording where GitHub’s main was the last time you communicated with GitHub. Because it’s local, you can look at it even without an internet connection. But it isn’t necessarily GitHub’s current state. If someone has pushed, the copy is out of date. Updating the copy is what fetch in 40 does.

What the copy really is#

In Part 3, #19, you saw that a branch is a file in .git/refs/heads/. Tracking branches live right next door, in .git/refs/remotes/.

text2 lines
cd ~/git-practice
ls .git/refs/remotes/origin
text3 lines
HEAD
main
shopping-list

Under refs/remotes/origin/, the names of the branches on GitHub are listed. Like local branches, their contents are commit IDs. The fetch = +refs/heads/*:refs/remotes/origin/* you saw in 36 was the rule “copy GitHub’s refs/heads/ to refs/remotes/origin/ locally.”

upstream: linking a local branch to its copy#

Which tracking branch is your local main paired with? This pairing is called upstream. The -u in git push -u was short for --set-upstream.

text1 lines
git branch -vv
text1 lines
* main          0293f47 [origin/main] Merge branch 'main' of https://github.com/<username>/git-practice

With -vv, each branch’s upstream is shown inside [ ]. main’s upstream is origin/main. This pairing is written in the repository’s settings (.git/config).

text1 lines
git config --get-regexp '^branch\.main'
text2 lines
branch.main.remote origin
branch.main.merge refs/heads/main

This means “main is paired with origin’s refs/heads/main.” Once upstream is set, you get two conveniences:

  • You can use git push and git pull without writing the destination or branch name
  • git status tells you whether you’re “ahead or behind” compared with the copy

When there’s no upstream#

Create a new branch and try pushing without -u.

text4 lines
git switch -c shopping-list
echo "Buy tissues" >> shopping.txt
git commit -am "Add tissues to shopping memo"
git push
text4 lines
fatal: The current branch shopping-list has no upstream branch.
To push the current branch and set the remote as upstream, use

    git push --set-upstream origin shopping-list

It stopped you, saying “shopping-list has no upstream.” That’s because Git doesn’t know where to send it. Add -u as suggested.

text1 lines
git push -u origin shopping-list
text7 lines
remote:
remote: Create a pull request for 'shopping-list' on GitHub by visiting:
remote:      https://github.com/<username>/git-practice/pull/new/shopping-list
remote:
To https://github.com/<username>/git-practice.git
 * [new branch]      shopping-list -> shopping-list
branch 'shopping-list' set up to track 'origin/shopping-list'.

Lines starting with remote: are messages from GitHub. It’s telling you “go here if you want to create a pull request” (pull requests are covered in Part 7, #45). The last line shows that shopping-list’s upstream has been set to origin/shopping-list.

Reading ahead and behind#

With an upstream, the difference between your local branch and the copy is shown as a number.

text2 lines
git switch main
git branch -vv
text2 lines
* main          db07100 [origin/main: ahead 1, behind 1] Add handkerchief to shopping memo
  shopping-list f818c73 [origin/shopping-list] Add face masks to shopping memo

This is the display after fetching, in a situation where you committed “handkerchief” on your original computer while, in the meantime, “shoe polish” was pushed to GitHub from your laptop.

DisplayMeaningWhat to do next
[origin/main]Same as the copyNothing
ahead 1Your local branch is 1 commit aheadPush
behind 1Your local branch is 1 commit behindPull
ahead 1, behind 1Each side has commits the other doesn’t (diverged)Pull to combine them, then push

git status shows the same thing in sentences.

text2 lines
Your branch and 'origin/main' have diverged,
and have 1 and 1 different commits each, respectively.

What to watch out for here is that these numbers are compared against the copy (origin/main). If you haven’t fetched, the copy is still old, so even if GitHub has new commits, they won’t show up as behind. When you “want to compare against the latest state,” run git fetch first.

FAQ#

Q. A branch I deleted on GitHub still shows up in my local git branch -a.

A. Tracking branches aren’t deleted automatically. Running git fetch --prune (-p for short) cleans up copies of branches that no longer exist on GitHub.

Q. Can I change the upstream later?

A. git branch -u origin/branch-name resets the upstream of the branch you’re currently on.

Summary#

  • main exists in three forms: your local branch, the tracking branch (origin/main) and the remote branch
  • origin/main is a copy of GitHub’s main as of the last time you communicated. It lives in .git/refs/remotes/
  • upstream is the pairing of a local branch with a tracking branch. Set it with git push -u
  • With an upstream, you can write git push and git pull briefly, and git status tells you the difference
  • ahead means your local branch is ahead; behind means it’s behind. The numbers are compared against the copy, so fetch first to compare against the latest

Next page#

42. Putting an Existing Project on GitHub — To wrap up Part 6, walk through the whole process of publishing a folder that never used Git to GitHub from scratch.