Pimp My Git - Manage Different Git Authentications
Sometimes you have to work on different projects that are hosted on different Git management systems (GitHub, Gitlab, BitBucket, Gitea). The classical scenario is that you work on open source projects that are hosted on GitHub and on enterprise projects that are hosted on an own Git management system. These projects also need different authentications. These authentications can be managed in Password Manager. But it remains cumbersome to log in with username and password. Especially with Git repositories, there is the possibility to authenticate using an SSH key and SSH can be configured to detect itself which key to use.
Let's say we have two Git projects that need two different authentication. Both projects are accessible under two different domains, example1.com and example2.com. For each of the two projects, we generate a public/private SSH key pair (id_example1
and id_example2
).
1$ ssh-keygen
2Generating public/private rsa key pair.
3Enter file in which to save the key (/home/sparsick/.ssh/id_rsa): /home/sparsick/.ssh/id_example1
4Enter passphrase (empty for no passphrase):
5Enter same passphrase again:
6Your identification has been saved in /home/sparsick/.ssh/id_example1.
7Your public key has been saved in /home/sparsick/.ssh/id_example1.pub.
8The key fingerprint is:
9SHA256:9XVMIDlWX/6OvAzVmqKUBm8pmNNoiFkjJZ/+RERhp/A sparsick@sparsick-ThinkPad-T14
10The key's randomart image is:
11+---[RSA 2048]----+
12| . +.. .oo.o|
13| = o +. =.|
14| . . E .. .. =|
15| + o . . .
16| . = . S . . o|
17| * + = o o = |
18| o o B o = .|
19| + . = . + . |
20| . . o |
21+----[SHA256]-----+
The private key is then located in the file id_example1
and the public key in the file id_example1.pub
. This public key must be stored in the respective Git management system or Git server, so that the authentication via SSH can work.
Now it must be entered in the SSH configuration for which domain which key is to be used. For this, if not existing, a file config is created under $USER_HOME/.ssh
and in it is entered for which domain which key is valid:
1Host example1.com
2HostName example1.com
3User git
4IdentityFile ~/.ssh/id_example1
5
6Host example2.com
7HostName example2.com
8User git
9IdentityFile ~/.ssh/id_example2
Thus, the git repositories can be cloned and pushed without entering the respective credentials.