To configure GitHub SSH on a Mac, follow this step-by-step guide. This will allow you to authenticate with GitHub using an SSH key rather than a username and password.
1. Check for Existing SSH Keys
First, check if you already have an SSH key pair on your system.
Command:
ls -al ~/.ssh
This will list the files in the ~/.ssh
directory. Look for files like:
id_rsa
andid_rsa.pub
(default key pair files)id_ed25519
andid_ed25519.pub
(if you are using Ed25519 keys)
If you already have an SSH key pair, you can use it for GitHub. Otherwise, you’ll need to generate a new one.
2. Generate a New SSH Key (if you don’t have one)
If you don’t have an SSH key or want to create a new one for GitHub, follow these steps.
Command:
ssh-keygen -t ed25519 -C "your-email@example.com"
Explanation:
-t ed25519
: Generates a more secure Ed25519 key (you can also usersa
if you prefer).-C "your-email@example.com"
: A comment to label the key (usually your GitHub email).
Steps after running the command:
- You will be prompted to enter a file to save the key. Press Enter to save it to the default location (
/Users/your-username/.ssh/id_ed25519
). - Optionally, enter a passphrase for added security, or press Enter to skip it.
3. Add the SSH Key to the SSH Agent
Once the key is generated, add it to the SSH agent, which manages your SSH keys and makes them available for authentication.
Start the SSH agent:
eval "$(ssh-agent -s)"
This will start the SSH agent in the background and output the process ID.
Add your SSH private key to the agent:
ssh-add ~/.ssh/id_ed25519
If you used a different name for the key file, replace id_ed25519
with your file name.
4. Add the SSH Key to Your GitHub Account
Now that the SSH key is generated and added to the SSH agent, the next step is to add the public key (id_ed25519.pub
) to your GitHub account.
Command to copy the public key:
pbcopy < ~/.ssh/id_ed25519.pub
This command copies your SSH public key to the clipboard. If you’re using a different key file, replace id_ed25519.pub
with your public key file.
Add the key to GitHub:
- Go to GitHub SSH settings.
- Click New SSH key.
- In the Title field, give it a descriptive name (e.g., “MacBook SSH Key”).
- Paste the copied SSH key into the Key field.
- Click Add SSH Key to save it.
5. Test Your SSH Connection
Now that the SSH key is set up, test the connection to GitHub to ensure everything is working.
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.
If you see this message, the SSH key is correctly set up!
6. Configure Git to Use SSH
Now that the SSH key is set up and working, configure your local Git repository to use SSH for communication with GitHub.
- Check your current remote URL: Run the following command inside your Git repository:
git remote -v
If the remote URL starts withhttps://
, it’s using HTTPS. We want to switch it to SSH. - Change the remote URL to use SSH: Run the following command to change the URL to use SSH instead of HTTPS:
git remote set-url origin git@github.com:username/repository.git
Replaceusername
with your GitHub username andrepository
with the repository name. - Verify the change: Run:
git remote -v
You should now see the remote URL asgit@github.com:username/repository.git
.
7. Push to GitHub using SSH
Now, when you push or pull from your repository, Git will use the SSH key for authentication instead of asking for your username and password.
Example command to push:
git push origin master
This should successfully push your changes to GitHub using SSH.
Recap of Key Steps
- Generate SSH key with
ssh-keygen -t ed25519 -C "your-email@example.com"
. - Add SSH key to SSH agent with
ssh-add
. - Copy the public key to your GitHub account via GitHub settings.
- Test the connection with
ssh -T git@github.com
. - Switch repository remote URL to SSH with
git remote set-url
.
If you run into any issues during the setup, feel free to let me know!