Setting Up SSH for GitHub
This guide explains how to set up SSH keys for secure, password-free communication with GitHub. The steps work on Windows (using Git Bash), Linux, and macOS (using the terminal). It’s designed to be simple and beginner-friendly.
Why Use SSH?
SSH keys allow you to push and pull code from GitHub without entering your username and password every time. It’s secure and convenient for managing your projects.
Step-by-Step Instructions
1. Generate an SSH Key Pair
SSH keys come in pairs: a private key (kept secret on your computer) and a public key (shared with GitHub).
-
Open a Terminal:
- On Windows: Open Git Bash (search for it in the Start menu).
- On Linux: Press
Ctrl + Alt + Tto open the terminal. - On macOS: Open Terminal (search for it using Spotlight or find it in Applications > Utilities).
-
Generate the Key:
-
Run this command, replacing
your.email@example.comwith the email used for your GitHub account:ssh-keygen -t ed25519 -C "your.email@example.com" -
If your system doesn’t support
ed25519(older systems), use this instead:ssh-keygen -t rsa -b 4096 -C "your.email@example.com"
-
-
Save the Key:
- Press Enter to accept the default file location (
~/.ssh/id_ed25519or~/.ssh/id_rsa). - Optionally, enter a passphrase for extra security (or press Enter to skip).
- Press Enter to accept the default file location (
2. Start the SSH Agent
The SSH agent manages your keys in the background.
- Run this command:
eval "$(ssh-agent -s)"
- You should see output like
Agent pid 1234(the number varies).
3. Add the SSH Key to the Agent
This step links your private key to the SSH agent.
- Run:
ssh-add ~/.ssh/id_ed25519
Or, if you used RSA:
ssh-add ~/.ssh/id_rsa
- If you set a passphrase, enter it when prompted.
4. Copy the Public Key
The public key is what you’ll share with GitHub.
-
Display the Key:
-
Run:
cat ~/.ssh/id_ed25519.pubOr, if you used RSA:
cat ~/.ssh/id_rsa.pub -
The output starts with
ssh-ed25519orssh-rsaand ends with your email.
-
-
Copy the Key:
- On Windows (Git Bash): Highlight the output, right-click to copy.
- On Linux: Highlight and press
Ctrl + Shift + Cor right-click to copy. - On macOS: Highlight and press
Cmd + C.
5. Add the Key to GitHub
Now, add your public key to your GitHub account.
-
Log in to GitHub:
- Open a browser and go to github.com.
- Sign in to your account.
-
Add the SSH Key:
- Click your profile picture (top-right) > Settings.
- In the left sidebar, click SSH and GPG keys.
- Click New SSH key or Add SSH key.
- In the Title field, enter a descriptive name (e.g., "My Laptop").
- In the Key field, paste your public key (copied in Step 4).
- Click Add SSH key.
6. Test the Connection
Verify that your SSH setup works with GitHub.
-
Run the Test Command:
-
In your terminal or Git Bash, run:
ssh -T git@github.com -
If prompted, type
yesto trust the host and press Enter.
-
-
Check the Result:
-
You should see a message like:
Hi your-username! You've successfully authenticated, but GitHub does not provide shell access. -
This means your SSH key is working!
-
Using SSH with GitHub
Now that SSH is set up, use it to interact with GitHub repositories:
-
Clone a Repository:
- On GitHub, go to a repository and click the Code button.
- Select SSH and copy the URL (e.g.,
git@github.com:username/repository.git). -
In your terminal or Git Bash, run:
git clone git@github.com:username/repository.git
-
Set SSH for an Existing Repository:
-
In your local repository folder, run:
git remote set-url origin git@github.com:username/repository.git
-
Tips for Beginners
- Keep Your Private Key Safe: Never share your private key (
id_ed25519orid_rsa). - Passphrase: If you added a passphrase, you’ll need to enter it when using the key (unless you use an SSH agent).
- Troubleshooting: If the connection test fails, double-check your key in GitHub and ensure the SSH agent is running.
- Help: Search GitHub’s documentation or forums if you run into issues.
You’re now ready to use SSH for secure, password-free GitHub interactions!