47

I'm trying to push one of my projects to github, and I keep getting this error:

fatal: The current branch master has no upstream branch.

I've never seen this before. I re-initialized my git, re-added my origin, deleted and re-made the repo, and recreated my SSH key.

Binarian
  • 12,296
  • 8
  • 53
  • 84
JShoe
  • 3,186
  • 9
  • 38
  • 61

11 Answers11

92

Instead of creating a new repository on Github, cloning that, or reinitializing your local repository, the following command would have been sufficient:

git push -u origin master

origin stands for the remote name (default is origin),
master is the branch you want to push, in your case it's master, otherwise you would have to change that in the command.
-u means, that your local branch will be setup to track the new created master branch on the origin repository (the master on the origin will be the upstream branch of your local branch). If the branch master doesn't exist on the remote repository, it will be created, otherwise it will be updated (the -u works no matter if it exists or not).

Kromster
  • 7,181
  • 7
  • 63
  • 111
dunni
  • 43,386
  • 10
  • 104
  • 99
  • 1
    Thanks! I didn't know about -u – Matt Dotson Jul 24 '13 at 22:46
  • If this didn't work, did you have a branch starting with a capital letter? Github seems to use lowercase letters for starting branch names. – Jason Harrison Oct 15 '14 at 22:58
  • 2
    Worked perfectly. In Tortoise Git, select Push and then tick Set upstream/track remote branch. – Jeff Dunlop Apr 25 '15 at 01:05
  • 1
    This is my 5th coming here for this line. It's needed in VS when you branch off a local branch rather than a remote one. Team Explorer in VS seemingly has no feature to push a local branch upstream if it's not already in the remote. – Philip Rego Dec 16 '16 at 15:26
32

The following command worked for me:

git branch --set-upstream-to=origin/master master
Manoj Shrestha
  • 4,246
  • 5
  • 47
  • 67
7

i faced the same problem just tell github to use the current head branch of your local repository:

git push --set-upstream origin master

wish it help you and others

Kashyap
  • 15,354
  • 13
  • 64
  • 103
Manee.O.H
  • 589
  • 8
  • 19
3

I had this problem today on my own remote repository, not Github and realized I had not made any commits to my local repository before trying to push to the remote repository.

git add -A
git commit
git push origin master
haydeniv
  • 31
  • 2
3

Some people coming to this page may simply have this error because they did git push origin and simply didn't realise that you need to specify the remote branch name as well, as in git push origin master.

If you do git branch --set-upstream-to=origin/master master a reference is added to .git\config to link the local and remote branches. I presume that then you no longer need to specify the branch name when pushing to the remote.

Cool Blue
  • 6,438
  • 6
  • 29
  • 68
1

Create the repo on github; add a README file on github and then clone the github repository. Creating the README file (or any file actually) is needed in order to get a master branch.

Notice how github prompts for creating a README when creating a repository: enter image description here

GoZoner
  • 67,920
  • 20
  • 95
  • 145
  • Downvoted. This answer just assumes that the repo doesn't exist yet. Which, judging by this answer having been accepted by JShoe, happened to be the case for them. In case it's not obvious: don't do this if you already have one set up, instead see the other popular answers all of which give some variation of `git push --set-upstream origin master`. – Leif Willerts Mar 09 '21 at 15:08
1

Cool Blue's answer ALMOST worked for me.

First I did: "git branch --set-upstream-to=origin/master master", as recommended by Cool Blue.

But still received error message:
"failed to push some refs to '' hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. 'git pull ...') before pushing again."

So I... did a "git push -f" command after the git branch, which worked finally worked for me.

After the forced push, subsequent "git push" commands worked without issue.

Nifty
  • 11
  • 1
1

In case someone is still running into this issue, this command solved it for me

git push --set-upstream origin master
V P
  • 170
  • 8
0

Try both the HTTP and SSH urls? I had a problem when I was using the SSH url but, when I switched to the HTTP one, it worked like a charm.

Here is what I changed:

First, view the remote URL

git remote -v 

and you get destinations back.

git remote rm destination 

Follow this link if you need help: https://help.github.com/articles/removing-a-remote/

Then,

git remote add origin url 
git push -u origin master
kgui
  • 4,015
  • 5
  • 41
  • 53
0

git push -u origin master has been deprecated.

Use git config --global push.default current instead

Saul Feliz
  • 668
  • 3
  • 11
  • 20
-1

You need to configure the remote first, then push.

git remote add origin url-to-your-repo

instructions

Santosh Pillai
  • 8,169
  • 1
  • 31
  • 27