How to Resolve "no upstream branch" Error in Git
Sometimes when attempting to git push
you can run into this error:
fatal: The current branch main has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin main
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.
What does "The current branch main has no upstream branch" Mean?
When you are working on a new branch, the remote repository doesn't know about your branch until you push that branch to it. This is why you get the error: The current branch main has no upstream branch.
if you try to run git push
on a branch that isn't in your remote repository yet.
How to Push Directly to an Upstream Branch
You can push a local branch directly to an upstream branch by running the git 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 who don't like remembering long flags like --set-upstream
. You can instead call:
git push -u origin main
That will push your branch to your origin repository so you or your teammates could access it later or from other devices.
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 you push them to your upstream branch.