[git] How to use a repo-specific ssh key
Jul 24, 2022
The problem
Once you have multiple accounts in GitLab you can’t use the same ssh key, so you have to generate a new ssh keypair and configure a repository to use the new key from a different location.
Steps
- Generate a new key
ssh-keygen -t rsa -f ./.ssh/git-lab-key
- Clone a repo with the new key
git -c core.sshCommand="ssh -i ~/{YOUR_PROJECT}/.ssh/git-lab-key" clone git@gitlab.com:<user>/<repo>.git
- Configure an existing repo to use the new key
git config core.sshCommand 'ssh -i ~/{YOUR_PROJECT}/.ssh/git-lab-key'