Git keeps prompting me for password

2019-01-01 04:14发布

问题:

I\'ve been using Git for a while now, but the constant requests for a password are starting to drive me up the wall.

I\'m using OSX and Github, and I set up Git and my SSH keys as instructed by GitHub\'s Set Up Git page . I\'ve also added the github SSH key to my Mac OSX keychain, as mentioned on GitHub\'s SSH key passphrases page . My public key is registered with Git.

Nevertheless, every time I try to git pull, I have to enter my username and password. Is there something other than an SSH key that I need to set up for this?

回答1:

I think you may have the wrong git repo url.

Open .git/config and find the [remote \"origin\"] section. Make sure you\'re using the SSH one:

ssh://git@github.com/username/repo.git

You can see the ssh url in the main page of your repo if you click clone or download and choose ssh

And NOT the https or git one:

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

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

[Edit:] If Git complains that \'origin\' has already been added, open the .config file and edit the url = \"...\" part after [remote origin] as url = ssh://github/username/repo.git



回答2:

Configuring credential.helper

On OS X (now macOS) run this in the Terminal

git config --global credential.helper osxkeychain

It enables git to use Keychain.app to store username and password and to retrieve the passphrase to your private ssh key from the keychain.

For windows use:

git config --global credential.helper wincred

Troubleshooting

If the git credential helper is configured correctly macOS saves the passphrase in the keychain. Sometimes the connection between ssh and the passphrases stored in the keychain can break. Run ssh-add -K or ssh-add ~/.ssh/id_rsa to add the key to keychain again.

macOS 10.12 Sierra changes to ssh

For macOS 10.12 Sierra ssh-add -K needs to be run after every reboot. To avoid this create ~/.ssh/config with this content.

Host *
   AddKeysToAgent yes
   UseKeychain yes
   IdentityFile ~/.ssh/id_rsa

From the ssh_config man page on 10.12.2:

UseKeychain

On macOS, specifies whether the system should search for passphrases in the user\'s keychain when attempting to use a particular key. When the passphrase is provided by the user, this option also specifies whether the passphrase should be stored into the keychain once it has been verified to be correct. The argument must be \'yes\' or \'no\'. The default is \'no\'.

Apple has added Technote 2449 which explains what happened.

Prior to macOS Sierra, ssh would present a dialog asking for your passphrase and would offer the option to store it into the keychain. This UI was deprecated some time ago and has been removed.



回答3:

This happened to me when I upgraded to macOS Sierra. Looks like the ssh agent got cleared upon upgrade.

$ ssh-add -L
The agent has no identities.

Simply running ssh-add located my existing identity, entered the password and good to go again.



回答4:

Use this: Replace github.com with the appropriate hostname

git remote set-url origin git@github.com:user/repo.git


回答5:

As others have said, you can install a password cache helper. I mostly just wanted to post the link for other platforms, and not just mac. I\'m running a linux server and this was helpful: https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux

For mac:

git credential-osxkeychain

Windows:

git config --global credential.helper wincred

Linux:

git config --global credential.helper cache
git config --global credential.helper \'cache --timeout=3600\'
# Set the cache to timeout after 1 hour (setting is in seconds)


回答6:

Also look for who is asking you for the passphrase. Is it git or your SSH agent?

In my case, every time I did git pull it was asking me:

Enter passphrase for key \'/work/username/.ssh/id_rsa\':

So I assumed it was git asking for a passphrase. So I kept hunting for solutions, only to realize later that my SSH agent had shut down. Which can be fixed using eval \'ssh-agent -s\' and ssh-add as given here.

This was a pretty silly mistake I made, but posting it here, just in case it helps someone save some time from barking up the wrong tree, like I did.



回答7:

Guide to git on windows and Github using SSH to push/pull http://nathanj.github.io/gitguide/tour.html

  1. Download and install putty
  2. Set environment variable \'GIT_SSH\' = \'path\\to\\plink.exe\' (in installed putty folder) - VERY IMPORTANT!!!
  3. RESTART WINDOWS EXPLORER for Env Variables to take effect (cannot only restart command prompt)
  4. Run puttygen.exe to generate new key, copy the PUBLIC key to Github site
  5. Save this new PRIVATE key somewhere safe on the disk (preferable not dropbox)
  6. Run putty.exe and connect SSH to github.co
  7. Quickly get to startup folder by running \"shell:startup\". 8 Make your private key startup with windows via pageant. Create a shortcut in Startup folder with syntax \"path\\to\\pageant.exe\" \"path\\to\\privatekey\"
  8. We do not need to set the \'puttykeyfile\' setting inside .git/config of our repos
  9. VERY IMPORTANT is that the \"SSH clone URL\" of Github is used and not Https


回答8:

In windows for git 1.7.9+, run the following command in command prompt to open the configuration file in text editor:

    git config --global --edit

Then in the file add the following block if not present or edit it accordingly

    [credential \"https://giturl.com\"]
        username = <user id>
        helper = wincred

Save and close the file. You will need to provide the crendentials only once after the above change.



回答9:

git config credential.helper store

Note: While this is convenient, Git will store your credentials in clear text in a local file (.git-credentials) under your project directory (see below for the \"home\" directory). If you don\'t like this, delete this file and switch to using the cache option.

If you want Git to resume to asking you for credentials every time it needs to connect to the remote repository, you can run this command:

git config --unset credential.helper

To store the passwords in .git-credentials in your %HOME% directory as opposed to the project directory: use the --global flag

git config --global credential.helper store


回答10:

Use the following command to increase the timeout period so that you could retype password for a while

git config --global credential.helper \'cache --timeout 3600\'

I used it for Bitbucket and Github it works for both. Only thing you need to do is 3600 is in seconds, increase it to whatever extent you want.I changed it to 259200 Which is about 30days. This way I re-enter my password for every 30days or so.



回答11:

I figure you fixed your problem, but I don\'t see the solution here that helped me, so here it is.

Type in terminal:

echo \"\" > ~/.ssh/known_hosts

That will empty your known_hosts file, and you\'ll have to add every host you used and have connected to, but it solved the problem.



回答12:

orkoden\'s answer on using the keychain with git in your terminal was incomplete and raises erros. This is what you have to do to save the username and password you enter in the the terminal in your keychain:

curl http://github-media-downloads.s3.amazonaws.com/osx/git-credential-osxkeychain -o git-credential-osxkeychain
sudo mv git-credential-osxkeychain /usr/local/bin
sudo chmod u+x /usr/local/bin/git-credential-osxkeychain

Then enter

git config --global credential.helper osxkeychain

If you have already done the part with git config before the curl stuff, it\'s no problem, it\'ll work



回答13:

As static_rtti said above, change

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

to

ssh://git@github.com/username/repo.git

I myself changed the https in the .git/config file to ssh, but it still wasnt working. Then I saw that you MUST change the github.com to git@github.com. A good way to get the actual correct url is to go to your project page and click this

Change HTTPS to SSH to get the right URL

Then add this URL to the config file.



回答14:

Step1: check your current config

cat .git/config

You will get:

[core]
    repositoryformatversion = 0
    filemode = true
    bare = false
    logallrefupdates = true
    ignorecase = true
    precomposeunicode = true
[remote \"origin\"]
    url = https://github.com/path_to_your_git.git
    fetch = +refs/heads/*:refs/remotes/origin/*
[user]
    name = your_username
    email = your_email
[branch \"master-staging\"]
    remote = origin
    merge = refs/heads/master-staging

Step2: remove your remote origin

git remote rm origin

Step3: add remote origin back with your username and password

git remote add origin https://your_git_username:your_git_password@github.com/path_to_your_git.git


回答15:

I agree with \"codehugger\" and using the instruction of \"orkoden\" it worked for me - on netbeans 7.3 - when you right-click on the file and select context menu - push - a \'push to remote\' window opened - there are 2 options here

  1. origin:https://github.com/myaccount/myproject.git/

  2. https://github.com/myaccount/myproject.git/

As you can see that the difference is the origin param in the url - you do not want to choose this option (1) you want to check option (2) and that works just fine for me. I hope this help someone.

Dung Le.



回答16:

There are different kind of authentications depending on your configuration. Here are few:

  1. git credential-osxkeychain.

    If your credential is invalid, remove it by:

    git credential-osxkeychain erase
    

    or:

    printf \"protocol=https\\nhost=github.com\\n\" | git credential-osxkeychain erase
    

    So git won\'t ask you for the keychain permission again. Then configure it again.

    See: Updating credentials from the OSX Keychain at GitHub

  2. Your SSH RSA key.

    For this, you need to compare your SSH key with what you\'ve added, check by ssh-add -L/ssh-add -l if you\'re using the right identity.

  3. Your HTTPS authentication (if you\'re using https instead of ssh protocol).

    Use ~/.netrc (%HOME%/_netrc on Windows), to provide your credentials, e.g.

    machine stash1.mycompany.com
    login myusername 
    password mypassword
    

    Learn more: Syncing with GitHub at SO



回答17:

Before you can use your key with github, follow this step in the tutorial Testing your SSH connection:

$ ssh -T git@github.com
# Attempts to ssh to GitHub


回答18:

If you\'re using Windows and this has suddenly started happening on out of the blue on GitHub, it\'s probably due to Github\'s recent disabling support for deprecated cryptographic algorithms on 2018-02-22, in which case the solution is simply to download and install the latest version of either the full Git for Windows or just the Git Credential Manager for Windows.



回答19:

If Git prompts you for a username and password every time you try to interact with GitHub, you\'re probably using the HTTPS clone URL for your repository.

Using an HTTPS remote URL has some advantages: it\'s easier to set up than SSH, and usually works through strict firewalls and proxies. However, it also prompts you to enter your GitHub credentials every time you pull or push a repository.

You can configure Git to store your password for you. For windows:

git config --global credential.helper wincred


回答20:

having a typo in the URL will make git asking you for username and password, stupid git

tested on Linux Kali, Git version 2.7.0

try:

git clone https://github.com/thisrepodoesntexists/doesntexists.git



回答21:

I feel like the answer provided by static_rtti is hacky in some sense. Don\'t know if this was available earlier but Git tools now provide Credential Storage.

Cache Mode

$ git config --global credential.helper cache

Use the “cache” mode to keep credentials in memory for a certain period of time. None of the passwords are ever stored on disk, and they are purged from the cache after 15 minutes.

Store Mode

$ git config --global credential.helper \'store --file ~/.my-credentials\'

Use the “store” mode to save the credentials to a plain-text file on disk, and they never expire.

I personally used the store mode. Deleted my repo, cloned it and then had to enter my credentials once.

Reference: https://git-scm.com/book/en/v2/Git-Tools-Credential-Storage



标签: git github