Git push requires username and password
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.
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).
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
.
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.
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