【问题标题】:Git commits with correct user from terminal but not from VS CodeGit 从终端使用正确的用户提交,但不是从 VS Code 提交
【发布时间】:2020-04-25 19:48:36
【问题描述】:

关于如何为 Git 使用多个身份的问题已经被问过好几次了……我阅读了一些说明并按照这个

https://itnext.io/setup-git-with-multiple-configs-9b4111d6928c

我在家里使用 Linux 和 Github。在工作中,我使用的是 Windows 10 和 Gitlab。工作需要两个 Git 身份。

我更改了位于

.gitconfig

C:\Users\jdoe

来自

[user]
    name = johndoe
    email = johndoe@gmail.com
[credential]
    helper = store
[winUpdater]
    recentlySeenVersion = 2.24.1.windows.2
[core]
    editor = \"C:\\Users\\jdoe\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" --wait

[includeIf "gitdir:~/Desktop/Repositories/Gitlab/"]
    path = ~/Desktop/Repositories/Gitlab/.gitconfig
[includeIf "gitdir:~/Desktop/Repositories/Github/"]
    path = ~/Desktop/Repositories/Github/.gitconfig
[credential]
    helper = store
[winUpdater]
    recentlySeenVersion = 2.24.1.windows.2
[core]
    editor = \"C:\\Users\\jdoe\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" --wait

接下来我在

创建了一个目录Gitlab

C:\Users\jdoe\Desktop\Repositories\Gitlab

和一个目录 Github

C:\Users\jdoe\Desktop\Repositories\Github

both 目录中,我创建了一个 .gitconfig。对于 Gitlab,我使用我的 Gitlab 凭据(工作帐户)

[user]
    name = jdoe
    email = john.doe@company.com

对于 Github,我使用我的私人帐户凭据

[user]
    name = johndoe
    email = johndoe@gmail.com

我使用演示存储库测试了这两个平台。提交两个平台时,都会收到来自未知不存在用户的用户名

的提交

母狗

我不知道这个用户名来自哪里。我希望 Gitlab 显示用户名 jdoe 和 Github 显示用户名 johndoe

我还尝试使用本指南删除所有通用凭据

https://stackoverflow.com/a/41968176/9945420

这样做后,系统提示我输入 Github 凭据。不幸的是,Github 和 Gitlab 仍然收到来自一个名为 Doe 的不存在用户的提交。

任何帮助将不胜感激


更新:

我在我的私人 Linux 机器上测试了这种方式,它工作正常。但对于我的 Windows 10 工作机器,它没有。


第二次更新:

我在没有使用 VS Code 的情况下对此进行了测试。通过终端提交时,它也适用于我的 Windows 10 机器...

当我在 VS Code 中打开终端窗口时,修改文件并运行

$ git add .
$ git commit -m "test"
$ git push

它正确提交。当我通过 VS Code Git 集成提交并运行时

$ git push

稍后,它使用不存在的用户提交。

【问题讨论】:

  • 你有用户名“Doe”的 GitHub / GitLab 帐户吗?两者都将显示拥有提交电子邮件 ID 的帐户的名称(如果存在),而不是显示提交中使用的名称。如果不存在注册电子邮件“john.doe@company.com”或“johndoe@gmail.com”的帐户,则将显示提交中的名称。
  • 不,我没有该用户名的帐户:/
  • 对于 Gitlab,我使用公司 Active Directory 用户登录
  • 您在更改凭据之前是否进行过任何提交?如果是这样,您必须使用自己的电子邮件地址更新您的提交。
  • 另外,目录中的配置文件不应该是.git/config而不是.gitconfig吗?

标签: git github visual-studio-code gitlab


【解决方案1】:

所以我通过将全局 .gitconfig 文件更改为

解决了这个问题
[includeIf "gitdir/i:~/Desktop/Repositories/Gitlab/"]
    path = ~/Desktop/Repositories/Gitlab/.gitconfig
[includeIf "gitdir/i:~/Desktop/Repositories/Github/"]
    path = ~/Desktop/Repositories/Github/.gitconfig
[credential]
    helper = store
[winUpdater]
    recentlySeenVersion = 2.24.1.windows.2
[core]
    editor = \"C:\\Users\\jdoe\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe\" --wait

/i 有助于区分大小写。 ~ 以用户目录的相对路径开头。

有一些答案提供了这种方法

https://stackoverflow.com/a/55107009/9945420

https://github.com/Microsoft/vscode/issues/62921

现在 VS Code 使用正确的 git 身份提交。


更新

在我的 Linux 机器上,我不必提供 /i,所以这似乎是与 Windows 相关的问题。

【讨论】:

    猜你喜欢
    • 2019-06-17
    • 2020-01-27
    • 1970-01-01
    • 2022-10-15
    • 1970-01-01
    • 2012-11-10
    • 1970-01-01
    • 2023-03-16
    • 2017-07-25
    相关资源
    最近更新 更多