414

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

peeplesoft@jane3:~/846156 (master) $ 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

So I tried it and got this:

peeplesoft@jane3:~/846156 (master) $ git push --set-upstream origin master

fatal: Authentication failed

Another stackoverflow thread suggested I try the following, with disappointing results.

peeplesoft@jane3:~/846156 (master) $ git push -u origin master

fatal: Authentication failed

Then I tried this:

peeplesoft@jane3:~/846156 (master) $ git config remote.origin.push HEAD

peeplesoft@jane3:~/846156 (master) $ git push

fatal: Authentication failed

Any hints?

alex
  • 479,566
  • 201
  • 878
  • 984
user1524361
  • 4,295
  • 3
  • 15
  • 6

33 Answers33

215

You fixed the push, but, independently of that push issue (which I explained in "Why do I need to explicitly push a new branch?": git push -u origin master or git push -u origin --all), you need now to resolve the authentication issue.

That depends on your url (ssh as in 'git@github.com/yourRepo, or https as in https://github.com/You/YourRepo)

For https url:

If your account is protected by the two-factor authentication, your regular password won't work (for https url), as explained here or here.

Same problem if your password contains special character (as in this answer)

If https doesn't work (because you don't want to generate a secondary key, a PAT: personal Access Token), then you can switch to ssh, as I have shown here.


As noted by qwerty in the comments, you can automatically create the branch of same name on the remote with:

git push -u origin head 

Why?

  • HEAD (see your .git\HEAD file) has the refspec of the currently checked out branch (for example: ref: refs/heads/master)
  • the default push policy is simple

Since the refpec used for this push is head: (no destination), a missing :<dst> means to update the same ref as the <src> (head, which is a branch).

That won't work if HEAD is detached though.


Or you can use Git 2.37 (Q3 2022) and the new global option push.autoSetupRemote:

git config --global push.autoSetupRemote true
git push
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    If you are on any branch, this is useful `git push origin head -u`. Automatically creates the branch of same name on the remote. – Qwerty May 15 '20 at 10:15
  • @VonC Can you take a look at https://stackoverflow.com/questions/64480132/how-to-authenticate-github-on-ubuntu-git-credential-netrc-is-not-a-git-comma – Richard Rublev Oct 22 '20 at 10:24
145

Also you can use the following command:

git push -u origin master

This creates (-u) another branch in your remote repo. Once the authentication using ssh is done that is.

TantrajJa
  • 1,987
  • 2
  • 15
  • 9
  • 2
    This is really relevant to the problem, since the issue is with authentication. He's also stated that he already tried that in his post. – Mike Precup Jul 19 '14 at 01:18
  • 4
    I downvoted for 2 reasons. 1st one: the OP stated that he already tried this. 2nd one: `-u` option is the shortcut for the `--set-upstream` option which has nothing to do with the creation of a new branch. From the [documentation of the `-u` or `--set-upstream` option](https://git-scm.com/docs/git-push#Documentation/git-push.txt--u): "For every branch that is up to date or successfully pushed, add upstream (tracking) reference, used by argument-less git-pull[1] and other commands.". Actually I don't get why this answer got upvoted that much as it is misleading. – louisfischer Jul 11 '19 at 09:44
93

If you define the action git push it should take it if no refspec is given on the command line, no refspec is configured in the remote, and no refspec is implied by any of the options given on the command line.

Just do it:

git config --global push.default current

then

git push
DariusV
  • 2,645
  • 16
  • 21
  • 1
    You need to be aware that this does changes to your global git settings, which may have some adverse effects. But this was exactly what I was looking for. – Mavamaarten Nov 25 '19 at 13:55
  • 2
    @Mavamaarten what are the potential adverse effects? – mutantacule Mar 26 '20 at 12:20
  • 2
    @kosii Literally what it does: to always push a local branch to upstream. It is my default way of work as I see no harm creating new branches upstream 99% of the time, but it may not be true for other people. – superarts.org Dec 21 '20 at 16:51
52

Apparently you also get this error message when you forget the --all parameter when pushing for the first time. I wrote

git push -u origin

which gave this error, it should have been

git push -u origin --all

Oh how I love these copy-paste errors ...

TheEye
  • 9,280
  • 2
  • 42
  • 58
  • 5
    Thanks. I was getting this error in Visual Studio Code and this worked, but in the context of multiple developers can somebody explain what this did? – Hell.Bent May 09 '17 at 15:43
22

Please try this scenario

git push -f --set-upstream origin master
Ajmal Sha
  • 906
  • 4
  • 17
  • 36
hoochanlon
  • 261
  • 2
  • 5
20

use this command first before you push in to the branch

git config --global push.default current

After executing the above command use git push command.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
KileMax
  • 213
  • 2
  • 10
18

You need to configure the remote first, then push.

git remote add origin url-to-your-repo

Actual Instructions

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

It means that you don't have your branch(the branch that you want to push) in your remote repository, in other words it does not exist in your remote repository(it wasn't created yet)... So use this Code:

git push -u origin 'your branch name'

this code will create your branch in your remote repository and will push it...

Enos Okello
  • 121
  • 1
  • 9
jamal zare
  • 1,037
  • 12
  • 13
11

on a very simple side, once you have other branches, you can't just use for pushing a branch

git push

But you need to specify the branch now, even if you have checkout the branch you want to push, so

git push origin <feature_branch>

Where can be even the master branch

Abhishek
  • 3,337
  • 4
  • 32
  • 51
10

Well, I was having the same trouble while uploading and I resolved it by doing the same thing which it says to do: Earlier I was trying to push through terminal to my repository in linux by https like

git push https://github.com/SiddharthChoudhary/ClientServerCloudComputing.git

But was not getting any result and hence I went down deeper and tried:

git push --set-upstream https://github.com/SiddharthChoudhary/ClientServerCloudComputing.git master

And it worked. Thus then you will get prompted with username and password. I also generated a token and instead of Password I pasted the token and thus, being done successfully.

  1. To generate a token go to your Github account and in Developer Settings and then create another token.
  2. After getting that, copy that token and paste in the password prompt when it's been asked.
zar
  • 11,361
  • 14
  • 96
  • 178
Siddharth Choudhary
  • 1,069
  • 1
  • 15
  • 20
  • You made my day! SIde note: using `SSH` i.e. `git@github.com/SiddharthChoudhary/ClientServerCloudComputing.git` should also work too, without the user credentials, if SSH authentication is set-up already. – massisenergy Jul 19 '20 at 19:32
  • EPIC WIN MY FRIEND! – Marlhex Sep 02 '20 at 05:28
7

I made the simple error of forgetting to commit:

git commit -m "first commit"

then git push origin master worked.

yl_low
  • 1,209
  • 2
  • 17
  • 26
6

I had the same problem

enter image description here

I resolved it that used below command

$ git branch --set-upstream develop origin/develop

and it will add a config in the config file in the .git folder.

enter image description here

bluetata
  • 577
  • 7
  • 12
  • This worked for me. I used the master branch both locally and remotely, so I wrote this: git branch --set-upstream master origin/master Git complained -set-upstream is getting deprecated, but replacing it with --set-upstream-to should do the trick. – Christophe Aug 20 '20 at 20:43
5

First use git pull origin your_branch_name Then use git push origin your_branch_name

FRabbi
  • 135
  • 1
  • 6
4

There is a simple solution to this which worked for me on macOS Sierra. I did these two commands:

git pull --rebase git_url(Ex: https://github.com/username/reponame.git)
git push origin master

If it shows any fatal error regarding upstream after any future push then simply run :

git push --set-upstream origin master
theRana
  • 704
  • 9
  • 10
4

If you constantly get the following git error message after attempting a git push with a new local branch:

fatal: The current branch has no upstream branch.

To push the current branch and set the remote as upstream, use

git push --set-upstream origin <branchname>

Then the issue is that you have not configured git to always create new branches on the remote from local ones.

The permanent fix if you always want to just create that new branch on the remote to mirror and track your local branch is:

git config --global push.default current

Now you can git push without any errors!

https://vancelucas.com/blog/how-to-fix-git-fatal-the-current-branch-has-no-upstream-branch/

fcdt
  • 2,371
  • 5
  • 14
  • 26
2

I was getting this error because I was trying to push the code without write access, I only had read access.

In Bitbucket, you can check access on the right side when you open your repo (Repository details) :

stackich
  • 3,607
  • 3
  • 17
  • 41
1

For me, it was because I had deleted the hidden .git folder.

I fixed it by deleting the folder, re-cloning, and re-making the changes.

AmmarBaali
  • 107
  • 3
0

1. A computer and your github associated. Use SSH. Computer code so you do not need to submit verified enter image description here

2. git can not manage empty folder. So you have to write such a readme.md saved in a file. Otherwise you will not find the file.

3. Your local project is nothing new projects move over. Please

git init

git remote add origin +"githublink"

git add .

git commit -m "" go again.

4. then git pull origin master (the key)

5. At last git push origin master (solve all problem).

http://my.oschina.net/psuyun/blog/123005 参考链接

Nishant Rajput
  • 2,053
  • 16
  • 28
王逍遥
  • 41
  • 4
0

If you are trying to push your code direct to the master branch then use command

git push origin master

It helps me.

0

I also got the same error.I think it was because I clone it and try to push back. $ git push -u origin master This is the right command.Try that

Counting objects: 8, done. Delta compression using up to 2 threads. Compressing objects: 100% (4/4), done. Writing objects: 100% (8/8), 691 bytes | 46.00 KiB/s, done. Total 8 (delta 1), reused 0 (delta 0) remote: Resolving deltas: 100% (1/1), done.

  • [new branch] master -> master Branch master set up to track remote branch master from origin.

    It was successful. Try to create new u branch 
    
Malsha
  • 41
  • 4
0

I had the same problem, the cause was that I forgot to specify the branch

git push myorigin feature/23082018_my-feature_eb
Black
  • 18,150
  • 39
  • 158
  • 271
0

For me, I was pushing the changes to a private repo to which I didn't had the write access. Make sure you have the valid access rights while performing push or pull operations.

You can directly verify via

0

If you are on any branch, you can use this:

git push origin head -u

This will automatically create new branch of the same name on the remote.

Qwerty
  • 29,062
  • 22
  • 108
  • 136
0

commit your code using

git commit -m "first commit"

then config your mail id using

git config user.email "example@example.com"

this is work for me

Boken
  • 4,825
  • 10
  • 32
  • 42
0

The thing that helped me: I saw that the connection between my directory to git wasn't established - so I did again: git push -u origin main

ghilesZ
  • 1,502
  • 1
  • 18
  • 30
mi re la
  • 1
  • 1
0

Different case with same error (backing up to external drive), the issue was that I'd set up the remote repo with clone. Works every time if you set the remote repo up with bare initially

cd F:/backups/dir
git init --bare
cd C:/working/dir
git remote add backup F:/backups/dir
git push backup master
Dave
  • 1,409
  • 2
  • 18
  • 14
0

In my mind, this is just a wrong default git behavior. Having reviewed all options support by git, also reviewed the relevant git code:

https://github.com/git/git/blob/140045821aa78da3a80a7d7c8f707b955e1ab40d/builtin/push.c#L188

The best solution I would suggest it simply override the push default command:

git config --global alias.pu 'push -u'

This basically changes the default behavior of push so that makes sense.

JAR.JAR.beans
  • 9,668
  • 4
  • 45
  • 57
0

Encountered just about the same problem, but not from the master branch. I tried to push two (2) branches to my remote repository, by using the $ git push command; which unfortunately threw up the:

fatal: The current branch <branch-name> has no upstream branch. To push the current branch and set the remote as upstream, use

 git push --set-upstream origin <branch-name>

I got it fixed with this command below:

$ git push -u origin --all

PS: The solution provided here should, i believe, make it easier for git to track out branches remotely; this could come in-handy someday, when working on projects with couple of branches.

Bravo Stack
  • 111
  • 1
  • 6
  • Seeing this mentioned upfront in [VonC's 2014 answer](https://stackoverflow.com/a/23402125): Does your answer add something? – greybeard Jul 26 '21 at 04:55
0

In my case, I have a local branch called Master, whereas master is on Github. I simply rename my Master -> master and checkout to master. And then push it. It works for me.

UC57
  • 359
  • 4
  • 16
0

It is better, Clone this repo newly. Maybe it just lost track.

Mujahidul Islam
  • 265
  • 4
  • 8
0

Please look at .git/config file, It should look like:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
[remote "origin"]
    url = https://github.com/blueray453/log-opened-file-paths.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
    remote = origin
    merge = refs/heads/master

Authentication failed means Authentication failed. Please make sure you have the correct access rights and the repository exists.

Look at [remote "origin"]. Are you using the right url? Does it start with https or git@github.com? Those two use different authentication procedure. If you are using wrong url then remove the [remote "origin"] section and run:

git remote add origin <right-url>

If [branch "master"] or [branch "main"] or whatever is missing, run:

git push --set-upstream origin master

or,

git push -u origin master

For simplicity, I will remove both [remote "origin"] and [branch "master"] section and then to push an existing repository from the command line:

git remote add origin <right-url>
git branch -M main
git push -u origin main

If you have followed Git asks for username every time I push, then from now on only git push will be enough.

Ahmad Ismail
  • 11,636
  • 6
  • 52
  • 87
-1

To resolve this issue, while checking out the code from git itself, u need to give the command like below:

git checkout -b branchname origin/branchname

Here, by default we are setting the upstream branch, so you will not be facing the mentioned issue.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Deepa
  • 1
-1

For me the problem come from the name of my branch : "#name-of-my-branch", without "#" it's work fine!