286 points · by djha-skin · 2y ago · 70 comments
I wrote it up here, if that sounds useful: https://www.brantonboehm.com/code/conditional-git-config/
For years I've been using [direnv](https://direnv.net/) for this, setting environment variables which git picks up. This looks like a more feature complete equivalent, although to be honest I only really need switching of committer email and the SSH key used.
it's much easier to have a conditional git config in ~/.gitconfig that modifies the git ssh command to explicitly use a different ssh key.
Match originalhost bitbucket.org exec "[[ ${PWD}/ == ${HOME}/repos/work/* ]]"
IdentityFile ~/.ssh/keys/work
User me-work
Match originalhost bitbucket.org exec "[[ ${PWD}/ == ${HOME}/repos/personal/* ]]"
IdentityFile ~/.ssh/keys/personal
User me Host project-foo
Hostname github.com
User git
IdentityFile ~/.ssh/some_key
Then I'll pull the project with: git clone project-foo:/Foo/project.gitGenuinely curios. After HTTPS became supported I never looked back. The whole authentication management and Git remote URL syntax are a huge mess I'd never want to touch unless forced to. HTTPS makes it a lot simpler... at least for me. Am I missing something?
I haven't had to set up Git server in a very long time. I'm not even sure if I had to set up HTTPS authentication for it. But, I can see how this may be potentially more difficult as you'd already have SSH set up for accessing the server by the time you get to set up Git.
It's been a lot easier on the client-side though. Especially having to deal with multiple users / multiple servers and having to share configuration between different machines. Usually it just boils down to having ~/.git-credentials and if necessary, adding a similar file in the root of the repo.
In case anyone is stuck in the same way that I was, the trailing slash at the end (which I had previously omitted, not realizing) is necessary for this to work. The docs[0] mention this, but I'd managed to repeatedly miss it:
> If the pattern ends with /, * will be automatically added. For example, the pattern foo/ becomes foo/*. In other words, it matches "foo" and everything inside, recursively.
[0]: https://git-scm.com/docs/git-config#Documentation/git-config...
[1]: https://chezmoi.io/
[2]: https://github.com/cglong/dotfiles/blob/ea33143679e936f4043a...
So “work-github” can be a Host entry (with a HostName of github.com) and key A. While “play-github” would use key B.
Of course, I can test it myself, but since somebody already does that I assumed I could just ask.
Thank you for sharing it!
[credential]
credentialStore = secretservice
helper = /usr/local/bin/git-credential-libsecret
[credential "https://github.com"]
username = akdor1154
[includeif "gitdir:~/src/mywork/"]
path=~/src/mywork/.gitconfig
~/src/mywork/.gitconfig: [user]
email = "12345+akdor1154-mywork@users.noreply.github.com"
signingkey = ~/.ssh/id_rsa.mywork.private
[credential "https://github.com"]
username = akdor1154-mywork
The `username` is passed to the global secret helper and is thus used for it as part of the pick/prompt context for password. I'm using the libsecret helper but this works with any.I wrote about this too https://lesterzone.github.io/blog/2023/01/10/git-work-person...
I consider this type of 'tips' hidden gems
I wrote this about 3 or 4 years ago detailing exactly the same: https://www.leemeichin.com/posts/conditional-git-config.html
What path should go into gitdir if the repos are at S:\Repos\ ?
I think Git on Windows uses forward slashes, but colon is retained, like this:
directory = C:/Program Files (x86)/MyProject
https://gitextensions.github.ioThanks
For local Linux projects, I have Docker images for my development environments, one per identity. So I keep my PC as minimal as I can, while the compilers, interpreters, etc, are only installed within a Docker image.
Then I go to a project's directory, run a helper command like `identity1-workspace`, and it (1) starts the Docker container for `identity1`, (2) mounts the current directory to `/workspace` within the container, and (3) also mounts some cache directories, etc.
For other hosts where I don't want to use Docker (e.g. in Raspberry Pis, or in VPS) I just have multiple users and SSH into the one I want to use.
I can do this because I just use nvim and the terminal for everything, so for me there's basically no difference between developing "natively" vs developing inside a Docker container vs developing through SSH. The only difference is whether I run the command `nvim` vs `identity1-workspace` vs `ssh identity1`[1], respectively.
All this sounds like too much work, but I'd rather use (1) Docker, or (2) `adduser` and `ssh`, because those I can do from memory and can trust to be reliable since `identity1` can't push to repos of `identity2` even by accident. In comparison, the article's `includeIf` looks like something that could easily leak an identity by accident, not to mention that I'd forget its syntax after 2 days.
[1]: Well in this case also have to then `cd somewhere ; nvim` because writing a helper for this is no big deal.
Maybe it was unusual enough that it didn't sound like a serious response (?), but you're completely right that the article's suggested solution can easily leak stuff by accident if you want to absolutely avoid committing with the wrong identity, thus revealing name, email, timezone, or some other metadata, in a repository where you don't want to.
If I contribute to a legal-gray-area anime fansub I don't want those people (other contributors) to know anything about my IRL identity, for example, so of course I'd reduce the possibility of info leaks due to typos or because my muscle memory betrayed me.
Like I mentioned in a sibling reply, I'd rather use a completely different OS user[1], a Docker container, or a different host.
[1]: That's literally what they are for.