My need was to be able to easily switch the SSH key used to authenticate to GitHub because I have a work account and a personnal account. I know of this method, but it would be very unpractical on my codebases.
The reason is that I use Nix flakes both on personnal and on professional projects - and those flakes tend to reference other, private flakes. Flakes have lockfiles that pinpoint dependecies via their git+ssh:
urls, and the aforementioned method would require messing with these URLs. In other words, I need git+ssh://git@github.com/org/repo
to stay as it is and not become something like git+ssh://git@github.com-work/org/repo
so that my Nix flakes and other dependency-pinning tools can work with private repos.
I did some reading on man ssh_config
and came up with an enhanced method that fixes it for me. I'm providing the code I inserted in my Home Manager config along with instructions on how to achieve this if you are not using Home M