What does "The current branch master has no upstream branch" Mean?
Sometimes when attempting to git push
you can run into this error:
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
What is an Upstream Branch?
An upstream branch is a remote branch that is hosted in a remote repository, such as GitHub or Bitbucket.
When you try to checkout a branch, you pull this remote branch from your repository.
When you are working on a new branch, the remote repository doesn't know about it yet. This is why you get the error The current branch master has no upstream branch.
if you try to run git push
on a branch that isn't in your remote repository yet.
How to Resolve This Error
To fix this error we need to add our branch to our repository. You can do this by using the command:
git push --set-upstream origin <branch>
If you are working on a branch called develop, this command would be:
git push --set-upstream origin develop
There is also a shorthand version for those of us who don't like remembering long flags like --set-upstream
. You can instead call:
git push -u origin master
That will push your branch to your origin repository so that you as well as other teammates can access it at any time.
If your computer were to explode and you hadn't pushed your branch to the repository, your work would be gone forever.
So don't forget to commit code often, but even then, those commits aren't safe until your push them to your upstream branch.