"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:
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:
Verify the tracking configuration
After pushing, check the branch status:
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:
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:
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:
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
masterwhen the repository actually usesmainis a very common mistake. - The remote may not be named
origin, so checkgit remote -vif 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-toalone is not enough and you still need a first push.
Related reading
- fatal unable to access 'https//gitlab-ci-tokenMASKEDgitlab.mydomain.com/xxx.git/' SSL certificate problem unable to get issuer certificate
- Fetch from origin with deleted remote branches
- fetch in git doesn't get all branches
- Filename too long in Git for Windows
- Find a commit on GitHub given the commit hash
- Find Oracle JDBC driver in Maven repository
- Find out Kafka version remotely
- Find out which remote branch a local branch is tracking
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo 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 courseInterview Questions practice on Codemia
Over 11,000 interview practice questions for 300+ companies, searchable by company and role.