How to Set the Upstream Branch in Git
The upstream of a branch is the remote branch that the branch tracks. Once an upstream is set, you can run git push and git pull without arguments, and git status shows how the branch differs from the remote. Set it when git push stops with "has no upstream branch" on a new branch, or when git pull says "There is no tracking information". I checked how to set and remove an upstream by comparing the output before and after each step.
The test environment was Ubuntu 24.04 with Git 2.43.0. I used a sample name and email address (Neko <neko@example.com>) for the author. For the remote, I used a bare repository created in the same environment (../remote.git). I did not test pushing to hosting services such as GitHub, and I did not test on Windows or macOS.
- 1. What happens with a branch that has no upstream
- 2. Use git push -u to set the upstream while pushing
- 3. Use git branch -u to set only the upstream of a branch that is already pushed
- 4. With an upstream, ahead and behind counts are shown
- 5. A branch created from a remote branch gets an upstream automatically
- 6. Use push.autoSetupRemote to skip -u every time
- 7. Use git branch –unset-upstream to remove an upstream
What happens with a branch that has no upstream
I ran git init, made one commit, and added a remote with git remote add origin ../remote.git. At this point, main has no upstream. git status shows nothing about the remote, and git branch -vv shows no brackets.
$ git status
On branch main
nothing to commit, working tree clean
$ git branch -vv
* main 6b9961f 最初のコミット
A git push without arguments stops and says there is no upstream. The message includes a command that pushes and sets the upstream at the same time.
$ git push
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
A git pull without arguments also stops, because Git does not know where to fetch from.
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> main
Use git push -u to set the upstream while pushing
If you are pushing a branch for the first time, git push -u origin main is the simplest option. -u is the short form of --set-upstream. It sets the upstream of the branch after the push succeeds (git push reference). The last line of the output shows that the branch now tracks origin/main.
$ git push -u origin main
To ../remote.git
* [new branch] main -> main
branch 'main' set up to track 'origin/main'.
After this, git branch -vv shows the upstream name in brackets, and git status shows the relationship with the remote.
$ git branch -vv
* main 6b9961f [origin/main] 最初のコミット
$ git status
On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean
The upstream is stored in the repository's config file as two values, branch.<branch name>.remote and branch.<branch name>.merge. These are the remote name and the branch name on the remote (git config reference).
$ git config --get-regexp '^branch\.'
branch.main.remote origin
branch.main.merge refs/heads/main
Use git branch -u to set only the upstream of a branch that is already pushed
If you push without -u, the branch is created on the remote, but no upstream is set. I created feature/login and pushed it with git push origin feature/login. In git branch -vv, feature/login had no brackets, and git pull stopped with the same message as before.
$ git push origin feature/login
To ../remote.git
* [new branch] feature/login -> feature/login
$ git branch -vv
* feature/login 8729925 ログイン画面を追加
main 6b9961f [origin/main] 最初のコミット
$ git pull
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.
git pull <remote> <branch>
If you wish to set tracking information for this branch you can do so with:
git branch --set-upstream-to=origin/<branch> feature/login
If the branch is already pushed, you can set only the upstream with git branch -u, without pushing again. -u is the short form of --set-upstream-to. If you omit the branch name, the current branch is used (git branch reference).
$ git branch -u origin/feature/login
branch 'feature/login' set up to track 'origin/feature/login'.
$ git branch -vv
* feature/login 8729925 [origin/feature/login] ログイン画面を追加
main 6b9961f [origin/main] 最初のコミット
$ git config --get-regexp '^branch\.'
branch.main.remote origin
branch.main.merge refs/heads/main
branch.feature/login.remote origin
branch.feature/login.merge refs/heads/feature/login
To make a remote branch the upstream, specify the remote-tracking branch (origin/...) that exists locally. If you specify a name that has no local remote-tracking branch, Git stops and says the branch does not exist. The hint suggests running git fetch if the branch exists on the remote, and using git push -u if you are creating a new branch.
$ git branch -u origin/feature/signup
fatal: the requested upstream branch 'origin/feature/signup' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
hint: Disable this message with "git config advice.setUpstreamFailure false"
With an upstream, ahead and behind counts are shown
For a branch with an upstream, Git shows how many commits the branch is ahead of or behind the upstream. I added one commit to feature/login. Then git status said the branch was one commit ahead, and git branch -vv showed ahead 1.
$ git status
On branch feature/login
Your branch is ahead of 'origin/feature/login' by 1 commit.
(use "git push" to publish your local commits)
nothing to commit, working tree clean
$ git branch -vv
* feature/login 7bcf752 [origin/feature/login: ahead 1] パスワード欄を追加
main 6b9961f [origin/main] 最初のコミット
In a command, you can refer to the upstream as @{u} (short for @{upstream}) (revision syntax reference). git log --oneline @{u}.. lists only the commits that have not been pushed yet.
$ git rev-parse --abbrev-ref @{u}
origin/feature/login
$ git log --oneline @{u}..
7bcf752 パスワード欄を追加
Because the upstream is set, a git push without arguments pushes the commit, and ahead 1 disappeared.
$ git push
To ../remote.git
8729925..7bcf752 feature/login -> feature/login
$ git branch -vv
* feature/login 7bcf752 [origin/feature/login] パスワード欄を追加
main 6b9961f [origin/main] 最初のコミット
I also tested the behind case. After I pushed one commit to feature/login from another clone, git status in the original repository still said "up to date". git status compares against the local origin/feature/login, so the branch only showed as behind after I ran git fetch. After the fetch, it reported that the branch was one commit behind.
$ git status
On branch feature/login
Your branch is up to date with 'origin/feature/login'.
nothing to commit, working tree clean
$ git fetch -q
$ git status
On branch feature/login
Your branch is behind 'origin/feature/login' by 1 commit, and can be fast-forwarded.
(use "git pull" to update your local branch)
nothing to commit, working tree clean
$ git branch -vv
* feature/login 7bcf752 [origin/feature/login: behind 1] パスワード欄を追加
main 6b9961f [origin/main] 最初のコミット
A branch created from a remote branch gets an upstream automatically
When you bring a branch from the remote into your local repository, the upstream is set without any extra steps. In a separate clone, I ran git switch feature/login for a branch that did not exist locally yet. Git created a branch with origin/feature/login as its upstream.
$ git branch -vv
* main 6b9961f [origin/main] 最初のコミット
$ git switch feature/login
Switched to a new branch 'feature/login'
branch 'feature/login' set up to track 'origin/feature/login'.
According to the git switch reference, when the named branch does not exist locally and exactly one remote has a remote-tracking branch with that name, the command behaves like git switch -c <branch name> --track <remote>/<branch name>.
I also created a branch with a different name, using git switch -c login-copy origin/feature/login. It also got an upstream. This is because the default value of branch.autoSetupMerge is true, and with that setting Git sets an upstream for a branch created from a remote-tracking branch (git branch reference). The branch local-only, created from a local branch, did not get an upstream.
$ git switch -c login-copy origin/feature/login
Switched to a new branch 'login-copy'
branch 'login-copy' set up to track 'origin/feature/login'.
$ git switch -c local-only
Switched to a new branch 'local-only'
$ git branch -vv
feature/login 7bcf752 [origin/feature/login] パスワード欄を追加
* local-only 7bcf752 パスワード欄を追加
login-copy 7bcf752 [origin/feature/login] パスワード欄を追加
main 6b9961f [origin/main] 最初のコミット
Use push.autoSetupRemote to skip -u every time
If typing -u origin <branch name> for every new branch is tedious, set push.autoSetupRemote to true. With this setting, a git push without arguments on a branch with no upstream behaves as if --set-upstream were given. This setting works when push.default is simple, upstream, or current (git config reference). In this test, I did not set push.default, so it stayed at simple, the default since Git 2.0.
I ran git push before and after setting this on the new branch feature/cart. Before the setting, it stopped and said there was no upstream. After the setting, it created the branch on the remote and also set the upstream.
$ git push
fatal: The current branch feature/cart has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature/cart
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.
$ git config --global push.autoSetupRemote true
$ git push
To ../remote.git
* [new branch] feature/cart -> feature/cart
branch 'feature/cart' set up to track 'origin/feature/cart'.
With --global, the setting is written to ~/.gitconfig in your home folder, not to the repository's .git/config. Without the option, it is written to the .git/config of the current repository (git config reference).
Use git branch –unset-upstream to remove an upstream
You can remove an upstream with git branch --unset-upstream. If you omit the branch name, the current branch is used (git branch reference). When I ran it on feature/cart, the brackets disappeared from git branch -vv, and the branch.feature/cart values were removed from the config file. git status no longer showed any relationship with the remote. The behind 1 on feature/login remains because I have not yet merged the change that I fetched in the previous section.
$ git branch --unset-upstream
$ git branch -vv
* feature/cart 7915430 カートを追加
feature/login 7bcf752 [origin/feature/login: behind 1] パスワード欄を追加
main 6b9961f [origin/main] 最初のコミット
$ git config --get-regexp '^branch\.feature/cart'
$ git status
On branch feature/cart
nothing to commit, working tree clean
Removing the upstream only changes the local setting. git branch -r still lists the remote-tracking branch origin/feature/cart, and I was able to set it as the upstream again with git branch -u.
$ git branch -r
origin/feature/cart
origin/feature/login
origin/main
$ git branch -u origin/feature/cart
branch 'feature/cart' set up to track 'origin/feature/cart'.
For a branch you are pushing for the first time, set the upstream with git push -u origin <branch name>. For a branch that is already pushed, use git branch -u origin/<branch name>. To check whether the upstream was set, look for the brackets in the output of git branch -vv.




