我从我的GitHub帐户克隆了一个Git存储库到我的电脑。

我想使用我的电脑和笔记本电脑,但只有一个GitHub帐户。

当我尝试使用我的电脑推送到GitHub或从GitHub中拉取时,它需要用户名和密码,但当我使用笔记本电脑时不需要!

我不想每次与origin交互时都键入用户名和密码。我在这里缺少什么?


当前回答

HTTPS更新:

GitHub为Windows推出了一个新程序,在您使用HTTPS时存储您的凭据:

要使用:

从此处下载程序运行程序后,它将编辑.gitconfig文件。重新检查它是否编辑了正确的.gitconfig,以防出现多个错误。如果它没有编辑正确的文件,请将以下内容添加到.gitconfig中[凭据]助手=!'C: \\Path\\To\\Your\\Downloaded\\File\\git credential winstore.exe'注意[credential]后面的换行符。这是必需的。打开命令行客户端,尝试git push origin master一次。如果它要求您输入密码,请输入密码,您就完成了。密码已保存!

其他回答

如果您克隆了HTTPS而不是SSH,并且在pull、push和fetch时遇到用户名和密码提示问题。您可以简单地为UBUNTU解决此问题

步骤1:移动到根目录

cd ~/

创建文件.git凭据

使用用户名密码和githosting URL将此内容添加到该文件

https://user:pass@example.com

然后执行命令

git config --global credential.helper store

现在,您将能够轻松推送并从回购中获取所有详细信息。

截至2021,HTTPS远程有一个安全、用户友好的跨平台解决方案。不再输入密码!不再有SSH密钥!不再有个人访问令牌!

安装GitHub开发的Git凭据管理器(下载)。它支持对GitHub、BitBucket、Azure和GitLab的无密码浏览器内OAuth认证。这意味着您可以在GitHub和其他平台上启用双因素身份验证,大大提高了帐户的安全性。

推送时,您可以选择身份验证方法:

> git push
Select an authentication method for 'https://github.com/':
  1. Web browser (default)
  2. Device code
  3. Personal access token
option (enter for default): 1
info: please complete authentication in your browser...

在Linux上,需要一点点设置。以下内容将凭据缓存在内存中20小时,因此您每天最多只能进行一次身份验证。

git-credential-manager-core configure
git config --global credential.credentialStore cache
git config --global credential.cacheoptions "--timeout 72000"

熟悉gnomekeyring或KWallet的高级用户可能更喜欢将凭证存储更改为libsecret。

外观配置(文档):

首选在终端而不是GUI中选择身份验证方法(点击次数较少)始终使用浏览器方法,而不是每次都被询问(甚至更少的按键)

git config --global credential.guiPrompt false
git config --global credential.gitHubAuthModes browser

如果SSH密钥或.netrc文件不适用于您,那么另一个简单但不太安全的解决方案可能适用于您:git credential store-Helper将凭据存储在磁盘上:

git config --global credential.helper store

默认情况下,凭据将保存在文件~/.git凭据中。它将被创建并写入。

请注意,使用此助手将在磁盘上存储未加密的密码,仅受文件系统权限的保护。如果这可能不是可接受的安全权衡。

还有一个选项:

而不是写作

git push origin HEAD

你可以写:

git push https://user:pass@yourrepo.com/path HEAD

显然,对于大多数shell,这将导致密码被缓存在历史中,所以请记住这一点。

当您使用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/*