Git
Branching
Upstream
Error
Version Control

"fatal: The current branch master has no upstream branch" in Git

Codemia Knowledge Hub ·

This error means your local branch exists, but Git does not know which remote branch it should track. Push the branch once with the upstream flag, and future git push and git pull commands will know where to go:

bash
git push -u origin master

What "upstream" means

An upstream branch is the remote-tracking branch associated with your local branch. On local master, Git typically expects something like origin/master as its upstream. Without that link, Git can still store commits locally, but it cannot infer where to push them.

Setting the upstream on first push

The -u flag in the command above sets origin/master as the upstream for your current local branch. After that, plain git push and git pull usually work without extra arguments.

If your branch is named main instead of master, which is common in newer repositories, use:

bash
git push -u origin main

Verify the tracking configuration

After pushing, check the branch status:

bash
git branch -vv

Git shows each local branch and, when configured, the remote branch it tracks. This is the quickest way to confirm the upstream is set correctly. Once tracking is configured, git status also becomes more informative, because Git can compare your local branch with its remote counterpart.

When the remote does not exist yet

If origin is missing, add it first:

bash
git remote add origin https://github.com/example/project.git
git push -u origin master

Without a configured remote, Git has nowhere to push, so you cannot set an upstream until the remote exists.

Set up tracking without pushing

If the remote branch already exists and you only need to connect your local branch to it, set the upstream explicitly:

bash
git branch --set-upstream-to=origin/master master

This helps when the branch was created locally from fetched remote data, or when the first push happened through another tool and your local clone never recorded the tracking relationship. It is also a good recovery command when repository tooling changes branch configuration behind the scenes.

Set the upstream automatically for new branches

If you often create a branch and push it right away, modern Git can set the upstream for you:

bash
git config --global push.autoSetupRemote true

With this setting, the first plain git push on a new branch establishes the tracking relationship in many common workflows.

Things to check if it still fails

  • Using master when the repository actually uses main is a very common mistake.
  • The remote may not be named origin, so check git remote -v if the push fails.
  • Setting the upstream only fixes tracking; it does not fix authentication or permission problems.
  • If the remote branch does not exist yet, --set-upstream-to alone is not enough and you still need a first push.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No course covers this one yet

Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.

Build my course
Interview Questions practice on Codemia

Over 11,000 interview practice questions for 300+ companies, searchable by company and role.

Browse interview questions