Setting up Git to use an SSH key for authentication involves a few steps. Here's a general guide:
If you don't have an SSH key pair, you'll need to generate one. Open a terminal and run:
bashssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Follow the prompts, and you can leave the passphrase empty if you want.
To avoid entering your passphrase every time you use your SSH key, you can add it to the SSH agent. Start the SSH agent:
basheval "$(ssh-agent -s)"
Add your SSH key to the agent:
bashssh-add ~/.ssh/id_rsa
Copy your public key to the clipboard:
bashpbcopy < ~/.ssh/id_rsa.pub
If you're on Linux, you can use:
bashxclip -sel clip < ~/.ssh/id_rsa.pub
Paste the copied SSH key into your Git hosting service (GitHub, GitLab, Bitbucket, etc.). The process might vary depending on the platform, but generally, you'll find a "SSH and GPG keys" section in your account settings where you can add your SSH key.
Make sure your SSH configuration is set up correctly. Check if your ~/.ssh/config
file exists; if not, create one. Add the following content:
plaintextHost * AddKeysToAgent yes UseKeychain yes IdentityFile ~/.ssh/id_rsa
This configuration ensures that your SSH key is added to the agent when you use Git.
Finally, test the SSH connection to your Git hosting service:
bashssh -T git@github.com
Replace github.com
with your Git hosting service's domain. You should see a message indicating a successful authentication.
When you clone or set up a Git repository, make sure to use the SSH URL instead of HTTPS. For example:
bashgit clone git@github.com:username/repo.git
That's it! Your Git setup is now configured to use an SSH key for authentication.