29

System settings: MacOS Catalina 10.15.6

> git --version
git version 2.24.3 (Apple Git-128)

file: ~/.gitconfig :

[user]
    name = nickname
    email = nickname@gmail.com
[includeIf "gitdir:~/Business"]
    path = ~/.gitconfig-work

file: ~/.gitconfig-work :

[user]
    name = First Last
    email = fl@work.com

Output when trying to check the configuration on terminal:

(base) MBP-Name:~ myname$ cd ~/Business/
(base) MBP-Name:Business myname$ git config --get user.name
nickname

I have tried both relative ~ and absolute / paths either for gitdir and path in the config file. On each change I am restarting the bash. Also, I have read several other stackoverflow questions with no success. Some of them where suggesting adding the i option for case insensitivity but it didn't fix the problem.

Any ideas ?

entropyfever
  • 445
  • 1
  • 4
  • 8
  • 29
    Is `~/Business/` a repository (working tree)? If not hint: `includeIf` works only in **repositories** under `~/Business/` but not in a non-repo directory. – phd Nov 15 '20 at 10:33
  • 10
    @phd You were totally right. I had to change ``` "gitdir:~/Business"``` to ``` "gitdir:~/Business/"``` and hit the command inside a repo folder```. Thank you <3 – entropyfever Nov 15 '20 at 10:36
  • 1
    @entropyfever That's helpful. You should add it as an answer... :D – Melvin Abraham Oct 08 '22 at 14:54
  • 1
    Took me a few hours to figure out why it doesn't work. The overridden config will only be visible when you are in a git directory, otherwise it always shows the global values. – Daniel B Feb 24 '23 at 04:23

9 Answers9

12

I tried removing the double quotes and typing it again. weirdly it worked. since I copied the text from https://blog.gitguardian.com/8-easy-steps-to-set-up-multiple-git-accounts/

Nithin P M
  • 191
  • 1
  • 7
  • 2
    This was the problem for me, the difference between the non-standard quote marks (“ ”) and standard quote marks (" "). Remove the fancy quote marks and its works – MrInvolved Jul 26 '22 at 22:45
6

I spent a lot of time trying to debug this but every time didn't work.

So I decided to delete and start fresh. This is what I did:

Create .gitconfig

[includeIf "gitdir:~/Documents/home/"]
path = ~/.gitconfig.home
[includeIf "gitdir:~/Documents/work/"]
path = ~/.gitconfig.work

Create .gitconfig.work

[user]
name = My Name
email My work email

Create .gitconfig.home

[user]
name = My Name
email My personal email

Then, I initialized git (git init) in both folders: ~/Documents/work/ and ~/Documents/home/.

After initializing everything work as expected.

I hope it helps you in case you are experiencing this issues.

Tested in Mac and Windows.

Peter
  • 2,004
  • 2
  • 24
  • 57
  • 2
    thanks for the help. the secret was `git init` on those folders – PinoSan Apr 02 '23 at 13:13
  • `git init` on the folder gets this working – lu_ke____ May 16 '23 at 22:44
  • Weirdly this isn't working for me. I had it set up with my home as default and work only applied to one area, but changed it to this setup to try to fix the issue, but no luck. All git commits have the correct user, but `git push` tries to use the wrong user. Even nuking the repo and re-cloning it didn't work. – Vala Jun 28 '23 at 09:33
  • 2
    For anyone else having a similar issue I tracked down what it was: if you have an `~/.ssh/config` file that sets a user name for your remote that will always take precedence over the git config. I.e. if you've set up an `IdentityFile` configuration for your remote, make sure you *don't* set the `User` property. – Vala Jun 28 '23 at 10:31
  • It's especially hidden as for public repositories on GitHub the user won't matter for cloning, but it will for pushing. – Vala Jun 28 '23 at 10:47
3

I myself also had issues with my .gitconfig. I read the docs a few times, searched Stackoverflow and some blogposts. I though I have tried every possible solution for it: relative paths, trailing slashes, different quotes, et cetera. Non of the solutions I tried actually worked.

I got to the point I just tried to include a file using [include], but with no success either.

After a few hours of trying and searching for answers, I came across a post that mentioned a .gitconfig to be included could also be a .txt file. I changed the files from .gitconfig-{company} to gitconfig-{company}.txt and renamed the paths in the global .gitconfig, and everything worked instantly!

I hope this works for you, and future googlers.

  • I think the `.gitconfig` file has to end in `.gitconfig`, like `.github.gitconfig`, which is what I use – mbomb007 Sep 14 '22 at 18:59
  • The global one does. But whenever I tried to import a file via the `[includeIf]`, I needed to add the `.txt` extension in order to make it work. It took me a long time to figure out, since every example said it should just be `.gitconfig-{company}`. Maybe there is a setting enabled somewhere that causes my system to only check for `.txt`. Maybe someone also struggles with that and find that the `.txt` works for them too :) – BerendPronk Sep 16 '22 at 07:05
  • Did you try `.{company}.gitconfig`? – mbomb007 Sep 16 '22 at 13:12
  • Could try that. Thanks for the insight. The examples said it didn't matter , but it seems more logical that way. I just wanted to let you guys know that a `.txt` file also suffices. – BerendPronk Sep 19 '22 at 07:10
1

TL;DR

  1. Make sure to test this includeIf business before assuming that it just works (just like anything else? ‍♀️). It seems to not work (right away) in a lot of circumstances.
  2. For some reason, git did not pick up the change until I use git config to make some random change. (Note: I am on win10)

Long Version

Firstly, make sure to look through the other answers in this post, since every post seems to contribute a different potential issue.

Now this is what happened to me (NOTE: I am on Windows 10):

  1. Open ~/.gitconfig and add the entry includeIf entry
    • e.g.:
      [includeIf "gitdir:~/x/"]
      path = ~/.x.gitconfig
      
  2. Create the custom ~/.x.gitconfig file and override some setting, e.g. set a.b = c (make sure its different from the default, so you can test it at first)
  3. NOTE: This answer claims that this won't affect old directories, but I have not tested it.
  4. Go to the directory (cd ~/x) and test. E.g. I tried git config a.b and it gave me the wrong value.
  5. I realized the folder was not yet git inited, so I ran git init, and it still did not work.
  6. Sln: I made a random local config change via git config some thing
  7. Tested again and it worked and always picked up all changes immediately right after that. It now works!
Domi
  • 22,151
  • 15
  • 92
  • 122
1

I was trying to add both users to the same gitconfig file and it wasn't working for me. So, I created a gitconfig for each user and it worked!!

~/.gitconfig

[includeIf "gitdir/i:~/Documents/Personal/"]
    path = ~/.gitconfig_personal

[includeIf "gitdir/i:~/Documents/Work/"]
    path = ~/.gitconfig_work

~/.gitconfig_personal

[user]
    name = Your Personal Name
    email = your_personal@email.com

~/.gitconfig_work

[user]
    name = Your Work Name
    email = your_work@email.com
Pedro Silva
  • 2,655
  • 1
  • 15
  • 24
1

I know this is starting to be an old question, but I had a similar problem and the solution for it was dead simple, so I'll share it in case anyone has thee same problem: the problem was simply that your dir should have a slash (/) at the end...

So in the case of the OP,

[includeIf "gitdir:~/Business"]

Should simply become

[includeIf "gitdir:~/Business/"]

Hope this helps!

0

Problem

In my case [includeIf] didn't work because my config is on the C: drive (C:\Users\User\.gitconfig), but my working projects are on the D: drive (D:\Projects\Work\).

Solution

In my case it should be:

[includeIf "gitdir/i:Work/"]

Instead of:

[includeIf "gitdir/i:~/Work/"]

Documentation

Bodix
  • 404
  • 5
  • 7
0

I was also stuck by the strange behaviors after I use [includeIf "gitdir:~/foo"], it works sometimes, and sometimes doesn't.

Therefore I digged into Git's git-config official documentation Documentation:

gitdir

The data that follows the keyword gitdir: is used as a glob pattern. If the location of the .git directory matches the pattern, the include condition is met.

The .git location may be auto-discovered, or come from $GIT_DIR environment variable. If the repository is auto discovered via a .git file (e.g. from submodules, or a linked worktree), the .git location would be the final location where the .git directory is, not where the .git file is.

I personally feel this design is bizarre though... It means the [includeIf "gitdir:~/foo"] in your ~/.gitconfig can be triggered when you cd ~/foo/existed_project && git fetch though, BUT will not be triggered when you cd ~/foo && git clone, because the directory ~/foo/.git/ is not existed...

Explicitly setting env var GIT_DIR=~/foo seems also not work (maybe because ~/foo/.git is still not existed?).

As far as I know, a simpler workaround to solve this is to ~/foo && git init... I don't know if there's a better or canonical practice after DuckDuckGoing for hours.

kuanyui
  • 782
  • 13
  • 23
-1

This will only affect future projects created with git init. Previous projects will remain unchanged

webxmsj
  • 7
  • 3
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/33101923) – Thomas Smyth - Treliant Nov 10 '22 at 09:52
  • 1
    Incorrect information – Matt Bracewell Mar 15 '23 at 12:01