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

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

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

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


当前回答

使用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'

其他回答

一个常见的原因是使用默认(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。

HTTPS更新:

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

要使用:

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

使用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'

对于那些对前面的答案感到困惑的新手,你可以这样做:

git remote -v

它会做出如下反应

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

然后,您可以运行许多其他人建议的命令,但现在您可以从上面知道您的名字和您的repo,因此您可以将上面的name/yourrepo.git剪切并粘贴到:

git remote set-url origin git@github.com:yourname/yourrepo.git
 # 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