如何确保 Git 不会询问我的 GitHub 用户名和密码?

pic*_*rdo 180 git login github

我正在 GitHub 上使用一个 repo,每次我尝试推送某些东西时,它都会询问我的 GitHub 用户名和密码。我不希望它这样做。

我尝试了在 Git 中设置电子邮件的说明,即设置 Git 变量 github.user 和 github.token,但这没有任何区别。

我不明白为什么会这样。

小智 218

今天遇到了类似的问题:我在我的工作副本中搞砸了,所以我决定重命名目录并从 github 再次克隆我的项目。但是这样做之后,我必须输入密码才能执行任何拉取/推送请求,而不是像以前那样只输入一次密码。

那是因为今天我用https协议克隆了项目!要检查您使用的是什么协议,请运行

git config -l
Run Code Online (Sandbox Code Playgroud)

并查看以“remote.origin.url”开头的行。

切换协议:

git config remote.origin.url git@github.com:the_repository_username/your_project.git
Run Code Online (Sandbox Code Playgroud)

the_repository_username并且your_project应该替换为适当的存储库名称和该存储库的所有者。如果您拥有存储库,则用户名将是您的,否则将是存储库所有者的用户名。

  • 它对我有用。一件事是 url 不包含我的用户名,而是包含在 github 上的 repo 地址:`git@github.com:some-user/repo-name.git` (5认同)

Ben*_*ier 34

您需要设置一个 ssh-agent,您只需对其进行一次身份验证。有关操作方法,请参阅SO 上的此答案

  • @picardo:我已经这样做了,它确实有效。 (2认同)

Era*_*ray 34

如果您使用的是 HTTPS 而不是 SSH,您可以按照以下步骤操作:

  1. 使用以下命令查找您的远程 URL (remote.origin.url)

    git config -l
    
    Run Code Online (Sandbox Code Playgroud)

    感谢塞尔吉奥·莫斯塔比利尼

  2. 您的远程 URL 将如下所示:https://{USERNAME}@github.com/{USERNAME}/{REPONAME}.git

  3. 执行这个命令:

    git config remote.origin.url https://{USERNAME}:{PASSWORD}@github.com/{USERNAME}/{REPONAME}.git
    
    Run Code Online (Sandbox Code Playgroud)

  • 如果您的用户名包含@,则需要对其进行 URL 编码。http://meyerweb.com/eric/tools/dencoder/ (4认同)
  • 请注意,密码将以纯文本形式存储在 `.git/config` 文件中! (4认同)
  • 我的用户名本身包含`@`,对我不起作用。 (2认同)

ste*_*ric 32

我更喜欢使用 HTTPS,我发现它比设置 ssh 密钥更容易、更安全。

使用 HTTPS,您可以通过以下方式阻止 git 询问您的 github 远程用户名:

git config --global url."https://yourusername@github.com".insteadOf "https://github.com"
Run Code Online (Sandbox Code Playgroud)

您至少可以通过以下方式降低 git 要求输入密码的频率:

git config --global credential.helper 'cache --timeout=28800'
Run Code Online (Sandbox Code Playgroud)

其中 28800 是 8 小时。当我开始工作日时,我只使用此设置输入一次密码。

之后,您将在您的 ~/.gitconfig

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

[credential]
    helper = cache --timeout=28800
Run Code Online (Sandbox Code Playgroud)

来源:

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

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


小智 8

此外,如果您希望每次都提示您输入密码,而不是您的用户名,那么您可以使用用户名将远程配置为 HTTPS .. 像这样..

git config remote.origin.url https://USERNAME@github.com/repo_owner/repo_name
Run Code Online (Sandbox Code Playgroud)

在此之后,每次都会提示您输入密码,但不会提示您输入用户名。

这就是我喜欢的方式,因为我喜欢在与世界分享之前被迫输入我的 github 密码。


Dae*_*yth 7

当您为 github 设置 ssh 密钥时,如果它不是您的默认密钥,则需要在您的 ~/.ssh/config

Host *github.com
    User git
    IdentityFile ~/.ssh/github_id_rsa
Run Code Online (Sandbox Code Playgroud)


Bri*_*rns 7

如果您在 Windows 上使用 HTTPS,请尝试Git Credential Store - 它使用 Windows Credential Store 来保存您的姓名和密码。

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.
Run Code Online (Sandbox Code Playgroud)

然后下次您输入您的姓名和密码时,它会为您记住它们。

  • 谢谢你。该项目更名为 [Git-Credential-Manager-for-Windows](https://github.com/Microsoft/Git-Credential-Manager-for-Windows)。 (2认同)