{"source": [{"type": "entry", "author": {"type": "card", "name": "Jamie Tanna", "photo": "https://avatars.webmention.io/www.jvt.me/f0f487b73880b54411ec067322d1d32edcb4b125853353123e076183d1131acf.png", "url": "https://www.jvt.me"}, "url": "https://www.jvt.me/mf2/2021/12/yupzj/", "published": "2021-12-23T17:02:50", "wm-received": "2021-12-23T17:14:27Z", "wm-id": 1321673, "wm-source": "https://www.jvt.me/mf2/2021/12/yupzj/", "wm-target": "https://fundor333.com/post/2021/advance-git-config-and-ssh-config/", "wm-protocol": "webmention", "like-of": "https://fundor333.com/post/2021/advance-git-config-and-ssh-config/", "wm-property": "like-of", "wm-private": false, "rels": {"canonical": "https://www.jvt.me/mf2/2021/12/yupzj/"}}, {"type": "entry", "author": {"type": "card", "name": "benji", "photo": "https://avatars.webmention.io/benji.dog/98f50ccee2f72b224906440e0a78cc1fa5ea016ae65e5fa0d42ff5000ad29d24.jpg", "url": "https://benji.dog"}, "url": "https://www.benji.dog/articles/git-config/", "published": "2024-11-22T00:00:00", "wm-received": "2024-11-23T05:32:56Z", "wm-id": 1861724, "wm-source": "https://www.benji.dog/articles/git-config/", "wm-target": "https://fundor333.com/post/2021/advance-git-config-and-ssh-config/", "wm-protocol": "webmention", "summary": {"content-type": "text/plain", "value": "This may be overkill, but it works on my machine"}, "content": {"html": "
\n

Note: I've had this post drafted for 3 YEARS!!! It's finally time to publish it.

\n
\n

I like to mess with my dotfiles and every so often, I find out about a new way to do things and I spend more time than I should learning how to use it.

\n

A few years ago I learned about includeIf for including specific files if some condition was met for git. The example that I first saw was doing:

\n
[includeIf \"gitdir:~/code/**\"]\n  path = ~/.config/git/personal\n[includeIf \"gitdir:~/work/**\"]\n  path = ~/.config/git/work\n
\n

So that ~/.config/git/personal is only included for git directories under ~/code and ~/.config/git/work is only included for directories under ~/work. The contents of those included files varies but usually it contains your git identity, signing keys, etc. Here's an example of what that could look like:

\n
[user]\n  name = benji\n  email = benji@work.com\n  signingkey = ~/.ssh/work.id_ed25519.pub\n
\n

That works pretty well but I usually organize all my code in ~/workspace regardless of whether its personal, work-1, work-2, etc. I wanted to be able to configure git depending on where that repo actually lives instead of where the directory is in my machine. Then I found out about hasconfig:remote.*.url:!

\n

This makes it so that I can configure git conditionally if the given remote URL exists for that directory I'm currently working in.

\n

A few examples of what I do is:

\n
[includeIf \"hasconfig:remote.*.url:git@github.com:orgname/**\"]\n  path = ~/.config/git/config-gh-org\n\n[includeIf \"hasconfig:remote.*.url:git@github.com:*/**\"]\n  path = ~/.config/git/config-gh\n\n[includeIf \"hasconfig:remote.*.url:git@gitlab.com:*/**\"]\n  path = ~/.config/git/config-gl\n\n[includeIf \"hasconfig:remote.*.url:git@git.sr.ht:*/**\"]\n  path = ~/.config/git/config-srht\n
\n

Now if I'm in a directory where the remote matches github.com:orgname/** it would use ~/.config/git/config-gh-org, otherwise it uses the general config file for any other GitHub repo.

\n

While that handles git identities, I still need to configure SSH keys separately to be able to pull and push to remotes. The simple version of my ~/.ssh/config looks like this:

\n
Host gitlab.com\nHostname gitlab.com\nUser git\nIdentityFile ~/.ssh/gitlab.id_ed25519\n\nHost github.com\nHostname github.com\nUser git\nIdentityFile ~/.ssh/github.id_ed25519\n
\n

The only problem with this is that in order to use a different IdentityFile for the same Hostname so that I could use a different key for repos under github.com/orgname, I'd have to use a different value for Host. So in my case I would add the following to my ~/.ssh/config:

\n
Host gh-work\nHostname github.com\nUser git\nIdentityFile ~/.ssh/work.id_ed25519\n
\n

Finally, to use that Host when I'm looking for a repo in github.com/orgname, I would add the following to my git config:

\n
[url \"gh-work:orgname\"]\n  insteadOf = git@github.com:orgname\n
\n

So when I clone, pull, or push a repo that's under my work's org account I can do:

\n
git clone git@github.com:orgname/project\n
\n

and insteadOf would replace github.com:orgname with gh-work:orgname so that it uses the right info from my SSH config. It's a neat trick which I saw referenced in this article.

\n

Are there any issues with this approach? Is there a better way to do this? I'm not sure so please let me know as I'd love to learn and I'll update this post accordingly.

\n

References

\n", "text": "Note: I've had this post drafted for 3 YEARS!!! It's finally time to publish it.\n\nI like to mess with my dotfiles and every so often, I find out about a new way to do things and I spend more time than I should learning how to use it.\nA few years ago I learned about includeIf for including specific files if some condition was met for git. The example that I first saw was doing:\n[includeIf \"gitdir:~/code/**\"]\n path = ~/.config/git/personal\n[includeIf \"gitdir:~/work/**\"]\n path = ~/.config/git/work\n\nSo that ~/.config/git/personal is only included for git directories under ~/code and ~/.config/git/work is only included for directories under ~/work. The contents of those included files varies but usually it contains your git identity, signing keys, etc. Here's an example of what that could look like:\n[user]\n name = benji\n email = benji@work.com\n signingkey = ~/.ssh/work.id_ed25519.pub\n\nThat works pretty well but I usually organize all my code in ~/workspace regardless of whether its personal, work-1, work-2, etc. I wanted to be able to configure git depending on where that repo actually lives instead of where the directory is in my machine. Then I found out about hasconfig:remote.*.url:!\nThis makes it so that I can configure git conditionally if the given remote URL exists for that directory I'm currently working in.\nA few examples of what I do is:\n[includeIf \"hasconfig:remote.*.url:git@github.com:orgname/**\"]\n path = ~/.config/git/config-gh-org\n\n[includeIf \"hasconfig:remote.*.url:git@github.com:*/**\"]\n path = ~/.config/git/config-gh\n\n[includeIf \"hasconfig:remote.*.url:git@gitlab.com:*/**\"]\n path = ~/.config/git/config-gl\n\n[includeIf \"hasconfig:remote.*.url:git@git.sr.ht:*/**\"]\n path = ~/.config/git/config-srht\n\nNow if I'm in a directory where the remote matches github.com:orgname/** it would use ~/.config/git/config-gh-org, otherwise it uses the general config file for any other GitHub repo.\nWhile that handles git identities, I still need to configure SSH keys separately to be able to pull and push to remotes. The simple version of my ~/.ssh/config looks like this:\nHost gitlab.com\nHostname gitlab.com\nUser git\nIdentityFile ~/.ssh/gitlab.id_ed25519\n\nHost github.com\nHostname github.com\nUser git\nIdentityFile ~/.ssh/github.id_ed25519\n\nThe only problem with this is that in order to use a different IdentityFile for the same Hostname so that I could use a different key for repos under github.com/orgname, I'd have to use a different value for Host. So in my case I would add the following to my ~/.ssh/config:\nHost gh-work\nHostname github.com\nUser git\nIdentityFile ~/.ssh/work.id_ed25519\n\nFinally, to use that Host when I'm looking for a repo in github.com/orgname, I would add the following to my git config:\n[url \"gh-work:orgname\"]\n insteadOf = git@github.com:orgname\n\nSo when I clone, pull, or push a repo that's under my work's org account I can do:\ngit clone git@github.com:orgname/project\n\nand insteadOf would replace github.com:orgname with gh-work:orgname so that it uses the right info from my SSH config. It's a neat trick which I saw referenced in this article.\nAre there any issues with this approach? Is there a better way to do this? I'm not sure so please let me know as I'd love to learn and I'll update this post accordingly.\nReferences\nhttps://fundor333.com/post/2021/advance-git-config-and-ssh-config/\nhttps://www.kenmuse.com/blog/ssh-and-multiple-git-credentials/#git\nhttps://garrit.xyz/posts/2023-10-13-organizing-multiple-git-identities\nhttps://stevenharman.net/configure-ssh-keys-for-multiple-github-accounts"}, "mention-of": "https://fundor333.com/post/2021/advance-git-config-and-ssh-config/", "wm-property": "mention-of", "wm-private": false, "rels": {"canonical": "https://benji.dog/articles/git-config/"}}], "stats": {"like-of": 1, "bookmark-of": 0, "mention-of": 1, "repost-of": 0, "in-reply-to": 0, "comments": [{"content": "Note: I've had this post drafted for 3 YEARS!!! It's finally time to publish it.\n\nI like to mess with my dotfiles and every so often, I find out about a new way to do things and I spend more time than I should learning how to use it.\nA few years ago I learned about includeIf for including specific files if some condition was met for git. The example that I first saw was doing:\n[includeIf \"gitdir:~/code/**\"]\n path = ~/.config/git/personal\n[includeIf \"gitdir:~/work/**\"]\n path = ~/.config/git/work\n\nSo that ~/.config/git/personal is only included for git directories under ~/code and ~/.config/git/work is only included for directories under ~/work. The contents of those included files varies but usually it contains your git identity, signing keys, etc. Here's an example of what that could look like:\n[user]\n name = benji\n email = benji@work.com\n signingkey = ~/.ssh/work.id_ed25519.pub\n\nThat works pretty well but I usually organize all my code in ~/workspace regardless of whether its personal, work-1, work-2, etc. I wanted to be able to configure git depending on where that repo actually lives instead of where the directory is in my machine. Then I found out about hasconfig:remote.*.url:!\nThis makes it so that I can configure git conditionally if the given remote URL exists for that directory I'm currently working in.\nA few examples of what I do is:\n[includeIf \"hasconfig:remote.*.url:git@github.com:orgname/**\"]\n path = ~/.config/git/config-gh-org\n\n[includeIf \"hasconfig:remote.*.url:git@github.com:*/**\"]\n path = ~/.config/git/config-gh\n\n[includeIf \"hasconfig:remote.*.url:git@gitlab.com:*/**\"]\n path = ~/.config/git/config-gl\n\n[includeIf \"hasconfig:remote.*.url:git@git.sr.ht:*/**\"]\n path = ~/.config/git/config-srht\n\nNow if I'm in a directory where the remote matches github.com:orgname/** it would use ~/.config/git/config-gh-org, otherwise it uses the general config file for any other GitHub repo.\nWhile that handles git identities, I still need to configure SSH keys separately to be able to pull and push to remotes. The simple version of my ~/.ssh/config looks like this:\nHost gitlab.com\nHostname gitlab.com\nUser git\nIdentityFile ~/.ssh/gitlab.id_ed25519\n\nHost github.com\nHostname github.com\nUser git\nIdentityFile ~/.ssh/github.id_ed25519\n\nThe only problem with this is that in order to use a different IdentityFile for the same Hostname so that I could use a different key for repos under github.com/orgname, I'd have to use a different value for Host. So in my case I would add the following to my ~/.ssh/config:\nHost gh-work\nHostname github.com\nUser git\nIdentityFile ~/.ssh/work.id_ed25519\n\nFinally, to use that Host when I'm looking for a repo in github.com/orgname, I would add the following to my git config:\n[url \"gh-work:orgname\"]\n insteadOf = git@github.com:orgname\n\nSo when I clone, pull, or push a repo that's under my work's org account I can do:\ngit clone git@github.com:orgname/project\n\nand insteadOf would replace github.com:orgname with gh-work:orgname so that it uses the right info from my SSH config. It's a neat trick which I saw referenced in this article.\nAre there any issues with this approach? Is there a better way to do this? I'm not sure so please let me know as I'd love to learn and I'll update this post accordingly.\nReferences\nhttps://fundor333.com/post/2021/advance-git-config-and-ssh-config/\nhttps://www.kenmuse.com/blog/ssh-and-multiple-git-credentials/#git\nhttps://garrit.xyz/posts/2023-10-13-organizing-multiple-git-identities\nhttps://stevenharman.net/configure-ssh-keys-for-multiple-github-accounts", "link": "https://www.benji.dog/articles/git-config/", "author_name": "benji", "author_photo": "https://avatars.webmention.io/benji.dog/98f50ccee2f72b224906440e0a78cc1fa5ea016ae65e5fa0d42ff5000ad29d24.jpg", "author_url": "https://benji.dog"}]}}