This Git error occurs when trying to set an upstream branch that your local repository doesn't know about. The fix is usually to run 'git fetch' first to update your local knowledge of remote branches, or use 'git push -u' to create and track the branch in one step.
This error appears when you run `git branch --set-upstream-to=origin/<branch>` but the specified remote tracking branch doesn't exist in your local repository's knowledge of the remote. Git can only set upstream tracking to branches it knows about. Your local Git maintains a cached view of remote branches (stored as references like `origin/main`, `origin/feature`, etc.). These references are updated when you run `git fetch`. If a branch exists on the remote but you haven't fetched recently, or if the branch doesn't exist on the remote at all, you'll see this error. The key insight is that `--set-upstream-to` requires the remote tracking branch to already exist locally. It doesn't reach out to the remote to check - it only looks at what Git already knows about.
The most common fix is to fetch from the remote to update your local branch references:
# Fetch from origin (most common remote name)
git fetch origin
# If using a different remote name (e.g., upstream for forked repos)
git fetch upstream
# Fetch from all remotes
git fetch --allAfter fetching, try setting the upstream again:
git branch --set-upstream-to=origin/featureNote: Running just git fetch without a remote name uses the default remote, which might not be the one you need.
Check what remote branches your local Git knows about:
# List all remote branches
git branch -r
# List all branches (local and remote)
git branch -a
# Check if a specific branch exists
git branch -r | grep featureIf the branch isn't listed, it either:
1. Doesn't exist on the remote (needs to be pushed)
2. Hasn't been fetched yet (run git fetch)
Check directly on the remote:
# See what branches exist on the remote without fetching
git ls-remote --heads originIf the branch doesn't exist on the remote yet (it's a new local branch), use git push -u to push and set up tracking simultaneously:
# Push the current branch and set upstream tracking
git push -u origin feature
# Or for the current branch (whatever it's named)
git push -u origin HEADThe -u (or --set-upstream) flag:
1. Pushes the branch to the remote
2. Creates the remote tracking branch (origin/feature)
3. Sets up your local branch to track it
After this, future git push and git pull commands will work without specifying the remote and branch.
Verify that your remote is configured correctly:
# List all remotes
git remote -v
# Check fetch configuration for origin
git config --get remote.origin.fetch
# Should return: +refs/heads/*:refs/remotes/origin/*If the fetch configuration is missing or incorrect, fix it:
# Set the correct fetch configuration
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"Check your .git/config file:
[remote "origin"]
url = https://github.com/user/repo.git
fetch = +refs/heads/*:refs/remotes/origin/*If the fetch line is missing, that's why git fetch isn't working properly.
If you did a shallow clone, you might be missing branch information:
# Check if you have a shallow clone
git rev-parse --is-shallow-repository
# Returns "true" if shallow
# Unshallow to get full history and all branches
git fetch --unshallow
# Or fetch specific branch depth
git fetch --depth=100 origin featureFor CI/CD pipelines, ensure your clone command includes branch information:
# Instead of shallow clone
git clone --depth 1 https://github.com/user/repo.git
# Use this to include branches
git clone --depth 1 --no-single-branch https://github.com/user/repo.gitDouble-check that you're using the correct names:
# List all remotes
git remote -v
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# upstream https://github.com/original/repo.git (fetch)
# List remote branches to see exact names
git branch -r
# origin/main
# origin/develop
# upstream/mainCommon mistakes:
- Using upstream when you meant origin
- Branch name case sensitivity (Feature vs feature)
- Missing remote prefix (using feature instead of origin/feature)
# Correct format
git branch --set-upstream-to=origin/feature
# NOT (missing origin/)
git branch --set-upstream-to=feature### Understanding Remote Tracking Branches
Remote tracking branches (like origin/main) are local references that track the state of branches on remote repositories. They're read-only snapshots updated by git fetch.
Local branches: Remote tracking branches: Remote branches:
main origin/main <--> main (on GitHub)
feature --> origin/feature <--> feature (on GitHub)### The Difference Between Remote Names
- origin: Default name for the primary remote (where you cloned from)
- upstream: Conventionally used for the original repo when working with forks
# Typical fork setup
git remote add upstream https://github.com/original/repo.git
git fetch upstream
git branch --set-upstream-to=upstream/main### Prune Stale Remote Tracking Branches
If a branch was deleted from the remote, your local remote tracking branch might still exist:
# Remove stale remote tracking branches
git fetch --prune origin
# Or enable auto-pruning
git config --global fetch.prune true### Setting Upstream for New Branches Automatically
Configure Git to automatically set up tracking for new branches:
# When pushing a new branch, automatically set upstream
git config --global push.autoSetupRemote trueWith this setting, git push on a new branch works without -u.
### Checking Current Upstream Configuration
# Show upstream for current branch
git rev-parse --abbrev-ref --symbolic-full-name @{u}
# Show upstream for all branches
git branch -vv
# * feature abc1234 [origin/feature] Latest commit message
# main def5678 [origin/main] Another commit### Single-Branch Clones
If you cloned with --single-branch, only one branch is tracked:
# See current fetch refspec
git config --get remote.origin.fetch
# Allow fetching all branches
git config remote.origin.fetch "+refs/heads/*:refs/remotes/origin/*"
git fetch originwarning: BOM detected in file, this may cause issues
UTF-8 Byte Order Mark (BOM) detected in file
fatal: Server does not support --shallow-exclude
Server does not support --shallow-exclude
warning: filtering out blobs larger than limit
Git partial clone filtering large blobs warning
fatal: Server does not support --shallow-since
Server does not support --shallow-since in Git
kex_exchange_identification: Connection closed by remote host
Connection closed by remote host when connecting to Git server