/errors/git/current-branch-has-no-upstream-branch
fatal: The current branch has no upstream branch
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash) · git version 2.55.0.windows.5updated
- tool
- Git
- exit code
- 128
- versions
- Git 2.x, push.autoSetupRemote: Git 2.37+
- os
- linux, macos, windows
fatal: The current branch X has no upstream branch. means git push without arguments does not know where to push, because the local branch X has no upstream (remote-tracking) branch yet. That is normal for a branch you just created. Push it once with git push -u origin X; -u stores the upstream, so later git push and git pull work without arguments.
Reproduce it
git init -q --bare origin.git
git clone -q origin.git work 2>/dev/null && cd work
git switch -q -c main && echo a > a && git add a && git commit -qm init && git push -q -u origin main
git switch -q -c feature && echo b > b && git add b && git commit -qm feature
git pushOutput · exit 128
fatal: The current branch feature has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin feature
To have this happen automatically for branches without a tracking
upstream, see 'push.autoSetupRemote' in 'git help config'.Git's message already contains the fix and mentions the setting that automates it.
Fix: push with -u once
git init -q --bare origin.git
git clone -q origin.git work 2>/dev/null && cd work
git switch -q -c feature && echo b > b && git add b && git commit -qm feature
git push -q -u origin feature
git status -sb | head -n 1
git rev-parse --abbrev-ref feature@{upstream}Output
## feature...origin/feature origin/feature
Make it automatic
git init -q --bare origin.git
git clone -q origin.git work 2>/dev/null && cd work
git config push.autoSetupRemote true
git switch -q -c feature && echo b > b && git add b && git commit -qm feature
git push -q
git rev-parse --abbrev-ref feature@{upstream}Output
origin/feature
Set it for all repositories with git config --global push.autoSetupRemote true (Git 2.37+). With it, the first plain git push of a new branch behaves like git push -u origin BRANCH.
| Command | Pushes | Sets upstream |
|---|---|---|
| git push -u origin feature | feature to origin/feature | yes |
| git push origin HEAD | current branch to the same name | no |
| git push origin feature | feature to origin/feature | no |
| git push (with push.autoSetupRemote) | current branch to the same name | yes, on the first push |
Sources
Related pages
errors · giterror: failed to push some refs (rejected, fetch first)git push is rejected with failed to push some refs when the remote branch has commits you do not have. Pull or fetch and rebase first, then push again safely.errors · giterror: src refspec main does not match anygit push fails with src refspec main does not match any when no local branch has that name: no commit yet, or the branch is master. Commit first or rename.flags · gitgit push --force-with-lease: a safer force push--force-with-lease overwrites the remote branch only if it still points where your remote-tracking ref says, so commits pushed since your fetch are never lost.