Cloning a GitHub repository using SSH keys is a secure and efficient way to access and collaborate on projects. In this guide, we’ll walk through the process step by step, making it easy for Mac users to get started with SSH key authentication on GitHub.
Prerequisites
Before we begin, ensure you have the following:
1. A GitHub account.
2. Git installed on your Mac. If not, you can download it from here.
3. Terminal or an alternative command line tool on your Mac.
Step 1: Generate SSH Key
First, let’s generate an SSH key pair. Open Terminal and enter the following command:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
Replace `”[email protected]”` with the email associated with your GitHub account. Press Enter to continue.
You’ll be prompted to choose a location for your SSH key. The default location (`~/.ssh/id_rsa`) is generally fine, so press Enter to proceed without changes.
Next, you’ll be asked to enter a passphrase for your SSH key. While optional, adding a passphrase adds an extra layer of security. Type your passphrase (it won’t be visible as you type) and press Enter. Confirm the passphrase when prompted.
Step 2: Add SSH Key to SSH Agent
Now, let’s add the SSH key to the SSH agent on your Mac. Start the SSH agent by running the following command:
eval "$(ssh-agent -s)"
This command starts the SSH agent in the background.
Add your SSH key to the SSH agent using:
ssh-add ~/.ssh/id_rsa
If you used a different location or filename for your SSH key, adjust the command accordingly.
Step 3: Add SSH Key to GitHub
To clone repositories using SSH, GitHub needs to recognize your SSH key. Copy your SSH public key to your clipboard using:
pbcopy < ~/.ssh/id_rsa.pub
This command copies the contents of your SSH public key to the clipboard.
Next, navigate to your GitHub account:
- Click on your profile picture in the top-right corner and select Settings.
- In the left sidebar, click on SSH and GPG keys.
3. Click on New SSH key.
4. Paste your SSH key into the Key field.
5. Give your SSH key a descriptive title (e.g., “Mac SSH Key”).
6. Click Add SSH key to save your key.
Step 4: Clone Repository
With your SSH key set up, you can now clone repositories using SSH. Navigate to the repository you want to clone on GitHub.
Click on the Code button and select SSH to get the SSH URL of the repository. It should look something like `[email protected]:username/repository.git`.
Open Terminal and navigate to the directory where you want to clone the repository using the `cd` command.
Clone the repository using SSH by running:
git clone [email protected]:username/repository.git
Replace `username/repository.git` with the actual username and repository name.
Step 5: Verify Cloning
Once the cloning process completes, navigate into the cloned repository using `cd repository_name`.
To verify that you’ve successfully cloned the repository using SSH, run:
git remote -v
You should see the SSH URL of the repository listed as `origin`.
Conclusion
Congratulations! You’ve successfully cloned a GitHub repository on your Mac using SSH keys. This method provides a secure and convenient way to interact with GitHub repositories without entering your credentials each time. Happy coding!