git Git不断提示我输入密码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7773181/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:04:50  来源:igfitidea点击:

Git keeps prompting me for a password

gitgithub

提问by Catherine

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

我已经使用 Git 有一段时间了,但是对密码的不断请求开始让我感到厌烦。

I'm using Mac OS X and GitHub, and I set up Git and my SSH keys as instructed by GitHub's Set Up Git page.

我正在使用 Mac OS X 和 GitHub,我按照 GitHub 的设置 Git 页面的指示设置了 Git 和我的 SSH 密钥。

I've also added the github SSH key to my Mac OS X keychain, as mentioned on GitHub's SSH key passphrases page. My public key is registered with Git.

我还在我的 Mac OS X 钥匙串中添加了 github SSH 密钥,如GitHub 的 SSH 密钥密码页面所述。我的公钥已在 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?

尽管如此,每次我尝试 Git pull 时,我都必须输入我的用户名和密码。除了 SSH 密钥之外,我还需要为此设置什么吗?

回答by static_rtti

I think you may have the wrong Git repository URL.

我认为您可能有错误的 Git 存储库 URL。

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

打开.git/config并找到 [remote "origin"] 部分。确保您使用的是 SSH:

ssh://[email protected]/username/repo.git

You can see the SSH URL in the main page of your repository if you click Clone or downloadand choose ssh.

如果单击克隆或下载并选择ssh ,则可以在存储库的主页中看到 SSH URL 。

And NOT the httpsor gitone:

而不是httpsgit之一:

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

You can now validate with justthe SSH key instead of the username and password.

您现在可以使用 SSH 密钥而不是用户名和密码进行验证。

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

如果Git的抱怨'origin' has already been added,打开.config的文件和编辑的url = "..."部分后[remote origin]url = ssh://github/username/repo.git



The same goes for other services. Make sure the address looks like: protocol://something@url

其他服务也是如此。确保地址如下所示:protocol://something@url

E.g. .git/configfor Azure DevOps:

例如.git/config对于Azure DevOps:

[remote "origin"]
    url = https://[email protected]/mystore/myproject/
    fetch = +refs/heads/*:refs/remotes/origin/*

回答by orkoden

Configuring credential.helper

配置 credential.helper

On OS X (now macOS), run this in Terminal:

在 OS X(现在是 macOS)上,在终端中运行:

git config --global credential.helper osxkeychain

It enables Git to use file Keychain.app to store username and password and to retrieve the passphrase to your private SSH key from the keychain.

它使 Git 能够使用文件 Keychain.app 来存储用户名和密码,并从钥匙串中检索到您的私有 SSH 密钥的密码短语。

For Windows use:

对于 Windows 使用:

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 keychaincan break. Run ssh-add -Kor ssh-add ~/.ssh/id_rsato add the key to keychain again.

如果 Git 凭证助手配置正确,macOS 会将密码保存在钥匙串中。有时SSH 和存储在钥匙串中的密码之间连接可能会中断。运行ssh-add -Kssh-add ~/.ssh/id_rsa再次将钥匙添加到钥匙串。

macOS v10.12 (Sierra) changes to ssh

macOS v10.12 (Sierra) 更改为 ssh

For macOS v10.12 (Sierra), ssh-add -Kneeds to be run after every reboot. To avoid this, create ~/.ssh/configwith this content.

对于 macOS v10.12 (Sierra),ssh-add -K需要在每次重启后运行。为避免这种情况,请~/.ssh/config使用此内容进行创建。

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

From the ssh_configmanpage on 10.12.2:

ssh_configman10.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'.

使用钥匙串

在 macOS 上,指定系统在尝试使用特定密钥时是否应在用户的钥匙串中搜索密码。当用户提供密码短语时,此选项还指定密码短语在验证正确后是否应存储到钥匙串中。参数必须是“是”或“否”。默认值为“否”。

Apple has added Technote 2449which explains what happened.

Apple 添加了Technote 2449来解释所发生的事情。

Prior to macOS Sierra, sshwould 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.

在 macOS Sierra 之前,ssh会显示一个对话框,询问您的密码,并提供将其存储到钥匙串中的选项。此 UI 前段时间已弃用并已删除。

回答by amcc

This happened to me when I upgraded to macOS v10.12 (Sierra). Looks like the SSH agent got cleared upon upgrade.

当我升级到 macOS v10.12 (Sierra) 时,这发生在我身上。看起来 SSH 代理在升级时被清除了。

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

Simply running ssh-addlocated my existing identity. I entered the password and was good to go again.

只需运行即可ssh-add找到我现有的身份。我输入了密码,很高兴再次去。

回答by Darrell Duane

Use this: Replace github.com with the appropriate hostname

使用这个:用适当的主机名替换 github.com

git remote set-url origin [email protected]:user/repo.git

回答by Chase Roberts

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: Caching your GitHub password in Git

正如其他人所说,您可以安装密码缓存助手。我主要只是想发布其他平台的链接,而不仅仅是 Mac。我正在运行 Linux 服务器,这很有帮助:在 Git 中缓存您的 GitHub 密码

For Mac:

对于 Mac:

git credential-osxkeychain

Windows:

视窗:

git config --global credential.helper wincred

Linux:

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)

回答by mithunpaul

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

还要寻找谁在向您询问密码。它是 Git 还是您的 SSH 代理?

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

就我而言,每次我这样做时git pull都会问我:

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

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)and ssh-addas given here.

所以我认为是 Git 要求输入密码。所以我一直在寻找解决方案,后来才意识到我的 SSH 代理已经关闭。可以使用eval $(ssh-agent)ssh-add如此处给出的那样进行修复

Also am pasting below a little snippet you can add to your ~/.bashrcfile (or the equivalent) to ensure that your SSH agent is started on your login.

还在下面粘贴一个小片段,您可以将其添加到您的~/.bashrc文件(或等效文件)中,以确保您的 SSH 代理在您登录时启动。

In any case 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.

无论如何,这是我犯的一个非常愚蠢的错误,但将其张贴在这里,以防万一它可以帮助人们节省一些时间,避免像我一样吠叫错误的树。

# Start the ssh-agent
function start_agent {
    echo "Initializing new SSH agent..."

    # Spawn ssh-agent
    /usr/bin/ssh-agent | sed 's/^echo/#echo/' > ${SSH_ENV}
    echo succeeded
    chmod 600 ${SSH_ENV}
    . ${SSH_ENV} > /dev/null
    /usr/bin/ssh-add
}

if [ -f "${SSH_ENV}" ]; then
     . ${SSH_ENV} > /dev/null
     ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
        start_agent;
    }
else
    start_agent;
fi

回答by Lokesh S

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.

注意:虽然这很方便,但 Git 会将您的凭据以明文形式存储在项目目录下的本地文件 (.git-credentials) 中(“home”目录见下文)。如果您不喜欢这样,请删除此文件并切换到使用缓存选项。

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 在每次需要连接到远程存储库时继续询问您的凭据,您可以运行以下命令:

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

要将密码存储在 %HOME% 目录而不是项目目录中的 .git-credentials 中:使用 --global 标志

git config --global credential.helper store

回答by Ravi Kumar

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. The only thing you need to do is 3600is in seconds. Increase it to whatever extent you want. I changed it to 259200which is about 30 days. This way I re-enter my password for every 30 days or so.

我将它用于 Bitbucket 和 GitHub,它适用于两者。您唯一需要做的 3600就是在几秒钟内。将其增加到您想要的任何程度。我将其更改259200为大约 30 天。这样我每 30 天左右重新输入一次密码。

回答by Rohan

In Windows for Git 1.7.9+, run the following command on the command prompt to open the configuration file in a text editor:

在 Windows for Git 1.7.9+ 中,在命令提示符下运行以下命令以在文本编辑器中打开配置文件:

    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 credentials only once after the above change.

保存并关闭文件。在上述更改后,您只需提供一次凭据。

回答by Francois

Guide to Git on Windows and GitHub using SSH to push/pull: An Illustrated Guide to Git on Windows

Guide to Git on Windows and GitHub using SSH to push/pull: An Illustrated Guide to Git on Windows

  1. Download and install PuTTY
  2. Set environment variable 'GIT_SSH' = 'path\to\plink.exe' (in installed putty folder) - very important!!!
  3. Restart Windows Explorerfor environment variables to take effect (cannot only restart command prompt)
  4. Run puttygen.exe to generate new key, copy the publickey to the GitHub site
  5. Save this new privatekey 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"
  9. We do not need to set the 'puttykeyfile' setting inside .git/config of our repositories
  10. Very importantis that the "SSH clone URL" of GitHub is used and not HTTPS.
  1. 下载并安装 PuTTY
  2. 设置环境变量 'GIT_SSH' = 'path\to\plink.exe'(在安装的 putty 文件夹中) -非常重要!!!
  3. 重启Windows资源管理器使环境变量生效(不能只重启命令提示符)
  4. 运行puttygen.exe产生新键,复制公开密钥到GitHub的网站
  5. 将此新私钥保存在磁盘上的某个安全位置(最好不是Dropbox
  6. 运行 putty.exe 并将 SSH 连接到 github.co
  7. 通过运行“shell:startup”快速进入启动文件夹。
  8. 通过选美使您的私钥在 Windows 上启动。在启动文件夹中创建一个快捷方式,语法为“path\to\pageant.exe”“path\to\privatekey”
  9. 我们不需要在存储库的 .git/config 中设置“puttykeyfile”设置
  10. 非常重要的是,使用的是 GitHub 的“SSH 克隆 URL”而不是 HTTPS。