19

I've been googling about this, trying different methods from different sources... but it doesn't work.

I want to push commits with my personal profile (defined with "git config --global user.email ...") unless I'm on my work folder.

The content of my .gitconfig is located at C:/Users/my-user/

[user]
    name = Personal
    email = personal@personal.com

[includeIf "gitdir: F:/Work/CompanyName/"]
    path = F:/Work/CompanyName/.gitconfig-work
    

Content of my .gitconfig-work is located at F:/Work/CompanyName/

[user]
    name = Work
    email = work@work.com

When I go to a cloned repository from work located at:

F:/Work/CompanyName/Project

I use:

git config --show-origin --get user.email

And it shows:

file:C:/Users/<my-user>/.gitconfig

Instead of the route I defined to work.

Thanks for your help.

✨ For the future googlers

I created a gist explaining the steps.

Icaruk
  • 271
  • 4
  • 12

1 Answers1

15

You need to remove one space in:

[includeIf "gitdir: F:/Work/CompanyName/"]

i.e., make this read:

[includeIf "gitdir:F:/Work/CompanyName/"]

Note the lack of a blank between the colon after gitdir and the path name to be tested.

(Your Git also needs to be at least version 2.13. You might consider using gitdir/i:f:/work/companyname/ so that you are not case-sensitive here, assuming your host system is also not case-sensitive, as the F: part suggests.)

torek
  • 389,216
  • 48
  • 524
  • 664
  • The last tutorial site I visited had space after gitdir, I thougth It was not important. Thanks :) – Icaruk May 01 '20 at 14:21
  • When I cloned already the repo with a tool(sourcetree), and check the email, it worked. But, when I want to clone a repo from company, `git config user.email` shows my personal email and I can not access to company repo. – Dr.jacky Aug 05 '20 at 07:58
  • @Dr.jacky: when you run `git clone`, you are in effect running a series of separate Git commands, starting with `mkdir` and `git init`, then `git remote add`, then `git fetch`, and so on. There might be some bug(s) in the code that combines all of this into one process, that forgets to include something here. If you can set up a reproducer you can send a bug report to the Git folks. – torek Aug 05 '20 at 16:40
  • Note, however, that `user.email` is *not* used for *authentication*. Authentication is done entirely outside of Git itself. If you're having trouble accessing a company repository, this is usually due to an authentication setting. – torek Aug 05 '20 at 16:42
  • 1
    @torek Even after following this successfully: https://stackoverflow.com/a/17158985/421467, I can not clone a new repo under an empty folder, under CompanyFolder. I sent an email to the mailing list. – Dr.jacky Aug 06 '20 at 09:26