"git config -l" 显示我的用户名/电子邮件已设置,但 git push 要求输入用户名。

3

假设我的电子邮件和用户名都包含在~/.gitconfig文件中:

$cat ~/.gitconfig
[user]
    name = MyFirst MyLast
    email = myemail@gmail.com

git config -l“看到”它们:

$git config -l
user.name=MyFirst MyLast
user.email=myemail@gmail.com

为什么git push无法识别它们?
 $git push origin master
Username for 'https://github.com': 

以下是更多信息:

状态:

git status
On branch master
Your branch is based on 'origin/master', but the upstream is gone.
  (use "git branch --unset-upstream" to fixup)
Untracked files:
  (use "git add <file>..." to include in what will be committed)

远程:

git remote -v
origin  https://github.com/<owner>/<repo> (fetch)
origin  https://github.com/<owner>/<repo> (push)

Git不会假设你在~/.gitconfig中的名字与你的GitHub用户名相同。Git软件是一个独立于GitHub网站的东西。我建议将SSH密钥添加到您的GitHub帐户中,这样您就不必每次推送时输入用户名/密码。 - Adrian
2个回答

4

这是两个完全不相关的数据部分

您配置中的用户名用于设置提交作者等事项。

当您在推送到远程时提示输入用户名时,那是另一回事。那些是身份验证凭据。

Git不会假设配置的用户名与身份验证凭据的用户名相同。

这是一件好事。例如,一个非常真实的例子:

我在Git中的配置用户名Thomas Stringer

我的GitHub用户名tstringer

如果Git默认它们相同,那对我来说将会非常烦人,需要覆盖或重新配置。


1
是的,我经常忘记在使用 ~/.ssh/config 时使用 ssh。git remote set-url origin ssh://git@github.com/<owner>/<repo> - WestCoastProjects

1
这是对@Thomas Stringer答案的评论/补充:
处理此类情况的方法是使用ssh(与~/.ssh/config一起使用)。
git remote set-url origin ssh://git@github.com/<owner>/<repo>

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