Git push 需要用户名和密码。

1859

我从GitHub账户将Git仓库克隆到了PC上。

我想要使用同一个GitHub账户在PC和笔记本电脑上进行操作。

当我尝试使用PC推送或拉取GitHub时,需要输入用户名和密码,但是使用笔记本电脑则不需要!

我不想每次与origin交互时都输入用户名和密码。我错过了什么?


3
您需要在您的Github账户中注册公钥(https://github.com/account/ssh),并配置您的SSH客户端来使用正确的用户名。 - jwodder
2
我已经做了所有的事情,但它仍然需要用户名和密码!是否可能在两台电脑上使用一个帐户? - TooCooL
1
这个问题在这方面涵盖了你所有的选项:https://dev59.com/om435IYBdhLWcg3wkA5e - ford
3
不再需要切换到 SSH,HTTPS 也可以实现。请检查我的回答。 - Varun Achar
1
我更喜欢使用加密的netrc.gpg,在其中可以存储所有https远程库的凭据。这对于新的GitHub双因素身份验证非常有效! - VonC
显示剩余4条评论
31个回答

2498

一个常见的原因是使用默认的 HTTPS 克隆而不是 SSH。您可以通过进入存储库,单击“Clone or download”,然后单击 URL 字段上方的“Use SSH”按钮并更新您的origin远程URL来纠正此问题。

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

您可以使用以下方式检查您是否将远程添加为HTTPS或SSH:

git remote -v

这在GitHub上有文档说明:从HTTPS切换到SSH的远程URL


64
想要更改URL,请前往此处:https://dev59.com/OnE95IYBdhLWcg3wKqok#2432799(提示:`git remote set-url origin git://new.url.here`)。 - Johan Kool
171
如果您由于安全限制无法使用ssh(如我),则可以执行以下操作:git remote set-url origin https://name:password@github.com/repo.git。该内容来源于此处的评论 - Bruno Berisso
162
为什么使用HTTPS克隆是常见错误?GitHub现在建议使用HTTPS。 为什么使用HTTPS进行克隆是常见错误?GitHub现在建议使用HTTPS。 - Dennis
8
默认情况下是这样的,但是您可以使用一个辅助程序来缓存您的凭据 - Dennis
32
使用这份指南解决了我的“Permission denied (publickey)”问题:https://help.github.com/articles/generating-ssh-keys/。 - voltrevo
显示剩余21条评论

605

永久验证 Git 仓库

运行以下命令以启用凭据缓存

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

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

你还应该指定缓存过期时间
git config credential.helper 'cache --timeout 7200'

启用凭证缓存后,它将被缓存7200秒(2小时)。对于每天登录一次的情况,43200秒=12小时也是一个合理的选择。
SebastianH所评论的,也可以使用类似的命令来全局配置,而不是针对每个存储库。
git config --global credential.helper store
git config --global credential.helper 'cache --timeout 7200'

1
你可能还需要使用 git remote set-url origin https://git@github.com/<owner>/<repo>.git 命令更改远程 URL。这个命令也适用于双重认证。 - Bron Davies
36
这应该是被采纳的答案。它准确回答了这个问题。 - Abraham Toledo
刚试了一下,出现了“致命错误:协议 'https' 不受支持”的提示。 - Joel
1
缓存是否会覆盖存储命令中的任何内容?我认为这个答案是误导性的,因为如果您已经永久存储了某些内容,设置缓存选项就没有意义了。不是吗? - topher217
不幸的是,我认为这种持久性无法在重新启动或过期后保持,因此如果使用2FA,一个人必须一遍又一遍地输入个人访问令牌,这是我不想做的事情。 - ipatch
显示剩余3条评论

162

我刚遇到了同样的问题,我发现最简单的解决方案是使用SSH URL而不是HTTPS:

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

而不是这个:

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

现在您可以使用只有SSH密钥进行验证,而无需使用用户名密码


1
这对我有用,但首先我需要解决这个问题:https://dev59.com/N3E85IYBdhLWcg3wtV_1 - Sridhar Sarnobat
要设置SSH密钥,您可以参考https://help.github.com/articles/connecting-to-github-with-ssh/。 - ksridhar
使用 git remote -v 查看当前的URL,使用 git remote set-url --push origin 设置它们。 - SurpriseDog

143

除了改用SSH,如果您不介意将密码以明文形式输入,则仍然可以使用HTTPS。将以下内容放入您的~/.netrc文件中,它就不会要求您输入用户名/密码(至少在Linux和Mac上):

machine github.com
       login <user>
       password <password>

补充说明(请参见VonC的第二个评论):在Windows上,文件名是%HOME%\_netrc

如果您想加密,请阅读VonC的第一个评论。

另外一个补充说明(参见user137717的评论),如果您使用Git 1.7.10或更新版本,可以使用以下链接中提供的方法缓存您的GitHub密码:

在Git中使用凭证助手缓存您的GitHub密码

如果您使用HTTPS克隆GitHub存储库,则可以使用凭证助手告诉Git每次与GitHub通信时记住您的GitHub用户名和密码。

这也适用于Linux、Mac和Windows。


3
可以在Linux上运行,但无法在Windows的gitbash上运行。 - Dielson Sales
4
我明白你的意思,但如果你不想把密码以明文形式输入,你可以...加密它;)请参考https://dev59.com/om435IYBdhLWcg3wkA5e#18362082。这甚至与Github的两步验证(2FA)兼容:https://dev59.com/XGYr5IYBdhLWcg3wR4Us#18607931。 - VonC
3
如果你在Windows上从DOS命令行或Git Bash中调用文件时,只要将文件名命名为%HOME%\_netrc(而非~/.netrc),就可以完美运行。参考https://dev59.com/om435IYBdhLWcg3wkA5e#18362082了解如何加密该文件。 - VonC
2
这在Linux上非常有效,特别是在VPN上使用git。 - Evan Hu
3
你不需要将其以明文或加密形式输入,辅助工具会为你缓存并且仅需30秒即可完成设置。https://help.github.com/articles/caching-your-github-password-in-git/#platform-linux - user137717
显示剩余5条评论

104

对于那些被之前的回答搞糊涂的新手,你可以这样做:

git remote -v

这将会响应出类似于以下内容:

origin    https://yourname@github.com/yourname/yourrepo.git (fetch)
origin    https://yourname@github.com/yourname/yourrepo.git (push)

那么你现在可以运行其他人建议的命令,但是现在你已经从上面知道了你的名称和存储库,所以你可以直接将yourname/yourrepo.git从上面复制并粘贴到以下位置:

git remote set-url origin git@github.com:yourname/yourrepo.git

3
我尝试了一下,每次都要求我“重新表述”。但我没有设置过。 - samayo
yourrepo 该用什么替换? - Raphi
2
@Raphi 运行 git remote -v 命令,看看会输出什么。 - Davide
2
其他答案应该在这个建议之前。 - Bennett Brown
1
仅供参考,对于组织机构下的存储库而言,其网址应为:https://github.com/orgname/yourrepo.git,而非个人账户。 - Nikhil VJ

66
如果你正在使用SSH并且私钥受到密码保护,那么当你使用Git进行网络操作(如push、pull和fetch)时,仍然会提示输入私钥的密码/口令。
使用ssh-agent来保存私钥的密码/口令凭据
如果你想避免每次输入口令,你可以使用ssh-agent在每个终端会话中保存你的私钥密码/口令凭据,正如我在我的答案中解释的无法打开与身份验证代理的连接
$ eval `ssh-agent -s`
$ ssh-add

在Windows的msysgit Bash中,您需要评估ssh-agent的输出,但我不确定在其他开发环境和操作系统中是否需要这样做。
ssh-add会在您的主目录.ssh文件夹中查找名为id_rsa的私钥,默认名称,但您可以传递文件路径以使用不同名称的密钥。
关闭ssh-agent时,您可以使用kill标志-k来终止会话。
$ ssh-agent -k

正如在ssh-agent手册中所解释的:

-k

Kill the current agent (given by the SSH_AGENT_PID environment variable).

可选超时参数

另外,它可以像这样带有可选的超时参数:

$ ssh-add -t <timeout>

<timeout>的格式为<n>h表示<n>小时,<n>m表示<n>分钟等。

根据ssh-agent手册

-t life

Set a default value for the maximum lifetime of identities added to the agent. The lifetime may be specified in seconds or in a time format specified in sshd_config(5). A lifetime specified for an identity with ssh-add(1) overrides this value. Without this option the default maximum lifetime is forever.

点击此页面查看更多时间格式

Cygwin用户的安全警告

Cygwin用户应该注意,在Cygwin中使用ssh-agent可能存在潜在的安全风险

people should be cognizant of the potential dangers of ssh-agent under Cygwin 1, though under a local netstat and remote portscan it does not appear that the port specified in /tmp/ssh-foo is accessible to anyone ...?

[1]: http://www.cygwin.com/ml/cygwin/2001-01/msg00063.html

而在引用链接处:

however, note that Cygwin's Unix domain sockets are FUNDAMENTALLY INSECURE and so I strongly DISCOURAGE usage of ssh-agent under Cygwin.

when you run ssh-agent under Cygwin it creates AF_UNIX socket in /tmp/ssh-$USERNAME/ directory. Under Cygwin AF_UNIX sockets are emulated via AF_INET sockets. You can easily see that if you'll look into /tmp/ssh-$USERNAME/agent-socket-* file via Notepad. You'll see something like

!<socket >2080

then run netstat -a and surprise! You have some program listening to port 2080. It's ssh-agent. When ssh receives an RSA challenge from the server, it refers to corresponding /tmp/ssh-$USERNAME/agent-socket-* (under Cygwin, in our case, that means it'll open connection to localhost:2080) and asks ssh-agent to process the RSA challenge with the private key it has, and then it simply passes the response received from the ssh-agent to the server.

Under Unix, such a scenario works without problems, because the Unix kernel checks permissions when the program tries to access an AF_UNIX socket. For AF_INET sockets, however, connections are anonymous (read "insecure"). Imagine, that you have the Cygwin ssh-agent running. A malicious hacker may portscan your box, locate open port used by ssh-agent, open a connection to your SSH server, receive the RSA challenge from it, send it to your ssh-agent via an open port he/she found, receive the RSA response, send it to the SSH server and voila, he/she successfully logged in to your server as you.


听起来不错,而且很详细。我负责 https credential helper,你负责 ssh 连接!+1 - VonC
太棒了。这实际上对我有所帮助,因为我的情况有点不同,即使我的远程设置为“ssh”而不是“https”,每次我发出git pushgit pull等命令时它仍然要求输入密码,而我不喜欢存储凭据,尽管这是我的个人机器(作为最佳实践)。 ssh-add的建议真的很有用。谢谢 :) - itsraghz

60

来源: 设置Git

以下命令将在一段时间内将您的密码保存在内存中(适用于 Git 1.7.10 或更新版本)。

$ git config --global credential.helper cache
# Set git to use the credential memory cache

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

2
我更喜欢使用“netrc”凭据帮助(https://dev59.com/om435IYBdhLWcg3wkA5e#18362082)来缓存*多个*凭据(而无需记住每个密码)。但是,如果您在Windows上并且想要使用内存缓存,则需要winstore(https://dev59.com/xWUp5IYBdhLWcg3wY21V#15310274)。 - VonC
10
在我看来,这是迄今为止最好的答案。 - chtenb

42
使用https进行Git pull & push时,只需为项目配置remote.origin.url,以避免每次推送时输入用户名(或/和密码)。

如何配置 remote.origin.url

URL格式:
    https://{username:password@}github.com/{owner}/{repo}
URL中的参数:
* username
可选项,需要使用的用户名。如果指定身份验证,需要时无需再次输入用户名。 不要使用电子邮件;请使用没有“@”符号的用户名,否则URL无法正确解析, * password 可选项,需要身份验证时使用的密码。 如果指定,则需要身份验证时无需再次输入密码。 提示:该值以明文形式存储,因此出于安全考虑,请勿指定此参数, *
例如:     git config remote.origin.url https://eric@github.com/eric/myproject

@更新 - 使用ssh

我认为使用ssh协议比https更好的解决方案,尽管设置步骤稍微复杂一些。

大致步骤:

  • 使用命令创建ssh密钥,例如在Linux上使用ssh-keygen,在Windows上使用msysgit提供了类似的命令。
  • 将私钥保留在本地机器的适当位置,例如~/.ssh。并通过ssh-add命令将其添加到ssh代理。
  • 将公钥上传到Git服务器。
  • 将Git仓库的remote.origin.url更改为ssh样式,例如git@gitlab.com:myaccount/myrepo.git
  • 然后拉取或推送时,再也不需要输入用户名或密码。

提示:

    如果您的ssh密钥有密码,则每次重启计算机后首次使用密钥时默认需要输入密码。

    @更新 - 在httpsssh协议之间切换。

    只需更改remote.origin.url即可,或者直接编辑repo_home/.git/config更改值(例如在Linux上使用vi)。

    通常我为每个协议添加一行,并注释其中一个使用#

    例如:

    [remote "origin"]
            url = git@gitlab.com:myaccount/myrepo.git
            # url = https://myaccount@gitlab.com/myaccount/myrepo.git
            fetch = +refs/heads/*:refs/remotes/origin/*
    

@plmday 是的,我正在使用它,我的 git 版本是 1.8.2.3 和 1.8.4,我不确定更高版本是否会对此有所改变。 - Eric
1
请注意,即使使用HTTPS,将密码放在URL中也意味着它对您和存储库之间的所有内容都是可见的。 - William
3
https://username:password@github.com/ 提供密码是安全的。参见 https://dev59.com/NG445IYBdhLWcg3wNXk_ - slowhand
1
这就是我一直在寻找的(为了完全自动化)。 - Joshua
@Joshua 这个答案有点过时了,实际上 ssh 是更好的解决方案,而且可以完全自动化,我现在都使用 ssh,除非由于网络问题无法使用。 - Eric
显示剩余4条评论

18

如果您在Github帐户上启用了双重身份验证(2FA),则常规密码无法使用于此,但您可以生成一个个人访问令牌并代替密码使用。

访问GitHub的 Settings -> Developer Settings -> Personal Access Tokens 页面(https://github.com/settings/tokens/new),并生成一个具有所有Repo权限的新令牌:

generate GitHub personal access token

页面将显示新令牌值。保存此值,并在将代码推送到GitHub上的存储库时使用它来代替密码:

> git push origin develop
Username for 'https://github.com': <your username>
Password for 'https://<your username>@github.com': <your personal access token>

1
出于安全考虑,我认为这是最好的 https 选项。存储密钥似乎比在某个地方以明文形式存储主密码更好的想法。缓存选项提供了额外的保护层,只有当前用户在您的计算机上时才允许某人进行交互,但如果有人在缓存过期之前访问了您的计算机,则仍存在潜在风险。即使发生了上述最坏的情况,您也可以从另一台计算机中删除 github 上的密钥。 - topher217
2
我认为在推送或与远程进行交互之前,您仍需要添加git config credential.helper store,否则下次与远程进行交互时,此令牌将不再存在。对吗? - topher217
1
@topher217:没错。你可以使用Git凭据助手,甚至像Mac OSX的Keychain这样的工具来存储这些令牌以供重复使用。 - Adil B
@topher 你可以考虑一下这个答案和我相应的评论(从那里向上滚动) 关于不同凭据助手的安全性。store 是最不安全的,因为它只是简单地保存(存储)内容。还有更安全的选择。 - Cadoiz

17

你可以在Git中缓存你的GitHub密码:

只需按照GitHub官方文档上的指示进行操作:官方文档链接

按照上面链接中的说明操作后,您应该能够无需每次输入用户名/密码即可推送/拉取您的存储库。


5
这是我认为这里最好的答案,应该被采纳。 - TheZuck
1
同意,引用官方文档的答案应该优先考虑。我很乐意遵循他们的指示并且更喜欢使用标准约定。 - johnnieb

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接