Live data from Hacker News

Conditional Git Configuration

blog.scottlowe.org

31–40 of 73 posts

Re: Conditional Git Configuration

#31
post #21
post #18

Earlier quoted context omitted.

So subdomains can be arbitrary and you just manually add them when performing git clone? Will then git push/pull/etc automatically use the fake subdomain you like for this repository?

You can have anything you’d like in your ssh config and it will be treated as a server name for auth and auto completion. So “work-github” can be a Host entry (with a HostName of github.com) and key A. While “play-github” would use key B.

You are basically just repeating what he said, but it doesn't answer my question. After all I'm not ssh-ing to GitHub directly, I'd use git, which will use ssh as a transport, and I don't know if git will use the subdomains I provide the way I think it should (my primary concern being to prevent GitHub from displaying 2 public keys for a single account, so that 3rd party cannot tell that it's most probably the same person).

Of course, I can test it myself, but since somebody already does that I assumed I could just ask.

Re: Conditional Git Configuration

#32
post #24
post #8

Nice. 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.

For the SSH part you can use ~/.ssh/config and it also has conditional stanzas like "Host foo.com" or "Host *.foo.com". Can set username, SSH Key, SSH Agent Socket (I use multiple SSH agents), etc.

meh.

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.

Re: Conditional Git Configuration

#33
I'd attempted to configure this some time back, but never gotten it working, and this was the kick in the pants I needed to finally get it working!

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...

Re: Conditional Git Configuration

#34

Oh nice, this will remove an annoyance in my dotfiles repo by letting me share my gitconfig between 3 different configurations (home linux amd64 box, work macOS arm64, client macOS arm64) Thank you for sharing it!

What do you have in your git config that requires switching over host architecture?

Re: Conditional Git Configuration

#35
post #31
post #21

Earlier quoted context omitted.

You can have anything you’d like in your ssh config and it will be treated as a server name for auth and auto completion. So “work-github” can be a Host entry (with a HostName of github.com) and key A. While “play-github” would use key B.

You are basically just repeating what he said, but it doesn't answer my question. After all I'm not ssh-ing to GitHub directly, I'd use git, which will use ssh as a transport, and I don't know if git will use the subdomains I provide the way I think it should (my primary concern being to prevent GitHub from displaying 2 public keys for a single account, so that 3rd party cannot tell that it's most probably the same p…

I tested it myself, like `ssh git@rando.github.com` and that didn't work.

That was my interpretation of the original comment. (reading it again, it seems like it was indeed saying what I typed below)

It seems like the reply was saying instead you can do in your .ssh/config file

   Host work.github.com
   HostName github.com
   Host me.github.com
   HostName github.com
(or just leave out the .github.com in the Host part)

So `ssh git@me.github.com` now works...

And then key the github config off of that? But I didn't try that part. So I could be wrong there.

Re: Conditional Git Configuration

#36
post #19
post #8

Nice. 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.

You don’t need for config for the SSH piece as it defers to SSH’s own config for it.

If you’re using SSH for commit signing, you do need that in your git configuration.

Re: Conditional Git Configuration

#39
Another trick I find useful for managing Git over SSH for multiple accounts (especially with bitbucket.org, which supports specifying different usernames) is the "Match exec" directive which allows you to include a shell (bash) conditional (such as a directory-check), e.g.

    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
Post reply on HN