Git push requires username and password

1,652,449

Solution 1

A common cause is cloning using the default (HTTPS) instead of SSH. You can correct this by going to your repository, clicking "Clone or download", then clicking the "Use SSH" button above the URL field and updating the URL of your origin remote like this:

git remote set-url origin [email protected]:username/repo.git

You can check if you have added the remote as HTTPS or SSH using:

git remote -v

This is documented at GitHub: Switching remote URLs from HTTPS to SSH.

Solution 2

Permanently authenticating with Git repositories

Run the following command to enable credential caching:

$ git config credential.helper store
$ git push https://github.com/owner/repo.git

Username for 'https://github.com': <USERNAME>
Password for 'https://[email protected]': <PASSWORD>

You should also specify caching expire,

git config --global credential.helper 'cache --timeout 7200'

After enabling credential caching, it will be cached for 7200 seconds (2 hour).

Solution 3

I just came across the same problem, and the simplest solution I found was to use SSH URL instead of HTTPS one:

ssh://[email protected]/username/repo.git

And not this:

https://github.com/username/repo.git

You can now validate with just the SSH key instead of the username and password.

Solution 4

Apart from changing to SSH you can also keep using HTTPS, if you don't mind to put your password in clear text. Put this in your ~/.netrc and it won't ask for your username/password (at least on Linux and Mac):

machine github.com
       login <user>
       password <password>

Addition (see VonC's second comment): on Windows the file name is %HOME%\_netrc.

Also read VonC's first comment in case you want to encrypt.

Another addition (see user137717's comment) which you can use if you have Git 1.7.10 or newer.

Cache your GitHub password in Git using a credential helper:

If you're cloning GitHub repositories using HTTPS, you can use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.

This also works on Linux, Mac, and Windows.

Solution 5

For the uninitiated who are confused by the previous answers, you can do:

git remote -v

Which will respond with something like

origin    https://[email protected]/yourname/yourrepo.git (fetch)
origin    https://[email protected]/yourname/yourrepo.git (push)

Then you can run the command many other have suggested, but now you know yourname and yourrepo from above, so you can just cut and paste yourname/yourrepo.git from the above into:

git remote set-url origin [email protected]:yourname/yourrepo.git
Share:
1,652,449
TooCooL
Author by

TooCooL

In love with web development technologies, mostly PHP and Laravel framework

Updated on April 08, 2022

Comments

  • TooCooL
    TooCooL about 2 years

    I cloned a Git repository from my GitHub account to my PC.

    I want to work with both my PC and laptop, but with one GitHub account.

    When I try to push to or pull from GitHub using my PC, it requires a username and password, but not when I'm using the laptop!

    I don't want to type my username and password every time I interact with origin. What am I missing here?

    • jwodder
      jwodder almost 13 years
      You need to register the pubkey with your Github account (github.com/account/ssh) and configure your SSH client to use the right username.
    • TooCooL
      TooCooL almost 13 years
      I have done all of that but still it requires username and password! is it possible to use one account with two PCs?
    • ford
      ford about 11 years
      This question covers all your options for this quite well: stackoverflow.com/questions/5343068/…
    • Varun Achar
      Varun Achar about 11 years
      No need to switch over to ssh anymore. It's possible with HTTPS too. Check my answer.
    • VonC
      VonC over 10 years
      I prefer using an encrypted netrc.gpg in which I can store all my credentials for https remote repo. And that works well with the new GitHub two-factor authentication!
    • Denny Mueller
      Denny Mueller almost 9 years
      I dont know how often I ended up in this thread because I took the https url instead of the ssh...
    • Josh Desmond
      Josh Desmond almost 7 years
      Note for those using two factor authorization: you will need to use an access token instead of your password. See here
    • Braiam
      Braiam almost 3 years
  • Tekkub
    Tekkub almost 13 years
    Then as I said, your ssh keys never come into play. If you want to use ssh, you need to use the ssh clone url instead
  • Johan Kool
    Johan Kool over 12 years
    And to figure out how to change the URL, go here: stackoverflow.com/a/2432799/60488 (spoiler: git remote set-url origin git://new.url.here)
  • Tom
    Tom over 12 years
    You can check your remotes with the command: git remote -v and then if it's not correct, you can use: git remote rm aliasname (like "origin" for ex.) and re-add using: git remote add [email protected]:xxxxx again like mentioned here, the https style remote will not use your SSH keys.
  • Shreyans
    Shreyans almost 12 years
    another way to change this is to edit the .git/config in your repo. edit the url under [remote "origin"] to use the git:// url instead of https://. Thanks @Tekkub
  • Jason
    Jason almost 12 years
    This worked for me, having teh same username/password prompting problem. Note that "username" is your github username and "repo.git" is the name of your repo (I did not include the ".git" on the end, and that still worked for me). The remainder was entered verbatim.
  • Jason
    Jason almost 12 years
    Issuing git remote -v with the same issue returned origin https://github.com/myusername/my-repo-name.git After fixing the problem it returns origin [email protected]:myusername/my-repo-name (both for fetch and push). Hopefully that will be useful in trying to understand the difference.
  • Bruno Berisso
    Bruno Berisso over 11 years
    If you can't user ssh for security restrictions (like me) you can do:git remote set-url origin https://name:[email protected]/repo.git (extracted from a comment here)
  • Dielson Sales
    Dielson Sales about 11 years
    Yeah, it works on Linux, but doesn't work for gitbash on Windows.
  • JOM
    JOM almost 11 years
    The most easy way to fix the problem, just edit remote origin URL. That's all. Done. Thanx!
  • Abhishek Biswal
    Abhishek Biswal almost 11 years
    Works! Thanks. You can re-check using the command git remote -v
  • Dennis
    Dennis almost 11 years
    Why is cloning with HTTPS a common mistake? GitHub now recommends using HTTPS.
  • VonC
    VonC over 10 years
    Sounds nice and detailed. I took care of https credential helper, and you took care of ssh connections! +1
  • VonC
    VonC over 10 years
    I prefer the 'netrc' credential help (stackoverflow.com/a/18362082/6309) for caching multiple credentials (without having to remember every passwords). But if you are on Windows and want to use the memory cache, you need winstore (stackoverflow.com/a/15310274/6309)
  • Sridhar Sarnobat
    Sridhar Sarnobat over 10 years
    This worked for me but first I needed to address this: stackoverflow.com/questions/2643502/…
  • Dennis
    Dennis over 10 years
    @smftre by default that's the case, but you can use a helper to cache your credentials.
  • VonC
    VonC over 10 years
    @dolmen I see what you mean, but if you mind putting your password in a clear text, you can... encrypt it ;) See stackoverflow.com/a/18362082/6309. and that is even compatible with the 2-factor authentication (2FA) of Github: stackoverflow.com/a/18607931/6309
  • VonC
    VonC over 10 years
    @Sales it works perfectly from a DOS session or a git bash on Windows, provided you call your file %HOME%\_netrc (instead of ~/.netrc). See also stackoverflow.com/a/18362082/6309 to encrypt that file.
  • day
    day over 10 years
    Are you sure the http[s]-based URL support username expansion? The manual git-fetch(1) mentions that only for git/ssh-based URLs.
  • Eric
    Eric over 10 years
    @plmday yes, I am using it, my git version is 1.8.2.3 and 1.8.4, I am not sure if higher verion do change about this.
  • samayo
    samayo almost 10 years
    I tried this. It asks me for paraphrase everytime. I didn't set up one
  • chharvey
    chharvey almost 10 years
    I've been using HTTPS for months now (with Git v1.8ish) without having to enter my username/password. Then I upgraded to Git v2.0.1 two days ago and I have to enter my username/password every single time I do git push. Please explain?
  • William
    William almost 10 years
    NB providing your password in the URL (even when using HTTPS) means that it is visible to everything between you and your repository.
  • voltrevo
    voltrevo over 9 years
    I get Permission denied (publickey) after doing this.
  • voltrevo
    voltrevo over 9 years
    Fixed my Permission denied (publickey) using this guide: help.github.com/articles/generating-ssh-keys .
  • chtenb
    chtenb over 9 years
    Using SSH doesn't work when cloning submodules recursively AFAIK. Unless these submodules have an ssh url, but that is deprecated since it disallows users without a ssh setup to clone the repository recursively.
  • chtenb
    chtenb over 9 years
    This is is the best answer thus far IMHO.
  • Evan Hu
    Evan Hu over 9 years
    This works very well in Linux, especially using git over VPN.
  • bela83
    bela83 almost 9 years
    @chharvey And now GitHub recommends HTTPS again ! See help.github.com/articles/set-up-git/…
  • slowhand
    slowhand almost 9 years
    No, providing the password in https://username:[email protected]/ is safe. See stackoverflow.com/questions/4980912/…
  • user137717
    user137717 over 8 years
    git can also cache your username and password. It takes 30 seconds to set up. help.github.com/articles/caching-your-github-password-in-git‌​/…
  • user137717
    user137717 over 8 years
    you don't need to put it in clear text or encrypt it. the helper utility will cache it for you and takes 30 seconds to set up. help.github.com/articles/caching-your-github-password-in-git‌​/…
  • user137717
    user137717 over 8 years
    Git includes instructions for every platform on that page.
  • Banjocat
    Banjocat over 8 years
    Why would you do this instead of just adding an SSH key?
  • TheZuck
    TheZuck over 8 years
    this is the best answer here and should be the accepted one IMO
  • vpalmu
    vpalmu about 8 years
    And this was what I was looking for (for full automation)
  • Eric
    Eric about 8 years
    @Joshua The answer is old, ssh is actually a better solution, it's full automation too, I use ssh now unless when it's not working due to network issue.
  • vpalmu
    vpalmu about 8 years
    @flybird: Can't use ssh because the server doesn't support it.
  • Raphi
    Raphi almost 8 years
    What goes in place of yourrepo?
  • Davide
    Davide almost 8 years
    @Raphi run git remote -v and see what comes out
  • Bennett Brown
    Bennett Brown almost 8 years
    Other answers should prepend this advice.
  • WillJones
    WillJones almost 8 years
    As per K M Rakibul Islam's answer below, If you are using Mac OSX, you can store you Github password in the keychain, so that do not need to enter you password each time, even over HTTPS. help.github.com/articles/caching-your-github-password-in-git
  • CFL_Jeff
    CFL_Jeff about 7 years
    Maybe it's because I'm using a Stash server, but I had to put in the protocol ("ssh://") portion of the url for it to work for me: git remote set-url origin ssh://git@stash-server/my/app/repo.git
  • Akin Williams
    Akin Williams almost 7 years
    Thanks to @BrunoBerisso for the comment on prepending credentials to the http(s) url. If you still want some security and also a sanity check before pushing, you can omit your password from the url: git remote set-url origin https://[email protected]/repo.git.
  • johnnieb
    johnnieb over 6 years
    Agreed, an answer citing the official documentation should take precedence. I gladly followed their instructions and prefer to use standard conventions.
  • Soren
    Soren over 6 years
    I get fatal: No such remote 'origin'
  • gman
    gman about 6 years
    Just thought I'd point out you can set ssh to use port 443 in various ways including your ~/.ssh/config file in which case AFAIK anyone that can use https can use ssh with github at least as they support ssh on port 443. example ~/.ssh/config
  • Jerald Sabu M
    Jerald Sabu M about 6 years
    This helped me. We can only connect to outside only via https, all other outgoing ports are blocked.
  • Woodchuck
    Woodchuck almost 6 years
    Note that to use the SSH method you will also need to set up an SSH key on your machine and associate it with your Github account.
  • Bron Davies
    Bron Davies over 5 years
    you may also have to change your remote url with git remote set-url origin https://[email protected]/<owner>/<repo>.git This also works with 2FA
  • mommi84
    mommi84 over 5 years
    I fixed the permission denied problem by renaming my private SSH key into id_rsa.
  • ksridhar
    ksridhar over 5 years
    to set up the SSH key you can refer to help.github.com/articles/connecting-to-github-with-ssh
  • Gonzalo Aguilar Delgado
    Gonzalo Aguilar Delgado about 5 years
    I also think that cloning with https is a bad idea because you are adding an extra mechanism that must be secured to allow pulling/pushing without enter the pass every time. With SSH you have the public key infrastructure that takes care of it. I don't know how secure is the caching mechanism but why to use it at all if ssh cloning gives you superpowers?
  • Anupam Maurya
    Anupam Maurya about 5 years
    with cat ~/.ssh/id_rsa.pub get the rsa key and paste in git web portal setting.
  • Peter Mortensen
    Peter Mortensen almost 5 years
    %HOME% is only in Git Bash and is an absolute path. It does not exist in a CMD session. It seems to be a combination of %HOMEDRIVE% and %HOMEPATH% (but in Linux/Unix path format).
  • Peter Mortensen
    Peter Mortensen almost 5 years
    In Bash, adding one or more leading spaces will usually keep it out of history. But not in Git Bash, though.
  • Atscub
    Atscub almost 5 years
    This should be the accepted answer. It answer exactly the question.
  • ericcurtin
    ericcurtin over 4 years
    @Banjocat some companies (such as mine sadly), do not allow external outbound connections on port 22. So to access sites such as github.com passwordless, ssh keys are not useful as https must be used instead.
  • Muhammad Usama Mashkoor
    Muhammad Usama Mashkoor about 4 years
    will it be deleted automatically from the ubuntu files to after 7200 for security purpose ?
  • Urasquirrel
    Urasquirrel about 4 years
    or 43,200 for 12 hours. You login once per day. That might be more reasonable for some.
  • Joel
    Joel about 4 years
    Just tried this - got: fatal: protocol 'https' is not supported
  • SebastianH
    SebastianH almost 4 years
    great answer - with one flaw: the two mentioned git config commands should match each other. With one refering to --global and the other not direct copy&paste does not achieve the intended effect.
  • Volomike
    Volomike almost 4 years
    This https technique worked for me on Github. However, remember that username on Github is not your email address. Yes, on Github, you can login with either in the username field. However, for git commits, you must specifically use your username.
  • Ayush Mandowara
    Ayush Mandowara almost 4 years
    The good thing about setting [user] is that if you have a master git credential even then the commits will be registered by the [user] in the config.
  • topher217
    topher217 over 3 years
    Does caching actually override anything from the store command? I'd think this answer is misleading as setting the cache options would be moot if you already have things stored permanently. No?
  • topher217
    topher217 over 3 years
    For security purposes, I find this to be the best https option. Storing a key seems to be a much better idea than storing your master password in plain text somewhere. The cache options provide additional layers of protection, by only allowing someone to interact if the current user on your machine, but if someone were to have access to your computer in the time before the cache dropped, there is still the potential risk. With this, even if the worst case scenario above occurred, you could just go remove the key from github from another computer.
  • topher217
    topher217 over 3 years
    I think you'd still need to add git config credential.helper store prior to pushing or otherwise interacting with the remote, otherwise this token would be no longer be present next time you went to interact with the remote. Right?
  • Adil B
    Adil B over 3 years
    @topher217: that's correct. You can use the Git credential helper or even something like Mac OSX's Keychain to store these tokens for repeated use.
  • FantomX1
    FantomX1 over 3 years
    git remote set-url origin https://[email protected]/name/repo.git
  • itsraghz
    itsraghz over 3 years
    fantastic. This has actually helped me because my situation was a bit different, as even though my remote was set to 'ssh' instead of 'https' it was still asking for a password to be entered every time when I issued git push , git pull etc, and I was not liking the suggestions of storing the creds though it is my personal machine (as a best practice). This suggestion of ssh-add really helped. Thank you :)
  • jkr
    jkr over 3 years
    git remote set-url origin name:[email protected]/repo.git , If @ is present in password, you need to encode that with %40 like, pass@word -> pass%40word.
  • John Gorenfeld
    John Gorenfeld almost 3 years
    Worked brilliantly for me in Windows WSL Ubuntu. I hope Github updates their "first time doing this??" prompt in fresh repos to suggest SSH instead of HTTPS.
  • Nikhil VJ
    Nikhil VJ almost 3 years
    just a note, for repos under org instead of personal accounts, it seems to be: https://github.com/orgname/yourrepo.git
  • Nikhil VJ
    Nikhil VJ almost 3 years
    Thanks, the last hack - editing repo_home/.git/config did the job for me. I'd already setup ssh keys before that.
  • SurpriseDog
    SurpriseDog almost 3 years
    View current urls with git remote -v and set them with git remote set-url --push origin
  • Caterpillaraoz
    Caterpillaraoz over 2 years
    what a poor UX,my god
  • Caterpillaraoz
    Caterpillaraoz over 2 years
    Done and worked! but it again asks for credentials when pushing a tag...
  • étale-cohomology
    étale-cohomology over 2 years
    what a the githosting URL? For example, what would that be for Gitlab
  • roy
    roy over 2 years
    Thanks, this worked for mac users
  • Nikki
    Nikki over 2 years
    This command doesn't work for me at all. No such command.
  • ipatch
    ipatch over 2 years
    unfortunately i don't think this persist through reboots or after the expiry period thus if using 2FA one has to type the personal access token OVER and OVER, something i'd prefer to not have to do.
  • Thilina Dharmasena
    Thilina Dharmasena over 2 years
    This worked only the windows 10 and that server git version miss with your computer version.
  • Deqing
    Deqing about 2 years
    Thanks, that helps. One thing to add, if github.com is not found in Keychain Access, that's ok, just do step 3 and then it will show in the keychain access.
  • tijko
    tijko about 2 years
    Yep I had the remote set to the wrong or should I say legacy url...big thx!
  • sci9
    sci9 about 2 years
    git remote set-url origin ssh://git@host:port/user/repo.git worked for me.