Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readGit
#git#branches#remotes#workflow
Bold white headline reading NO UPSTREAM with a Git branch crate pointing a cyan arrow at an amber empty remote slot.

Introduction & Quick Summary

A fresh repository, one commit, and git push refuses:

code
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

Quick answer: run the command git prints, once per branch.

bash
git push -u origin main # -u is short for --set-upstream git branch --set-upstream-to=origin/main main # branch already exists on the remote

The 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:

ini
[branch "main"] remote = origin merge = refs/heads/main

remote 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:

bash
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 main

Related messages come from the same missing link, in other commands:

CommandMessageMissing piece
git pushThe current branch main has no upstream branchbranch.main.remote
git pullThere is no tracking information for the current branchsame
git status(no "Your branch is up to date" line)same
git push in detached HEADfatal: You are not currently on a branchnot on a branch at all
git push after a renamenothing to push, or pushes the old namemerge still names the old ref

Key Comparison Table

The commands that create a tracking relationship, and what each one requires:

CommandNeeds remote branchNeeds local branchEffect
git push -u origin mainno, creates ityespushes and sets upstream
git push -u origin HEADno, uses local nameyessame, no name to type
git branch -u origin/mainyes, must existyessets upstream only
git push --set-upstream origin local:remotenoyesdifferent names on each side
git cloneyesnosets 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.defaultPushesNotes
simple (default)current branch to its upstreamrefuses if names differ
currentcurrent branch to the same namecreates the remote branch if missing
upstreamcurrent branch to its upstreamallows different names
matchingevery branch with a local and remote pairsurprising in shared repos
nothingnothingforces explicit refspecs

Practical Edge Cases, Error Fixes & Deep Dive Code Examples

1. The remote branch already exists

bash
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:

bash
git push -u origin main:master # keep the remote name git push -u origin main # create a new remote branch called main

The 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:

bash
git config --show-origin --get push.default git config --global push.autoSetupRemote true

push.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.

bash
# 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/main

Setting 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

bash
git branch -m main trunk git push -u origin trunk git push origin --delete main

git 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:

bash
git switch -c release-2026-11-26 # attach HEAD to a branch first git push -u origin HEAD:release-2026-11-26
bash
# 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

bash
git branch --unset-upstream # makes the branch local-only again git remote remove origin # removes the remote and its tracking refs

Untracking 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:

bash
git remote add upstream https://github.com/project/repo.git git fetch upstream git branch -u origin/main # push target back to your fork

git 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 documentation

The Git documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit Git work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Git guides

More in Git

All Git guides

The guides filed next to this one in Git, in library order.