How do you set up Git to use SSH instead of HTTPS?
Posted by TinaGrn
Last Updated: July 17, 2024
To set up Git to use SSH instead of HTTPS, you'll need to follow these steps:
1. Generate SSH Key (if you don't have one)
If you don't already have an SSH key, you can generate one with the following command:
ssh-keygen -t ed25519 -C "your_email@example.com"
For older systems that do not support ed25519, you can use RSA:
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Press Enter to accept the default file location, and set a passphrase or leave it empty for no passphrase.
2. Add SSH Key to the SSH Agent
Start the SSH agent in the background:
eval "$(ssh-agent -s)"
Then, add your SSH private key to the SSH agent:
ssh-add ~/.ssh/id_ed25519
(or ~/.ssh/id_rsa if you generated an RSA key)
3. Copy the Public SSH Key
Use the following command to copy your SSH public key to the clipboard:
# For macOS:
pbpaste < ~/.ssh/id_ed25519.pub

# For Linux:
xclip -sel clip < ~/.ssh/id_ed25519.pub

# For Windows (Git Bash):
cat ~/.ssh/id_ed25519.pub | clip
4. Add SSH Key to Your Git Host
Now, you need to add your SSH key to your Git hosting service (e.g., GitHub, GitLab, Bitbucket). - GitHub: - Go to Settings - Click on SSH and GPG keys - Click New SSH key and paste your key there. - GitLab: - Go to Preferences - Navigate to SSH Keys - Paste your SSH key and save. - Bitbucket: - Go to Personal settings - Under Security, click on SSH keys - Add your SSH key there.
5. Change Your Git Remote URL to Use SSH
If you already have a repository cloned using HTTPS, you'll need to change the remote URL to use SSH. You can do this with the following command in your Git repository:
git remote set-url origin git@github.com:username/repository.git