If you see the message “the current branch has no upstream” in your command window, it means you’ve tried to git push
to a remote repository from a local branch that has not yet been connected to a remote branch. In other words there is no upstream branch. That means git doesn’t know where to push your changes to. In this post I’ll explain how to set the upstream branch to your local branch.
‘No upstream branch’ error
If you get an error when you git push, then it could well be because your current branch has not yet been ‘linked’ to a remote branch. The error you see will look like this:
$ git push
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
How to set the upstream branch in git
Make sure you are already on the correct local branch, by using git status if you need to check which branch you are already on. Read one of my other posts if you need to use git switch to change branches. Then to set the upstream branch for that local branch, use these flags along with your git push command :
git push --set-upstream origin master
This would set the upstream branch of the current branch to ‘master’. I would always recommend keeping the name of your remote branch exactly the same as your local branch. So if your current local branch was called my-feature-branch then you would type this instead:
git push --set-upstream origin my-feature-branch
What is git push -u ?
Often in git there are several ways of writing each flag. In this scenario:
--set-upstream
is the same as
-u.
So these two commands will do exactly the same thing:
git push --set-upstream origin my-feature-branch
git push -u origin my-feature-branch
For any other information about flags to use with the git push command, read the official git push documentation. Once you’ve set the upstream branch, other commands will automatically use it too without you having to specify the remote branch name. For instance:
git pull
If your remote is not called the default name ‘ORIGIN’
What if your remote is not called the default name ‘ORIGIN’? Let’s say it’s called MY-REMOTE, and the branch is called master, then you would type the following command :
git push --set-upstream MY-REMOTE master
You can read more about what Git Origin is here.
Summary
You will now know what to do if you see the message “git fatal: The current branch has no upstream branch”, and have example commands to set the upstream branch in git, no matter what your branch name or remote name.