Sub topic 9
What git push -u sets up
This chapter assumes you have a local repository with at least one commit, an empty GitHub repository added as origin, and working authentication. The previous chapter covered pushing your first branch. Here you will look at what the -u flag changed.
What the flag does
The command from the first push was:
git push -u origin mainThe -u flag is short for --set-upstream. It does two things after the push succeeds:
- It creates or updates the remote-tracking branch
origin/main. This is a local read-only bookmark that records wheremainwas on GitHub at your last contact. - It links your local
maintoorigin/main. That link is called the upstream ofmain.
The link is stored in .git/config. You can see it directly:
git config --get-regexp '^branch\.main\.'The output contains two entries:
branch.main.remote origin
branch.main.merge refs/heads/mainThe first entry names the remote. The second names the branch on that remote. Git reads both whenever you run a command on main without arguments.
What changes in daily use
With an upstream set, these commands need no arguments:
git push
git pull
git statusgit push sends main to origin/main. git pull fetches from origin and merges origin/main into main. git status compares your branch with its upstream and reports the difference:
Your branch is ahead of 'origin/main' by 2 commits.It can also say you are behind, or that the branches have diverged. Without an upstream, git status has nothing to compare against and says nothing about the remote.
Inspecting tracking information
Use git branch -vv to list every local branch with its upstream and latest commit:
git branch -vvA tracked branch shows its upstream in square brackets, for example [origin/main]. An untracked branch shows no brackets. To print just the upstream of the current branch, run:
git rev-parse --abbrev-ref --symbolic-full-name @{u}This prints origin/main. If no upstream exists, the command fails with an error.
Setting or changing an upstream later
You can set the link without pushing:
git branch --set-upstream-to=origin/main mainYou can also remove it:
git branch --unset-upstream mainEvery new branch needs its own upstream. Create a branch and push it the first time with -u:
git switch -c feature-login
git push -u origin feature-loginCommon errors
- "The current branch has no upstream branch." You ran
git pushon a branch that was never pushed with-u. Git prints the exact command to fix it. Rungit push -u origin BRANCH-NAME. - "There is no tracking information for the current branch." You ran
git pullon a branch with no upstream. Set one withgit branch --set-upstream-to=origin/BRANCH-NAME. - Pushing to a different branch name. By default (
push.defaultset tosimple),git pushrefuses to push when the upstream branch has a different name than the local branch. Push with an explicit refspec such asgit push origin local-name:remote-name.
If you do not want to type -u for every new branch, Git 2.37 and later can do it automatically:
git config --global push.autoSetupRemote trueWith this setting, the first plain git push on a new branch creates the remote branch and sets the upstream.
Exercise
You created a branch named fix-typo and ran git push with no arguments. Git refused because the branch has no upstream. Which single command pushes the branch and fixes the problem for later pushes?
Show answer
git push -u origin fix-typo. It pushes the branch, creates origin/fix-typo, and records origin and refs/heads/fix-typo as the upstream in .git/config, so later git push and git pull need no arguments.