Git: The current branch has no upstream branch
The branch exists locally with no tracking configured. Set upstream once with push -u, or let push.autoSetupRemote do it on every new branch.

Introduction & Quick Summary
A fresh repository, one commit, and git push refuses:
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 mainQuick answer: run the command git prints, once per branch.
git push -u origin main # -u is short for --set-upstream
git branch --set-upstream-to=origin/main main # branch already exists on the remoteThe branch exists locally and its commits are complete. What is missing is a tracking relationship: a pair of config entries saying which remote and which remote ref this local branch should be compared against. A clone writes those entries for you, which is why the error almost always appears on a repository you initialised yourself, on a branch you created with git switch -c, or in a CI job that built its history from scratch.
Core Mechanics, Signatures & Reproducible Setup
Upstream tracking is two lines in .git/config:
[branch "main"]
remote = origin
merge = refs/heads/mainremote names which remote to push to and fetch from, and merge names the ref that git status, git pull and git push compare against. Without them, git has a local branch and no idea where it belongs. Reproduce it exactly:
mkdir up && cd up && git init -b main
echo hi > a.txt && git add . && git commit -m "init"
git remote add origin git@github.com:me/up.git
git push
# fatal: The current branch main has no upstream branch.
git push -u origin mainRelated messages come from the same missing link, in other commands:
| Command | Message | Missing piece |
|---|---|---|
git push | The current branch main has no upstream branch | branch.main.remote |
git pull | There is no tracking information for the current branch | same |
git status | (no "Your branch is up to date" line) | same |
git push in detached HEAD | fatal: You are not currently on a branch | not on a branch at all |
git push after a rename | nothing to push, or pushes the old name | merge still names the old ref |
Key Comparison Table
The commands that create a tracking relationship, and what each one requires:
| Command | Needs remote branch | Needs local branch | Effect |
|---|---|---|---|
git push -u origin main | no, creates it | yes | pushes and sets upstream |
git push -u origin HEAD | no, uses local name | yes | same, no name to type |
git branch -u origin/main | yes, must exist | yes | sets upstream only |
git push --set-upstream origin local:remote | no | yes | different names on each side |
git clone | yes | no | sets upstream automatically |
push.default decides what a plain git push means once upstream exists, and it is the reason two developers on the same repository see different behaviour:
push.default | Pushes | Notes |
|---|---|---|
simple (default) | current branch to its upstream | refuses if names differ |
current | current branch to the same name | creates the remote branch if missing |
upstream | current branch to its upstream | allows different names |
matching | every branch with a local and remote pair | surprising in shared repos |
nothing | nothing | forces explicit refspecs |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. The remote branch already exists
git fetch origin
git branch --set-upstream-to=origin/main main
git status -sb # ## main...origin/main-sb is the fastest confirmation: the ...origin/main suffix appears only when tracking is configured. Without a prior git fetch, the remote ref may not exist locally and the command fails with "the requested upstream branch 'origin/main' does not exist".
2. Pushing a branch whose name differs on the remote
Local main, remote still on master, is the classic mismatch. Two honest options:
git push -u origin main:master # keep the remote name
git push -u origin main # create a new remote branch called mainThe first sets merge = refs/heads/master so later plain pushes follow the old branch. The second leaves master untouched and is usually what a repository owner wants — but it also means two default branches exist until the hosting platform's setting is changed.
3. push.default and remote.pushDefault
A machine configured with push.default = current never shows this error, because the very first git push creates the remote branch. The same repository on a colleague's machine fails, and neither of you changed anything about the project:
git config --show-origin --get push.default
git config --global push.autoSetupRemote truepush.autoSetupRemote (Git 2.37+) makes the first push set upstream automatically, which removes the error permanently for people who create many branches. It is the single most useful fix for teams that branch constantly.
# what the config looks like after a plain push with autoSetupRemote
$ git config --get-regexp '^branch\.'
branch.main.remote origin
branch.main.merge refs/heads/mainSetting it per repository (git config push.autoSetupRemote true) rather than globally is the cautious version: it changes behaviour for everyone who works in that clone without touching their other projects.
4. Renaming a branch after it was pushed
git branch -m main trunk
git push -u origin trunk
git push origin --delete maingit branch -m carries the branch.<name> section to the new name, so the local config may still point at refs/heads/main. The explicit -u above rewrites it. Check with git config --get-regexp '^branch\.' when status output looks wrong after a rename.
5. CI, scripts and detached HEAD
A pipeline that checks out a commit rather than a branch has no upstream, and git push fails even with credentials configured:
git switch -c release-2026-11-26 # attach HEAD to a branch first
git push -u origin HEAD:release-2026-11-26# in the workflow, after actions/checkout with fetch-depth: 0
branch=$(git rev-parse --abbrev-ref HEAD)
git config user.email "ci@example.com"
git config user.name "ci"
git add -A && git commit -m "chore: regenerate sitemap" || exit 0
git push origin "HEAD:${branch}"If a commit was made on a detached HEAD, it is not lost — create the branch at the current commit (git switch -c fix/x) and push. Scripts should prefer git push origin HEAD:<remote-branch> with an explicit refspec, because it works regardless of the local branch name and never depends on user config.
6. Removing tracking on purpose
git branch --unset-upstream # makes the branch local-only again
git remote remove origin # removes the remote and its tracking refsUntracking is useful before pushing the same history to a second remote. The configuration is per branch and stays in .git/config, so a repository copied with cp -r keeps the old remote names and produces "no such remote" errors instead of this one.
Fork workflows deserve their own line. On a fork, origin is your copy and upstream is the project you forked, and the first push after syncing routinely fails because the local branch tracked upstream/main — a remote you may not have permission to write to:
git remote add upstream https://github.com/project/repo.git
git fetch upstream
git branch -u origin/main # push target back to your forkgit push with tracking on upstream/main produces "remote: Permission to project/repo.git denied", which is a different error but the same root cause. Two clones of the same project are easiest to compare with the text diff tool once the config lines are extracted, and branch names that follow a convention are quicker to filter from git branch -vv with the regex tester.
Common Compilation/Runtime Mistakes & Version Gotchas
Putting -u in the wrong place. git push origin -u main is parsed differently from git push -u origin main and may push the wrong refspec. Keep the flag first.
Assuming the hint is optional. The printed command is generated from the current branch and remote, so it is correct in almost every case. Run it once rather than guessing the flags.
Confusing this with a rejected push. "Updates were rejected because the tip of your current branch is behind" means upstream is configured; the fix there is git pull --rebase, not -u.
Setting upstream to a remote-tracking ref that will be deleted. Pointing at a short-lived branch such as origin/fix/temp leaves the config dangling after the branch is deleted; git fetch --prune then makes git status report "gone".
Version notes. push.autoSetupRemote arrived in Git 2.37. git switch (2.23) creates branches without tracking by design, while the older git checkout -b also did not track unless branch.autoSetupMerge allowed it — --track was always required when starting from a remote branch. git config --show-origin (2.26) is the quickest way to find which file set push.default.
Reference material: git push and git branch.
Official references
primary documentationThe Git documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related Git guides
All Git guidesGit: fatal: not a git repository (or any parent): .git
The command ran outside a working tree or the .git metadata is gone. Confirm the root, restore a deleted .git, and repair worktree pointers.
Git: fatal: Need to specify how to reconcile divergent branches
Git 2.34 and later refuse to guess between merge, rebase and fast-forward. Set pull.rebase or pull.ff once, and know what each strategy does to history.
Git: fatal: unable to auto-detect email address
A commit needs an author identity. Containers and fresh installs have no user.email, so Git guesses from the OS user and refuses to continue.
Git: could not read Username, terminal prompts disabled
A headless git has no terminal to ask for credentials. Supply a token through a helper, a header or SSH instead of relying on a prompt.
More in Git
All Git guidesThe guides filed next to this one in Git, in library order.