Git push requires username and password
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
TooCooL
In love with web development technologies, mostly PHP and Laravel framework
Updated on April 08, 2022Comments
-
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 almost 13 yearsYou need to register the pubkey with your Github account (github.com/account/ssh) and configure your SSH client to use the right username.
-
TooCooL almost 13 yearsI have done all of that but still it requires username and password! is it possible to use one account with two PCs?
-
ford about 11 yearsThis question covers all your options for this quite well: stackoverflow.com/questions/5343068/…
-
Varun Achar about 11 yearsNo need to switch over to ssh anymore. It's possible with HTTPS too. Check my answer.
-
VonC over 10 yearsI 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 almost 9 yearsI dont know how often I ended up in this thread because I took the https url instead of the ssh...
-
Josh Desmond almost 7 yearsNote for those using two factor authorization: you will need to use an access token instead of your password. See here
-
Braiam almost 3 yearsDoes this answer your question? Is there a way to cache https credentials for pushing commits?
-
-
Tekkub almost 13 yearsThen 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 over 12 yearsAnd 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 over 12 yearsYou 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 almost 12 yearsanother way to change this is to edit the
.git/config
in your repo. edit the url under[remote "origin"]
to use thegit://
url instead ofhttps://
. Thanks @Tekkub -
Jason almost 12 yearsThis 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 almost 12 yearsIssuing
git remote -v
with the same issue returnedorigin https://github.com/myusername/my-repo-name.git
After fixing the problem it returnsorigin [email protected]:myusername/my-repo-name
(both for fetch and push). Hopefully that will be useful in trying to understand the difference. -
Bruno Berisso over 11 yearsIf 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 about 11 yearsYeah, it works on Linux, but doesn't work for gitbash on Windows.
-
JOM almost 11 yearsThe most easy way to fix the problem, just edit remote origin URL. That's all. Done. Thanx!
-
Abhishek Biswal almost 11 yearsWorks! Thanks. You can re-check using the command
git remote -v
-
Dennis almost 11 yearsWhy is cloning with HTTPS a common mistake? GitHub now recommends using HTTPS.
-
VonC over 10 yearsSounds nice and detailed. I took care of https credential helper, and you took care of ssh connections! +1
-
VonC over 10 yearsI 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 over 10 yearsThis worked for me but first I needed to address this: stackoverflow.com/questions/2643502/…
-
Dennis over 10 years@smftre by default that's the case, but you can use a helper to cache your credentials.
-
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 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 over 10 yearsAre you sure the http[s]-based URL support username expansion? The manual
git-fetch(1)
mentions that only forgit
/ssh
-based URLs. -
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 almost 10 yearsI tried this. It asks me for
paraphrase
everytime. I didn't set up one -
chharvey almost 10 yearsI'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 almost 10 yearsNB providing your password in the URL (even when using HTTPS) means that it is visible to everything between you and your repository.
-
voltrevo over 9 yearsI get
Permission denied (publickey)
after doing this. -
voltrevo over 9 yearsFixed my
Permission denied (publickey)
using this guide: help.github.com/articles/generating-ssh-keys . -
chtenb over 9 yearsUsing 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 over 9 yearsThis is is the best answer thus far IMHO.
-
Evan Hu over 9 yearsThis works very well in Linux, especially using git over VPN.
-
bela83 almost 9 years@chharvey And now GitHub recommends HTTPS again ! See help.github.com/articles/set-up-git/…
-
slowhand almost 9 yearsNo, providing the password in
https://username:[email protected]/
is safe. See stackoverflow.com/questions/4980912/… -
user137717 over 8 yearsgit 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 over 8 yearsyou 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 over 8 yearsGit includes instructions for every platform on that page.
-
Banjocat over 8 yearsWhy would you do this instead of just adding an SSH key?
-
TheZuck over 8 yearsthis is the best answer here and should be the accepted one IMO
-
vpalmu about 8 yearsAnd this was what I was looking for (for full automation)
-
Eric about 8 years@Joshua The answer is old,
ssh
is actually a better solution, it's full automation too, I usessh
now unless when it's not working due to network issue. -
vpalmu about 8 years@flybird: Can't use ssh because the server doesn't support it.
-
Raphi almost 8 yearsWhat goes in place of
yourrepo
? -
Davide almost 8 years@Raphi run
git remote -v
and see what comes out -
Bennett Brown almost 8 yearsOther answers should prepend this advice.
-
WillJones almost 8 yearsAs 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 about 7 yearsMaybe 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 almost 7 yearsThanks 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 over 6 yearsAgreed, an answer citing the official documentation should take precedence. I gladly followed their instructions and prefer to use standard conventions.
-
Soren over 6 yearsI get
fatal: No such remote 'origin'
-
gman about 6 yearsJust 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 about 6 yearsThis helped me. We can only connect to outside only via https, all other outgoing ports are blocked.
-
Woodchuck almost 6 yearsNote 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 over 5 yearsyou 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 over 5 yearsI fixed the permission denied problem by renaming my private SSH key into
id_rsa
. -
ksridhar over 5 yearsto set up the SSH key you can refer to help.github.com/articles/connecting-to-github-with-ssh
-
Gonzalo Aguilar Delgado about 5 yearsI 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 about 5 yearswith
cat ~/.ssh/id_rsa.pub
get the rsa key and paste in git web portal setting. -
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 almost 5 yearsIn Bash, adding one or more leading spaces will usually keep it out of history. But not in Git Bash, though.
-
Atscub almost 5 yearsThis should be the accepted answer. It answer exactly the question.
-
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 about 4 yearswill it be deleted automatically from the ubuntu files to after 7200 for security purpose ?
-
Urasquirrel about 4 yearsor 43,200 for 12 hours. You login once per day. That might be more reasonable for some.
-
Joel about 4 yearsJust tried this - got:
fatal: protocol 'https' is not supported
-
SebastianH almost 4 yearsgreat 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 almost 4 yearsThis 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 almost 4 yearsThe 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 over 3 yearsDoes 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 over 3 yearsFor 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 over 3 yearsI 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 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 over 3 years
git remote set-url origin https://[email protected]/name/repo.git
-
itsraghz over 3 yearsfantastic. 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 ofssh-add
really helped. Thank you :) -
jkr over 3 yearsgit 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 almost 3 yearsWorked 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 almost 3 yearsjust a note, for repos under org instead of personal accounts, it seems to be:
https://github.com/orgname/yourrepo.git
-
Nikhil VJ almost 3 yearsThanks, the last hack - editing
repo_home/.git/config
did the job for me. I'd already setup ssh keys before that. -
SurpriseDog almost 3 yearsView current urls with
git remote -v
and set them withgit remote set-url --push origin
-
Caterpillaraoz over 2 yearswhat a poor UX,my god
-
Caterpillaraoz over 2 yearsDone and worked! but it again asks for credentials when pushing a tag...
-
étale-cohomology over 2 yearswhat a the
githosting URL
? For example, what would that be forGitlab
-
roy over 2 yearsThanks, this worked for mac users
-
Nikki over 2 yearsThis command doesn't work for me at all. No such command.
-
ipatch over 2 yearsunfortunately 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 over 2 yearsThis worked only the windows 10 and that server git version miss with your computer version.
-
Deqing about 2 yearsThanks, 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 about 2 yearsYep I had the remote set to the wrong or should I say legacy url...big thx!
-
sci9 about 2 years
git remote set-url origin ssh://git@host:port/user/repo.git
worked for me.