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

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

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

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


当前回答

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

其他回答

一个常见的原因是使用默认(HTTPS)而不是SSH进行克隆。您可以通过转到存储库,单击“克隆或下载”,然后单击URL字段上方的“使用SSH”按钮,然后更新源远程的URL,如下所示:

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

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

git remote -v

GitHub中记录了这一点:将远程URL从HTTPS切换到SSH。

如果您在Github帐户上启用了2FA,那么您的常规密码将无法用于此目的,但您可以生成一个个人访问令牌并将其替换为使用。

访问GitHub中的设置->开发人员设置->个人访问令牌页面(https://github.com/settings/tokens/new),并生成具有所有Repo权限的新令牌:

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

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

使用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 credential.helper store
git config --global credential.helper 'cache --timeout 7200'
 # gen  the pub and priv keys
 # use "strange" naming convention, because those WILL BE more than 10 ...
 ssh-keygen -t rsa -b 4096 -C "me@corp.com" -f ~/.ssh/id_rsa.me@corp.com@`hostname -s`

 # set the git alias ONLY this shell session
 alias git='GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa.me@corp.com.`hostname -s`" git'

 # who did what when and why
 git log --pretty --format='%h %ai %<(15)%ae ::: %s'

 # set the git msg
 export git_msg='issue-123 my important commit msg'

 # add all files ( danger !!! ) and commit them with the msg
 git add --all ; git commit -m "$git_msg" --author "Me <me@corp.com"

 # finally 
 git push

对于Windows Git用户,在运行Git-config--globalcredential.helper store后,如果仍然提示输入密码,最好使用以下命令检查配置文件的写入位置

git config --list --show-origin

在我的例子中,在手动编辑配置文件“C:\Program Files\Git\mingw64\etc\gitconfig”并添加以下文本后,它就工作了。

[credential]
    helper = store