-
Notifications
You must be signed in to change notification settings - Fork 23
Configuring SSH Access to Github or GitLab
-
If you don't have one already, create a
.sshdirectory in your home directory:mkdir ~/.ssh -
Create a new keypair in
~/.ssh/configfollowing these instructions -
Add this to your
~/.ssh/configfile (create it if necessary)Host github.com IdentityFile ~/.ssh/my_file.key # change this to use the key you generated, above -
Add your SSH key to your GitHub account, or if using GitLab: Add your SSH key to your GitLab account
-
If you're using MacOS, and you have not done so already, cache these credentials in the system keychain as follows:
ssh-add -K /Users/$USER/.ssh/id_rsa.github
If you're like me, you may have different work and play accounts with GitHub (Gitlab users: substitute GitLab for GitHub throughout this section).
Note, these instructions are intended for use in MacOS. If you use Linux or Windows, it will be similar, but different.
Change into the repository directory you wish to configure access for:
cd my_repo
Set the local credentials to match the username you wish to use, and also set the correct SSH key file (the one you created above)
git config user.name "Your Name"
git config user.email "[email protected]"
git config core.sshCommand "ssh -i /Users/$USER/.ssh/id_rsa.github"
Add the credentials to the MacOS key store:
ssh-add -K /Users/$USER/.ssh/id_rsa.github # change this to refer to the keys you generated above
Test that SSH how identifies the correct user account (without a password)
ssh -i /Users/$USER/.ssh/id_rsa.github [email protected]
Finally, check-out out the repo.
git clone [email protected]:(YOUR GITHUB HANDLE)/repo-name.git
If you can't convince MacOS to use the right identity, your old identity is probably still cached. Take some advice from Aliens and do a:
ssh-add -D # delete all cached SSH keys
ssh-add -K /Users/$USER/.ssh/id_rsa.github # re-cache your new ssh-key
You can check which identity may be confusing GitHub by doing
and then checking:
ssh -i /Users/$USER/.ssh/id_rsa.github [email protected]
If these are different, and GitHub doesn't recognize the second identity, double-check that you have done this step properly:
git config core.sshCommand "ssh -i /Users/$USER/.ssh/id_rsa.github"