What the error means
You are on a local branch that doesn’t know which remote branch it should track. As a result, commands like git pull, git push, and git status can’t infer where to read from or write to, and Git prints:
fatal: no upstream configured for current branch
In Git terms, your branch is missing an upstream (tracking) branch, commonly origin/<branch>.
Quickstart: fix in under a minute
- Confirm your current branch
- Run:
git rev-parse --abbrev-ref HEAD
- Ensure a remote exists
- Run:
git remote -v - If none, add one:
git remote add origin <repo-url>
- Set upstream by pushing the current branch
- Fastest fix:
git push -u origin HEAD - This creates (if needed) and sets
origin/<current-branch>as upstream.
- Or map to an existing remote branch
- If the remote branch already exists with the same name:
git branch --set-upstream-to=origin/$(git rev-parse --abbrev-ref HEAD) - If names differ (e.g., local
featureto remotefeat/123):git branch --set-upstream-to=origin/feat/123 feature
- Verify
git rev-parse --abbrev-ref --symbolic-full-name @{u}should print something likeorigin/main.
Minimal working example
Reproduce the error and fix it locally.
#!/usr/bin/env bash
set -euo pipefail
# Start clean
rm -rf demo-upstream && mkdir demo-upstream && cd demo-upstream
git init
echo "hello" > file.txt
git add file.txt
git commit -m "init"
git switch -c feature/no-upstream
# Simulate trying to pull without upstream
set +e
git pull 2>err.txt
status=$?
set -e
echo "Exit: $status"; cat err.txt
# Expect: fatal: no upstream configured for current branch
# Fix: add a remote and set upstream when pushing
git remote add origin https://example.com/your/repo.git
git push -u origin HEAD
# Verify
git rev-parse --abbrev-ref --symbolic-full-name @{u}
# Expect: origin/feature/no-upstream
Common scenarios and exact commands
| Situation | Command |
|---|---|
| New local branch, push for the first time | git push -u origin HEAD |
| Map local to existing same-named remote branch | git branch --set-upstream-to=origin/$(git rev-parse --abbrev-ref HEAD) |
| Map local to differently named remote branch | git branch --set-upstream-to=origin/remote-name local-name |
| Change upstream to another remote (e.g., fork) | git branch --set-upstream-to=upstream/main main |
| Unset upstream | git branch --unset-upstream |
| Create branch that tracks remote on creation | git switch -c feature --track origin/feature |
Notes:
git push --set-upstream origin <branch>is equivalent togit push -u origin <branch>.- Replace
originwith your remote name if different.
Verify configuration
- Show tracked upstream of current branch:
git rev-parse --abbrev-ref --symbolic-full-name @{u}
- Show remote tracking info:
git remote show origin
- Inspect branch config directly:
git config --get branch.$(git rev-parse --abbrev-ref HEAD).remotegit config --get branch.$(git rev-parse --abbrev-ref HEAD).merge
If set correctly, you’ll see a remote (e.g., origin) and a merge ref (e.g., refs/heads/main).
Why this happens
- You created a branch locally and never pushed it with
-u. - You cloned with no default upstream for the current branch (e.g., detached HEAD checkout, or created a new branch from a tag/commit).
- Branch names differ locally and remotely (e.g.,
mastervsmain). - You added a remote after creating the branch and never linked them.
DevOps tips for teams and CI
- In CI jobs that push back to the repo, set upstream explicitly:
git push --set-upstream origin "$BRANCH"
- Standardize default branch names across repos (
mainvsmaster) to avoid mismatches. - Use protected branches and branch naming conventions; upstream mapping should follow those conventions to keep pipelines deterministic.
- Prefer
git fetch --all --prunein CI before setting upstream to sync remote refs.
Pitfalls and gotchas
- Detached HEAD: You can’t set an upstream for a detached HEAD. Create a branch first:
git switch -c my-branch. - Wrong remote: If your fork uses remotes
origin(your fork) andupstream(source repo), be precise about which remote to track. - Deprecated flags:
git branch --set-upstreamis deprecated; use--set-upstream-to. - Name mismatches: If your local is
mainbut the remote usesmaster, either rename local (git branch -m master) or map explicitly (git branch --set-upstream-to=origin/master main). - Missing remote branch:
git branch --set-upstream-to=origin/foofails iforigin/foodoesn’t exist. Create it by pushing:git push -u origin foo. - Policies blocking push: Protected branches may reject the first push even with
-u; open a PR or use an allowed branch.
Performance notes
- Setting the upstream itself is instant; the cost is from network ops.
- Prefer
git push -u origin HEADto avoid extra discovery of branch names. - If the remote has many refs, use partial or shallow operations to reduce latency:
- Clone shallow in CI:
git clone --filter=blob:none --depth=1 <url> - Fetch specific branch before mapping:
git fetch origin my-branch:my-branch
- Clone shallow in CI:
- Avoid repeated remote lookups in scripts by caching the current branch:
BR=$(git rev-parse --abbrev-ref HEAD)and reuse it.
Troubleshooting
- Error:
fatal: The upstream branch of your current branch does not match the name of your current branch:- Either rename local to match remote (
git branch -m newname) or keep the mismatch but set upstream explicitly with--set-upstream-to.
- Either rename local to match remote (
- Error:
fatal: ambiguous argument '@{u}':- No upstream is set. Fix using the quickstart steps, then rerun the command.
- Remote changed default branch:
- Update your local:
git remote set-head origin -aand remap if needed.
- Update your local:
FAQ
What is an upstream branch?
- The remote branch your local branch tracks for pull/merge and push defaults (e.g.,
origin/main).
- The remote branch your local branch tracks for pull/merge and push defaults (e.g.,
Do I have to use
origin?- No. Use any remote name (
upstream,gitlab, etc.), just be consistent.
- No. Use any remote name (
Will setting upstream rewrite history?
- No. It only updates configuration. History changes come from actual pushes, merges, or rebases.
Can I set upstream without pushing?
- Yes, if the remote branch already exists:
git branch --set-upstream-to=origin/<branch>.
- Yes, if the remote branch already exists:
How do I see all branches missing upstream?
git for-each-ref --format='%(refname:short) %(upstream:short)' refs/heads | awk '$2==""{print $1}'
With an upstream configured, git pull and git push will work without extra arguments, keeping your DevOps workflows predictable and scriptable.