35

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
  • 713
  • 2
  • 8
  • 20

1 Answers1

24

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. The final slash is important as well; don't omit it without understanding what it does)

torek
  • 448,244
  • 59
  • 642
  • 775
  • 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
  • My issue was that Ubuntu's default Git version was 2.11 instead of 2.13+. – Christopher Peisert Jun 17 '22 at 23:21
  • 4
    I'm working on macOS, and it turns out you need to end your path with trailing `/`, in my case instead of `[includeIf "gitdir:~/Work"]` I had to do `[includeIf "gitdir:~/Work/"]` – Forsaken Jul 18 '22 at 14:58
  • 2
    @Forsaken: all the examples end with a slash. The documentation states that when you use a `pattern [that] ends with /, ** will be automatically added` (see [the `git config` documentation](https://git-scm.com/docs/git-config) and search for the gitdir directive in includeIf directives), so if you want to match things *under* ~/Work you will need either the trailing slash (to get Git to add `**`) or `/**`. But to match one *particular* repository in ~/Work you could spell it out. – torek Jul 18 '22 at 17:51
  • @torek I probably didn't noticed that or thought it was ambiguous. Thanks for explanation how git treats trailing `/`! – Forsaken Aug 04 '22 at 11:09
  • Thanks a lot @Forsaken as it is a detail easy to miss – MVCDS Nov 10 '22 at 10:03