2

I use Git with two different emails / profiles, resulting in:

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

If I use VS Code and open a terminal, my commits use the repo email address; this makes sense because I am effectively using Git (not VSCode). However, if I use the Source Control Git sidebar and type a commit message, Ctrl+Enter to commit, etc., then it commits using the global email (myGenericEmail@address.com), even though I am working in a repo which has a different setup.

Is there a way to tell VS Code to use the repo's settings for commits, rather than invoking --global for commits?

[EDIT in info from comments]
I suppose I should mention I have in my gitconfig the command

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

and in that alternate gitconfig-private, I have

[user] 
    email = myRepoEmail@address.com
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
Alex G
  • 747
  • 4
  • 15
  • 27
  • I can't seem to reproduce this. What is the output of `git config --local --get user.email` ? – Gino Mempin Mar 13 '19 at 04:22
  • `--global` command outputs `myGenericEmail@address.com`. Omitting it outputs `myRepoEmail@address.com`. But using `--local` outputs nothing. I suppose I should mention I have in my `gitconfig` the command `[includeIf "gitdir:C:/Users/myname/privateRepoFolder"] path = ~/gitconfig-private`, and in that alternate `gitconfig-private`, I have `[user] email = myRepoEmail@address.com`. – Alex G Mar 13 '19 at 21:29
  • ^That is respected by Git, and is the reason why my repo has `user.email` set to `myRepoEmail` even though I never explicitly set it (using `git config user.email myRepoEmail@address.com`). However, it doesn't seem that VS Code is reading it the same way as Git does. – Alex G Mar 13 '19 at 21:31
  • 1
    On my env, `--local` outputs the repo's email address, which is respected by VS Code when I commit using the Source Control panel. When I remove the `--local` `user.email`, then VS Code reverts to the `--global` `user.email`. – Gino Mempin Mar 14 '19 at 00:21
  • Right, so I guess the issue is that VS Code doesn't follow the `conditionalIf` that Terminal Git otherwise respects? Seems like a bug to me. Also, do you manually set the `--local` email address on each repo, or is there a way to do this by default? Seems tedious to do it every time. (But it sounds like you *do* do this on your repos.) – Alex G Mar 14 '19 at 22:07
  • 1
    Most of the time, I just use my global settings, and only for special projects/repos do I need set special `--local` `user.*` settings. I would add that I'm on Ubuntu/MacOS, haven't checked your issue yet on Windows, which you seem to be on. – Gino Mempin Mar 14 '19 at 23:54
  • 1
    Have you checked this? [Git integration does not correctly support `includeIf` directive, again](https://github.com/Microsoft/vscode/issues/62921) – Gino Mempin Mar 15 '19 at 00:16
  • 1
    @Gino That worked. Feel free to add as an answer and I'll mark it solved. – Alex G Mar 18 '19 at 04:59

2 Answers2

2

With Git 2.8 or more, I would actually recommend:

git config --global user.useConfigOnly true

And then launch VSCode.
That way, VSCode could not default to a generic email if, for any reason, it does not find your user.name/user.email configured in any local repo: it would have to ask you who you are in that repo.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • It still doesn't fix the issue. When I do this, VS Code is happy to commit. Oddly enough, if I delete the line containing my email from the `--global` config, then VS Code complains that no email is set, although I can still commit from the terminal because it detects my `privateRepo` email address. Note that I don't have `git config user.email myPrivateEmail@a.com` set, but instead I am using a `conditionalIf "gitdir:C:/Users/myPrivateRepos" --> path = ~/.gitconfig-private` which overrides the global email with a new one if the directory is in that folder. – Alex G Mar 13 '19 at 21:39
  • Somehow it seems that VS Code and Git (from terminal) are using two slightly different invocations of Git. I think it's the same program running, so it's probably just that the calls that VS Code uses are different slightly, though I don't know how. – Alex G Mar 13 '19 at 21:40
  • @AlexG the idea is to not have any email set in `git config -l --show-origin` => Then this config will force you to set the right email in the right repo. – VonC Mar 13 '19 at 21:51
  • Oh.... Yeah, but I was trying to avoid that. I really like the Git conditionalIf method, and seeing as it works on the command line Git, I expected there should be a reason it wouldn't work in VS Code. – Alex G Mar 14 '19 at 22:04
  • @AlexG Do you mean includeIf? (https://stackoverflow.com/a/43621480/6309) It should work, with Git 2.13 or more. – VonC Mar 15 '19 at 07:49
2

I finally had a chance to check on Windows.

From your comments:

I suppose I should mention I have in my gitconfig the command [includeIf "gitdir:C:/Users/myname/privateRepoFolder"] path = ~/gitconfig-private, and in that alternate gitconfig-private, I have [user] email = myRepoEmail@address.com.

On Windows, the includeIf conditional include is a little particular about the path.

First, make sure to end the path with a / to specify a directory, like in their example:

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

So you should use:

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

I found that it also works by ending the path with the .git file (to be more explicit).

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

Second, for VSCode, it only works if you use case-insensitive path matching (gitdir/i):

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

This case-insensitive behavior is a known behavior of VSCode.
See the reported issue Git integration does not correctly support includeIf directive:

I tried to commit from VSCode, which failed to pick up the includeIf config. Then I ran the wrapper from the command line, and it did pick up the includeIf. I then checked the log:

In VSCode:

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

Outside:

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

Note the difference in the case letter.

Gino Mempin
  • 25,369
  • 29
  • 96
  • 135
  • Awesome, thanks. I feel like the "known behavior" is a bug but people seem content with forcing case-insensitivity. Maybe off-topic, but isn't something like this a quick fix? At any rate it would prevent people like me from asking this question on SO. – Alex G Mar 19 '19 at 03:06
  • 1
    From the discussion on the issues page, someone did submit a PR for VS Code ([git: fix includeIf on Windows by uppercasing drive letter](https://github.com/Microsoft/vscode/pull/50376)) and it seemed to only require a few lines, but sadly it was [not merged](https://github.com/Microsoft/vscode/pull/50376#issuecomment-421297324). – Gino Mempin Mar 19 '19 at 03:36