How to Connect to GitHub Using SSH on macOS, Linux, and Windows
How to Connect to GitHub Using SSH on macOS, Linux, and Windows
In this tutorial, I’ll guide you step-by-step on how to connect to GitHub using SSH on macOS, Linux, and Windows. We’ll also use a well-known public repository (like OBS Studio) to show how to clone a repo securely using SSH.
I. Why Use SSH?
SSH keys provide a secure and convenient method for authenticating to GitHub. Instead of using your GitHub username and password each time you push or pull, you can authenticate automatically with SSH keys.
II. Step-by-Step Guide
1. Generate a New SSH Key
The first step in setting up SSH authentication with GitHub is generating an SSH key. This process is similar across macOS, Linux, and Windows.
1.a. macOS and Linux:
Open your terminal and run the following command to generate an SSH key:
ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
- Replace your_email@example.com
with the email associated with your GitHub account.
- Press Enter
to accept the default file location ( or /home/your_username/.ssh/id_rsa
).
- If prompted, set a passphrase, or leave it blank for no passphrase.
1.b. Windows:
For Windows, you can use Git Bash (which is included when you install Git for Windows) to generate your SSH key. Follow the same steps as above in Git Bash.
ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
After generating the SSH key, the key pair (private and public) will be saved in your .ssh
directory.
2. Add the SSH Key to the SSH Agent
Next, you’ll need to add the SSH key to your SSH agent, which helps manage the keys.
2.a. macOS:
Ensure the SSH agent is running:
eval “$(ssh-agent -s)”
Then, add the key to the agent:
ssh-add ~/.ssh/id_rsa
2.b. Linux:
Start the SSH agent:
eval “$(ssh-agent -s)”
Add your SSH key:
ssh-add ~/.ssh/id_rsa
2.c. Windows:
In Git Bash, start the SSH agent:
eval “$(ssh-agent -s)”
Add your key:
ssh-add ~/.ssh/id_rsa
3. Add the SSH Key to Your GitHub Account
Now that your SSH key is generated, you need to add the public key to your GitHub account.
To do this, first, copy your public key to the clipboard.
3.a. macOS and Linux:
pbcopy < ~/.ssh/id_rsa.pub
3.b. Windows (in Git Bash):
clip < ~/.ssh/id_rsa.pub
Next, follow these steps:
1. Go to GitHub SSH settings.
2. Click New SSH key.
3. Paste your key into the “Key” field and give it a name like “My Computer SSH Key.”
4. Click Add SSH Key.
4. Test Your SSH Connection
Once you’ve added the SSH key to GitHub, you should test whether the connection is working.
In the terminal (macOS, Linux, or Git Bash on Windows), run the following command:
ssh -T git@github.com
You should see a message like:
Hi username! You’ve successfully authenticated, but GitHub does not provide shell access.
This message confirms that your SSH key is correctly set up.
5. Clone a GitHub Repository Using SSH
Now that you’ve set up SSH, you can clone repositories securely. Here’s the skeleton command for cloning any repository:
git clone git@github.com:<owner>/<repo>.git
For this tutorial, let’s use the OBS Studio public repository as an example. To clone it, use:
git clone git@github.com:obsproject/obs-studio.git
This will clone the repository into your current directory.
III. Troubleshooting
If you encounter the error Permission denied (publickey)
, it means there’s an issue with your SSH keys. Double-check the following:
- Ensure the SSH key is added to your GitHub account.
- Ensure the SSH agent is running and the key is added to it.
- Run ssh -T git@github.com
to verify that SSH authentication is working.
IV. Conclusion
Using SSH keys to authenticate with GitHub simplifies the process of pushing and pulling code without needing to enter a username and password each time. Once your SSH keys are set up, your workflow will be faster and more secure.
Feel free to share this tutorial with your developer friends or bookmark it for future reference. Happy coding!
— -
This post should be helpful for anyone trying to set up SSH authentication with GitHub on macOS, Linux, or Windows. Let me know if you’d like any further adjustments before posting on Medium!