【问题标题】:Use current repo's user.email for commits in VS Code在 VS Code 中使用当前 repo 的 user.email 提交
【发布时间】:2019-03-12 23:15:04
【问题描述】:

我将 Git 与两个不同的电子邮件/个人资料一起使用,结果是:

git config user.email >> myRepoEmail@address.com
git config --global user.email >> myGenericEmail@address.com

如果我使用 VS Code 并打开终端,我的提交使用 repo 电子邮件地址;这是有道理的,因为我有效地使用了 Git(而不是 VSCode)。但是,如果我使用源代码管理 Git 侧边栏并键入提交消息、Ctrl+Enter 提交等,那么它使用全局电子邮件 (myGenericEmail@address.com) 提交,即使我在一个具有不同设置。

有没有办法告诉 VS Code 使用 repo 的设置进行提交,而不是调用 --global 进行提交?

[编辑来自 cmets 的信息]
我想我应该提到我的 gitconfig 中有命令

[includeIf "gitdir:C:/Users/myname/privateRepoFolder"] 
    path = ~/gitconfig-private 

在那个备用 gitconfig-private 中,我有

[user] 
    email = myRepoEmail@address.com

【问题讨论】:

  • 我似乎无法重现这一点。 git config --local --get user.email 的输出是什么?
  • 在我的环境中,--local 输出 repo 的电子邮件地址,当我使用源代码控制面板提交时,VS Code 会尊重该地址。当我删除 --local user.email 时,VS Code 将恢复为 --global user.email
  • 大多数时候,我只是使用我的全局设置,只有对于特殊项目/repos,我才需要设置特殊的--localuser.* 设置。我要补充一点,我在 Ubuntu/MacOS 上,还没有在 Windows 上检查你的问题,你似乎在。
  • @Gino 成功了。随意添加作为答案,我会将其标记为已解决。

标签: git visual-studio-code vscode-settings


【解决方案1】:

我终于有机会在 Windows 上进行检查了。

来自您的 cmets:

我想我应该提到我的 gitconfig 命令 [includeIf "gitdir:C:/Users/myname/privateRepoFolder"]path = ~/gitconfig-private,在那个备用 gitconfig-private 中,我有 [user] email = myRepoEmail@address.com.

在 Windows 上,includeIf conditional include 对路径有点特殊。

首先,确保以/ 结束路径以指定目录,如他们的示例中所示:

; include for all repositories inside /path/to/group
[includeIf "gitdir:/path/to/group/"]
    path = /path/to/foo.inc

所以你应该使用:

[includeIf "gitdir:C:/Users/myname/privateRepoFolder/"] 
    path = ~/gitconfig-private

我发现它也可以通过以 .git 文件结束路径来工作(更明确地说)。

[includeIf "gitdir:C:/Users/myname/privateRepoFolder/.git"] 
    path = ~/gitconfig-private

其次,对于VSCode,只有在使用不区分大小写的路径匹配(gitdir/i)时才有效:

[includeIf "gitdir/i:C:/Users/myname/privateRepoFolder/"] 
    path = ~/gitconfig-private

这种不区分大小写的行为是 VSCode 的已知行为。
查看报告的问题Git integration does not correctly support includeIf directive

我尝试从 VSCode 提交,但未能获取 includeIf 配置。然后我从命令行运行包装器,它确实选择了 向上 includeIf。然后我检查了日志:

在 VSCode 中:

config --get-all user.name in c:\Users\zhuowei\Documents\repos\includeIf

外面:

config --get-all user.name in C:\Users\zhuowei\Documents\repos\includeIf

注意大小写的区别。

【讨论】:

  • 太棒了,谢谢。我觉得“已知行为”是一个错误,但人们似乎满足于强制不区分大小写。也许题外话,但这样的事情不是一个快速的解决方案吗?无论如何,它会阻止像我这样的人在 SO 上提出这个问题。
  • 从问题页面的讨论中,有人确实为 VS Code (git: fix includeIf on Windows by uppercasing drive letter) 提交了 PR,它似乎只需要几行,但遗憾的是它是 not merged
【解决方案2】:

With Git 2.8 or more,其实我会推荐:

git config --global user.useConfigOnly true

然后启动 VSCode。
这样,如果由于任何原因,VSCode 没有在任何本地存储库中找到您的 user.name/user.email 配置,则 VSCode 无法默认使用通用电子邮件:它会 询问您是谁在那个仓库里。

【讨论】:

  • 它仍然没有解决问题。当我这样做时,VS Code 很乐意提交。奇怪的是,如果我从 --global 配置中删除包含我的电子邮件的行,那么 VS Code 会抱怨没有设置电子邮件,尽管我仍然可以从终端提交,因为它检测到我的 privateRepo 电子邮件地址。请注意,我没有设置git config user.email myPrivateEmail@a.com,而是使用conditionalIf "gitdir:C:/Users/myPrivateRepos" --> path = ~/.gitconfig-private,如果目录位于该文件夹中,则使用新电子邮件覆盖全局电子邮件。
  • 不知何故,VS Code 和 Git(来自终端)似乎使用了两种略有不同的 Git 调用。我认为是同一个程序在运行,所以可能只是 VS Code 使用的调用略有不同,虽然我不知道如何。
  • @AlexG 的想法是不要在git config -l --show-origin 中设置任何电子邮件 => 然后此配置将强制您在正确的仓库中设置正确的电子邮件。
  • 哦....是的,但我试图避免这种情况。我真的很喜欢 Git conditionalIf 方法,并且看到它在命令行 Git 上工作,我预计应该有一个原因它在 VS Code 中不起作用。
  • @AlexG 你的意思是 includeIf 吗? (stackoverflow.com/a/43621480/6309) 它应该可以使用 Git 2.13 或更高版本。
猜你喜欢
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 2013-10-13
  • 1970-01-01
  • 1970-01-01
  • 2016-09-25
  • 1970-01-01
相关资源
最近更新 更多