Git & GitHub Course
Git & GitHub
/
Intermediate

Upstream Branches

Definition

A remote branch that your local branch is tracking. It allows Git to know which specific remote branch to push to or pull from when you use short commands.

Explain Like I'm New

It's a permanent tether between your laptop and GitHub. If your local `dev` branch is 'tracking' the upstream `origin/dev` branch, Git constantly monitors the tether. It will tell you 'You are 2 commits behind the server' when you type `git status`.

Real World Example

You type `git pull` without specifying a branch name. Git looks at the upstream tether, realizes you are on `dev`, and automatically pulls from `origin/dev`.

Common Use Cases

  • •Streamlining daily workflow commands

Terminal Output

bash / terminal
# Create a new branch $ git checkout -b feature-xyz # Try to push it simply (THIS WILL FAIL!) $ git push # fatal: The current branch feature-xyz has no upstream branch. # Do it correctly by setting the upstream link (-u) $ git push -u origin feature-xyz # Now the tether is established! Tomorrow, you can just type: $ git push

Interview Questions

basic

  • How do you see which upstream branch your local branch is tracking?

intermediate

  • What error do you get if you try to `git push` a brand new branch without an upstream?

Flash Cards

Question

How to see it?

Click to reveal answer
Answer

Run `git branch -vv`. It lists all branches and the upstream remote they are tethered to.

Question

What error?

Click to reveal answer
Answer

fatal: The current branch has no upstream branch. To push the current branch and set the remote as upstream, use: `git push --set-upstream origin branch-name`.