GNU/Linux Desktop Survival Guide
by Graham Williams |
|||||
Git Multiple Users and SSH Keys |
20200914 Consider the scenario where you might have multiple user accounts on a git server. Perhaps the git accounts are ktfred and ktmary on https://gitlab.com. Also, rather than both sharing your personal public key, you want each to have their own private/public key pairs.
First clone the remote repository:
$ git clone git@gitlab.com:kt/mykt.git |
It is a public repository so anyone can clone it but not push to it.
Configure a local git username and email address to override any global values.
$ git config user.name "K T Mary" $ git config user.email "ktmary@kt.com" $ git config --list |
Now create a new private/public key pair to access the repository as ktmary.
$ ssh-keygen -f ~/.ssh/id_rsa_ktmary_gitlab -N "" -C ktmary@kt.com $ cat ~/.ssh/id_rsa_ktmary_gitlab.pub ssh-rsa AAAAB3NzaC1...A4tHGD0= ktmary@kt.com |
Copy the public key into SSH Keys under User Settings in gitlab. Then configure the local repository clone to use this public key.
$ git config core.sshCommand "ssh -i ~/.ssh/id_rsa_ktmary_gitlab.pub -F /dev/null" |
A push should then work.
$ git push |