Just put this in your ~/.gitconfig (or ~/.config/git/personal as in the article)
[core]
sshCommand = /usr/bin/ssh -o IdentitiesOnly=yes -i ~/.ssh/IdentityFile2 -a
This makes submodules easy without the `insteadOf`... time to go change my security questions
Host customer-github
Hostname github.com
IdentityFile ~/.ssh/customer_rsa
User git
All I have to do is use the alias in any git clone command and I'm done. git clone gitlab.com/acme-corp/project-name
I could use: git clone work:project-name
But this kinda broke `includeIf` since it store the `insteadOf` remote url directly. I then had to convert existing repositories to use the `insteadOf` url.I wrote a little bit about it here: https://bentinata.com/log/git-insteadof-includeif
The private bits are all in the same place: if one is compromised, so are the rest.
However if you're worried about this then you should probably be using a hardware token anyway - something that supports SSH authentication via FIDO2, GPG, or smart card interface.
Curious though that the compliance rules are strict enough it warrants distinct keypairs, but not that strict for the devs to use dedicated hardware.
Your signing key for personal projects probably has a different temporality.
What does this achieve exactly?
[includeIf "hasconfig:remote.*.url:gh-work:**/**"]
path = ~/.gitconfig.d/gh-work.inc
So, any git repo cloned with the ssh identity defined under `gh-work` will take on the config of `gh-work.inc`, which includes the git identity, and also the same signing key as in the ssh config.Essentially, the name `gh-work` becomes the distinguishing element in both my ssh identity and my git identity, and I find this easier to think about.
After configuring the identities you just need to run
$ git su Personal
$ git su Work
And all the identity configuration (email, name, SSH key and optionally PGP key) will be set up into the repo's .git/config file.Saved me a ton of time.
https://github.com/dolmen/github-keygen
12 years old, but still actively maintained.
One thing though: what’s the point of using separate keys for work/personal/github/gitlab? I fail to see a practical and security advantage over using one key (per workstation).
In fact, I was one of not too many who used separate account for work, and people didn’t understand it, wondering why the hassle.
These days I prefer to use local VMs to compartmentalize different areas of work (personal, consulting, etc) so my git config is plain and simple. Lately I’ve been doing mostly consulting work around open-source so I’ve been using my primary GH account for the most part, but separate VMs allow me to use a different key (account) without advanced git config incantations.
For many of the companies that I've worked at, the laptops were taken home to be used as personal computers at the end of the day and this was a well-known thing and I was often looked at weird when I said I had another laptop.
One time I took the wrong laptop in and had to work on my personal laptop in the office. It wasn't so much fun that day.
* use a dedicated work machine and
* also want to version control your dotfiles (including ~/.config/git/) and
* don't want to leak your work repository organisation via your dotfiles,
you can instead add something like
[include]
path = work.gitconfig
which will override any settings above it and also fail gracefully/silently if work.gitconfig does not exist.I ended up creating a "SSH environment" manager 4 years ago to help with this: https://github.com/theonejb/sshenv
It's worked wonderfully for me since then, and it's something I use almost daily.
Aside: I use NixOS with home-manager (on linux and mac), which makes this trivial [1]. Added the following lines to my home-manager config:
programs.git = {
enable = true;
...
includes = [
{
condition = "hasconfig:remote.*.url:[email protected]:<work>/**";
contents = {
user.email = "<work email>";
};
}
];
}
[1]: https://nix-community.github.io/home-manager/options.xhtml#o...Even if I'm in my work profile and I need to do something in an org called `acmecorp`, I will create @acmecorp-identifier to do that.
This is just a very long experience...
* Security policies for work things have a blast radius of just that employer
* OSS things have a lifetime beyond the life of an employment / contract
* Source control elsewhere (GitHub / GitLab / Bitbucket / Gitea / Forgejo / etc) all has a local blast radius, and if a provider / org forces changes (roll your keys!) then the impact is limited to just that provider
* When something changes ownership (i.e. an org), the impact to me is low
It seems much more sane.
I think of a single git identity across multiple orgs as a bit of a smell.
I suddenly felt a deep connection with the author. It is not only me.
I promise you, my dear drafts, that one day, I will set you free to see the world!
Just keep a .gitconfig in your HOME with aliases for your identities. Then just after initializing/cloning the repo do git config-company or git config-personal
er453r@r7:~$ cat ~/.gitconfig
[user]
useConfigOnly = true
[alias]
config-personal = !echo CONFIG-PERSONAL && \
git config --local user.email '[email protected]' && \
git config --local user.name 'personal' && \
git config --local core.sshCommand 'ssh -i ~/.ssh/id_rsa_personal'
config-company = !echo OLD CONFIG-COMPANY && \
git config --local user.email '[email protected]' && \
git config --local user.name 'Name Surname' && \
git config --local core.sshCommand 'ssh -i ~/.ssh/id_rsa_company'