Git asks for username every time I push

2019-01-09 20:41发布

问题:

Whenever I try to push into my repo git asks for both username & password.

I have no problem in re-entering my password each time but the problem is in entering username. I use https to clone my repository.

So, how can I configure git so that it doesn't asks for username on each git push.

I am new to linux but IIRC in windows git push only asks for password.

回答1:

Edit (by @dk14 as suggested by moderators and comments)

WARNING: If you use credential.helper store from the answer, your password is going to be stored completely unencrypted ("as is") at ~/.git-credentials. Please consult the comments section below or the answers from the "Linked" section, especially if your employer has zero tolerance for security issues.

Rationale: the approach described in the answer might introduce a major vulnerability, especially for enterprise operating on private financial data. Combined with minor security issues (often present in such) or minimal social engineering it allows to hijack most of credentials in the project's network as the location of "bare" password is known in advance. This possibly includes credentials giving partial access to production environment, therefore de facto elevating [internal] attacker's access rights and allowing to cover up hacker's identity to some extent.

As a side note (to extend this warning by example), several large enterprise projects seem to have this problem despite providing secure virtual environments for employees...Basically my appeal to get rid of https auth was rejected on this ground - but, at the time, I didn't think that an exploit (like cat ~/git.credentials | curl ...) can be injected to auxiliary bash/build scripts hosted on Git in order to spread it across users, maybe even anonymously (as some are automatically synchronized). To be fair, this hack usually wouldn't (at least) directly give access to critical systems. Except if Git-credentials === corporate-email-credentials.


Edit #2 (by @dk14):

Even though accepted, it doesn't answer the actual OP's question about omitting a username only (not password). For the readers with that exact problem @grawity's answer might come in handy.


Original answer (by @Alexander Zhu):

You can store your credentials using the following command

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

Also I suggest you to read
$ git help credentials



回答2:

You can accomplish this in the .git/config file of your local repository. This file contains a section called 'remote' with an entry called 'url'. The 'url' entry should contains the https link of repository you're talking about.

When you prefix the host 'url' with your username, git shouldn't be asking for your username anymore. Here's an example:

url = https://username@repository-url.com


回答3:

Permanently authenticating with Git repositories

Run following command to enable credential caching:

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

Username for 'https://github.com': <USERNAME>
Password for 'https://USERNAME@github.com': <PASSWORD>

Use 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).


Read credentials Docs

$ git help credentials


回答4:

If you are using https instead of ssh, you can edit "url" in .git/config as user701648 says, but if you want you can add also the password:

url = https://username:password@repository-url.com


回答5:

Add new SSH keys as described in this article on GitHub.

If Git still asks you for username & password, try changing https://github.com/ to git@github.com: in remote URL:

$ git config remote.origin.url 
https://github.com/dir/repo.git

$ git config remote.origin.url "git@github.com:dir/repo.git"


回答6:

The easiest way I found was with this command:

git config --global credential.https://github.com.username <your_username>

This works on a site by site basis and modifies your global git config.

To see the changes, use:

git config --global --edit


回答7:

You can set your username for all repositories at a given site by putting something like the following in your Git configuration file. You'll want to change "https://example.com" and "me" to the actual URL and your actual username.

[credential "https://example.com"]
    username = me

(This is directly from "git help credentials")



回答8:

Make sure that you are using SSH instead of http. Check this SO answer.



回答9:

In addition to user701648's answer, you can store your credentials in your home folder (global for all projects), instead of project folder using the following command

$ git config --global credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>


回答10:

Changing the cloned repo works:

git remote set-url origin git@github.com:gitusername/projectname.git

Using cached credentials works, and setting the timeout period makes things less annoying. If you use the Windows credential helper, that should be the easiest method (especially if SSH is blocked).



回答11:

If you use SSH version, you will not have any problems with passwords.Only one time you generate SSH key, to tell git, that this pc will work with this github account and never ask me again about any access (for this pc).

How To Generate SSH for Github



回答12:

When I only git pull, git pull origin xxx, git asks for both username & password.

git config credential.helper store

it works.



回答13:

The easiest way is to create a ~/.netrc file with the following contents:

machine github.com
login YOUR_GITHUB_USERNAME
password YOUR_GITHUB_PASSWORD

(as shown here: https://gist.github.com/ahoward/2885020)

You can even close up the permissions on this file so that no one can read your password by typing:

chmod 600 ~/.netrc


回答14:

  1. $ git config credential.helper store

  2. $ git push/push https://github.com/xxx.git

  3. Then enter your user name and password.

  4. Done


回答15:

To avoid entering username, but still be prompted to enter a password, then you can simply clone your repository including the username:

git clone user_name@example.gitrepo.com/my_repo.git


标签: linux git github