我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
我从我的GitHub帐户克隆了一个Git存储库到我的电脑。
我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。
当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!
我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?
当前回答
除了更改为SSH之外,如果您不介意以明文形式输入密码,您还可以继续使用HTTPS。把这个放到你的~/.netrc中,它不会要求你输入用户名/密码(至少在Linux和Mac上是这样):
machine github.com
login <user>
password <password>
添加(请参见VonC的第二条评论):在Windows上,文件名为%HOME%\_netrc。
如果您想加密,请阅读VonC的第一条评论。
如果您有Git 1.7.10或更高版本,则可以使用另一个插件(请参见user137717的注释)。
使用凭据助手在Git中缓存GitHub密码:
如果要使用HTTPS克隆GitHub存储库,可以使用凭据助手,告诉Git记住您的GitHub用户名和密码。
这也适用于Linux、Mac和Windows。
其他回答
当您使用https进行Git推送时,只需为项目配置remote.origin.url,以避免每次推送时输入用户名(或/和密码)。
如何配置remote.origin.url:
URL format: https://{username:password@}github.com/{owner}/{repo} Parameters in URL: * username
Optional, the username to use when needed.
authentication, if specified, no need to enter username again when need authentication. Don't use email; use your username that has no "@", otherwise the URL can't be parsed correctly, * password optional, the password to use when need authentication. If specified, there isn't any need to enter the password again when needing authentication. Tip: this value is stored as plain text, so for security concerns, don't specify this parameter, * e.g 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密钥有密码短语,则默认情况下,您需要在每次重新启动计算机后首次使用密钥时输入密码。
@更新-在https和ssh协议之间切换。
只需更改remote.origin.url就足够了,或者您可以直接编辑repo_home/.git/config来更改值(例如在Linux上使用vi)。
通常我为每个协议添加一行,并使用#注释掉其中一行。
E.g.
[remote "origin"] url = git@gitlab.com:myaccount/myrepo.git # url = https://myaccount@gitlab.com/myaccount/myrepo.git fetch = +refs/heads/*:refs/remotes/origin/*
对我有用的是编辑.git/config并使用
[remote "origin"]
url = https://<login>:<password>@gitlab.com(...).git
不用说,这是一种不安全的密码存储方式,但在某些环境/情况下,这可能不是问题。
检查您的git版本和更新。然后将解决问题
$git更新windows的git
更新说明:当您的PC git版本和服务器git版本不匹配时,我们可以使用此选项。
您可以在Git中缓存GitHub密码:
只需遵循GitHub官方文档中的说明即可。
按照上述链接中的说明进行操作后,您应该可以在不每次输入用户名/密码的情况下,向存储库推送/从存储库中推送。
这就是对我有用的:
git remote set-url origin https://username@github.com/username/reponame.git
例子:
git remote set-url origin https://jsmith@github.com/jsmith/master.git