How do I ensure Git doesn't ask me for my GitHub username and password?

174

57

I am working with a repo on GitHub and everytime I try to push something, it asks for my GitHub username and password. I don't want it doing that.

I tried the instructions on setting your email in Git, namely set up the Git variables github.user and github.token, but that didn't make any difference.

I don't understand why this is happening.

picardo

Posted 2010-10-14T17:17:28.260

Reputation: 2 387

You need to cache the password: https://help.github.com/articles/set-up-git#password-caching

– Piotr Usewicz – 2012-06-14T19:59:36.647

Answers

34

You need to set-up an ssh-agent against which you only need to authenticate once. See this answer over at SO for a howto.

Benjamin Bannier

Posted 2010-10-14T17:17:28.260

Reputation: 13 999

1But this solution is for ssh-family phrases. The problem is git uses "git push" which doesn't trigger the ssh-agent, I assume. (I've already done this, so I know it doesn't work.) – picardo – 2010-10-14T18:27:38.093

2@picardo: I've done this, and it does indeed work. – Daenyth – 2010-10-14T19:44:47.800

215

Had a similar problem today: I messed things up in my working copy, so I decided to rename the directory and clone my project again from github. But after doing that, I had to enter my password to do any pull/push request, instead of entering the passphrase just once as I used to.

That was because today I used the https protocol to clone the project! To check what protocol you're using just run

git config -l

and look at the line starting with 'remote.origin.url'.

To switch protocols:

git config remote.origin.url git@github.com:the_repository_username/your_project.git

the_repository_username and your_project should be replaced with the appropriate repository name and the owner of that repository. The username will be yours if you own the repository, or the repository owner's username otherwise.

Sergio Morstabilini

Posted 2010-10-14T17:17:28.260

Reputation: 2 251

Why not just do git config --global user.name "my_username"? I do agree about the dumbness of github not adding the username to the clone URL. – Buttle Butkus – 2016-01-26T04:14:38.037

Thank you, @ChrisK!! The SSH / HTTPS difference at https://help.github.com/articles/why-is-git-always-asking-for-my-password/ was helpful for me. :-)

– Ryan – 2017-01-10T20:29:55.780

Instead of listing all settings with git config -l, you can use git remote -v to specifically view your remote repository URLs. – Stevoisiak – 2018-06-25T15:09:35.533

When I try pushing I get git@github.com: Permission denied (publickey). – Stevoisiak – 2018-06-25T15:43:31.197

5It worked for me. One thing is that the url does not my contain my username but the repo address as it appears on github: git@github.com:some-user/repo-name.git – B Seven – 2012-02-15T07:04:38.177

1

Note: Google brought me here. I had the remote.origin.url set to the https method and didn't even notice there was https vs ssh methods. This URL helped me see the obvious: https://help.github.com/articles/why-is-git-always-asking-for-my-password

– Chris K – 2012-06-17T03:31:04.543

Is there any way to pass password entering too? – zishe – 2012-07-30T20:58:57.077

Q for those with git knowledge: Any difference (or preference) between git config remote.origin.url ... used above, vs. git remote set-url origin ...? – Daryn – 2013-01-09T18:47:47.787

I could not figure how to remove a wrong origin like that. The answer is: git remote rm origin – Vacilando – 2013-04-07T14:56:59.640

31

If you are using HTTPS instead of SSH , you can follow this :

  1. Find your remote URL (remote.origin.url) with

    git config -l
    

    thanks to Sergio Morstabilini

  2. Your remote URL will be like this : https://{USERNAME}@github.com/{USERNAME}/{REPONAME}.git

  3. Execute this command :

    git config remote.origin.url https://{USERNAME}:{PASSWORD}@github.com/{USERNAME}/{REPONAME}.git
    

Eray

Posted 2010-10-14T17:17:28.260

Reputation: 441

2my username itself contains @, didn't worked for me. – coding_idiot – 2014-08-20T04:08:11.700

But if it does not (you don't need to use your email) it works perfectly! Thanks. – Killah – 2014-10-16T19:33:31.050

4

If your username includes an @, you need to URL-encode it. http://meyerweb.com/eric/tools/dencoder/

– Tim Bodeit – 2014-10-22T07:23:26.963

1You should omit your password for security reasons. – zygimantus – 2015-12-26T17:14:14.187

3beware, password will be stored in plain text inside in the .git/config file! – Amro – 2016-01-13T13:07:52.077

This answer is wrong, instead of username after @github.com, it should be the path to your repo. For example, my repo in my organization is mycompanyname/myteamname, so mycompanyname should go after github.com – CommonCoreTawan – 2019-04-18T15:26:34.423

26

I prefer to use HTTPS, I find it easier and more secure than setting up the ssh keys.

Using HTTPS, you can prevent git from asking your username for github remotes with the following:

git config --global url."https://yourusername@github.com".insteadOf "https://github.com"

And you can at least reduce the frequency git asks for your password with:

git config --global credential.helper 'cache --timeout=28800'

Where 28800 are 8 hours. I use this setup to enter my password only once, when I start my working day.

After that you will have these entries inside your ~/.gitconfig

[url "https://yourusername@github.com"]
    insteadOf = https://github.com

[credential]
    helper = cache --timeout=28800

Source:

http://git-scm.com/docs/git-credential-cache

http://git-scm.com/docs/git-config

stefanmaric

Posted 2010-10-14T17:17:28.260

Reputation: 361

6

When you set up an ssh key for github, if it's not your default key, you will need to add a section to your ~/.ssh/config

Host *github.com
    User git
    IdentityFile ~/.ssh/github_id_rsa

Daenyth

Posted 2010-10-14T17:17:28.260

Reputation: 5 742

6

If you are on Windows using HTTPS, try the Git Credential Store - it uses the Windows Credential Store to hold your name and password.

Windows Credential Store for Git
This application is a small helper app designed to follow the 
git credentials API as defined by the Git Documentation.

Installation
1. Download the git-credential-winstore.exe application
2. Run it! If you have GIT in your PATH, it should just work.

Then the next time you enter your name and password it will remember them for you.

Brian Burns

Posted 2010-10-14T17:17:28.260

Reputation: 560

1

Thank you. The project was renamed to Git-Credential-Manager-for-Windows.

– Michael S. – 2016-05-11T15:16:33.223

5

Also, if you wish to be prompted for your password every-time, but just not your username, then you configure the remote as HTTPS with a username.. Like this..

git config remote.origin.url https://USERNAME@github.com/repo_owner/repo_name

After this, you will be prompted for your password every time, but not your username.

This is how I prefer it, since I like being forced to type my github password before sharing with the world.

David Jeske

Posted 2010-10-14T17:17:28.260

Reputation: 181

2

use SSH

To prevent GitHub asking for the password while pushing, ideally you should use SSH (git@github.com:...) instead of HTTPS URL (https://github.com/...) and add your SSH key to your GitHub account. See: Which remote URL should I use?

GitHub - Clone with SSH

a credential helper

Otherwise if you really need to use HTTPS, to cache your GitHub password in Git, you should use a credential helper to tell Git to remember your GitHub username and password every time it talks to GitHub.

  • Mac: git config --global credential.helper osxkeychain (osxkeychain helper is required),
  • Windows: git config --global credential.helper wincred
  • Linux and other: git config --global credential.helper cache

.netrc

Another method is to use configure your user/password in ~/.netrc (_netrc on Windows), e.g.

machine github.com
login USERNAME
password PASSWORD

OAuth

Use OAuth token (Personal API token) to push the changes, e.g.

git push https://4UTHT0KEN@github.com/foo/bar

Related:

kenorb

Posted 2010-10-14T17:17:28.260

Reputation: 16 795

2

Things are a little different if you're using 2-factor auth as I am. Since I didn't find a good answer elsewhere, I'll stick one here so that maybe I can find it later.

If you're using 2-factor auth, then specifying username/password won't event work - you get access denied. But you can use an application access token and use git's credential helper to cache that for you. Here are the pertinent links:

And I don't remember where I saw this but when you're asked for your Username - that's where you stick the application access token. Then leave the password blank. Worked on my mac.

John Berryman

Posted 2010-10-14T17:17:28.260

Reputation: 153

1

Also if you are using Windows, you can use:

$ git config --global credential.helper wincred

you just have to sign in one more time and then git will remember.

William Earl Cheaqui

Posted 2010-10-14T17:17:28.260

Reputation: 11

1

Asking the username is annoying for me, but asking for a password is nice since it ensures you really want to go public with your changes...

So I just add this to my ".gitconfig"

[url "https://myusername@github.com"]
    insteadOf = https://github.com

ViToni

Posted 2010-10-14T17:17:28.260

Reputation: 111

0

Use Git Credential Manager for Windows, if you're on Windows.

This project includes:

  • Secure password storage in the Windows Credential Store
  • Multi-factor authentication support for Visual Studio Team Services
  • Two-factor authentication support for GitHub
  • Personal Access Token generation and usage support for Visual Studio Team Services and GitHub
  • Non-interactive mode support for Visual Studio Team Services backed by Azure Directory
  • Kerberos authentication for Team Foundation Server
  • Optional settings for build agent optimization

Simply download the latest version and install it.

How to use

You don't. It magically works when credentials are needed.

Jalal

Posted 2010-10-14T17:17:28.260

Reputation: 101