git insteadOf and includeIf interaction
Have you ever set your gitconfig with insteadOf like this?
$ cat ~/.config/git/config
[url "git@github.com"]
insteadOf = "gh"
You can clone without fully typing "git@github.com", and it would resolve the remote URL like usual.
$ git clone gh:torvalds/linux
$ git remote -v
origin git@github.com:torvalds/linux
I've been set it up like that for years. All this time, I thought it happens only once when I do git clone. Today, when I'm playing with the new includeIf, it doesn't works as I expected.
https://github.com/git/git/commit/399b198489a041e2842fb4b257bea5adb02d28d1
The optional config file never got included. I tried changing both git config and git remote to http format, and it works. One curious "cat .git/config" on my repository later, I found that git store the remote using the original insteadOf url.
$ cat .git/config
[remote "origin"]
url = "gh:torvalds/linux"
And since includeIf checks config file, it doesn't match to any of the remote. Now, I've changed it to the correct one, and everything works as expected.
$ cat ~/.config/git/config
[includeIf "hasconfig:remote.*.url:gh:*/**"]
path = github
You may also want to keep 3 kind of includeIf, in case you forgot to use one or the other.
$ cat ~/.config/git/config
[includeIf "hasconfig:remote.*.url:https://github.com/torvalds/*"]
path = github
[includeIf "hasconfig:remote.*.url:git@github.com:torvalds/*"]
path = github
[includeIf "hasconfig:remote.*.url:gh:torvalds/*"]
path = github
One use cases is if you want to have different commit author detail set up for different repo. This is how I set up my git config.
$ cat ~/.config/git/config
[url "git@git.sr.ht:~"]
insteadOf "hut:"
[url "git@gitlab.com:"]
insteadOf "lab:"
[includeIf "hasconfig:remote.*.url:hut:bentinata/*"]
path = personal
[includeIf "hasconfig:remote.*.url:lab:workplace/*"]
path = work
$ cat ~/.config/git/personal
[user]
email = bentinata@personal-domain.example
$ cat ~/.config/git/work
[user]
email = ben.nata@workplace.com
Now I can clone without typing as much, and still have different committer email for personal and work repositories when I move between projects.
Please leave your comment as this toot replies:
https://fosstodon.org/@bentinata/110351404712679455
View this page on gemini! (Same content, just different protocol.)