Configuring Git Bash to Clone a Private Repository using SSH
Recently, I was granted access to a private GitHub repository to make some modifications. While I had all the necessary permissions and could access the repository via the GitHub interface, I encountered a challenge when trying to clone it using Git Bash. Each attempt resulted in the common error: “404 — Repository not found”, despite correctly setting up my global Git credentials.
After troubleshooting, I was able to resolve the issue. This article aims to guide others who might face similar challenges when attempting to clone a private repository over the internet.
There are two primary methods for resolving this issue:
- Authentication with a Personal Access Token (PAT)
- Authentication with an SSH Key
In this article, we will focus on the second method — Authentication using an SSH key.
Step 1: Check if you have an SSH Key
Before generating a new SSH key, check if one already exists:
- Open Git Bash.
- Run the following command:
ls -al ~/.ssh
If you see files like id_rsa
and id_rsa.pub
, you already have an SSH key pair. You can skip to Step 4.
Step 2: Generate a New SSH Key
If no SSH key exists or you prefer to create a new one, follow these steps:
- Run the SSH key generation command:
ssh-keygen -t rsa -b 4096 -C "your_emai@example.com"
Replace your_email@example.com
with the email linked to your GitHub account.
2. When prompted to enter a file location, press Enter to save it in the default directory (~/.ssh/id_rsa
).
3. Set a passphrase for additional security, or press Enter for no passphrase.
Step 3: Start the SSH Agent
The SSH agent runs in the background to manage your SSH keys.
- Start the agent:
eval "$(ssh-agent -s)"
2. Add your private key to the SSH agent:
ssh-add ~/.ssh/id_rsa
Step 4: Add Your SSH Key to GitHub
To allow GitHub to recognize your machine, add your public SSH key to your GitHub account.
- Copy your public key to the clipboard:
cat ~/.ssh/id_rsa.pub
2. Go to your GitHub account settings:
- Navigate to Settings > SSH and GPG keys.
- Click New SSH key.
- Paste the copied key into the “Key” field and give it a title (e.g., “Work Laptop”).
- Click Add SSH key.
Step 5: Test the Connection
- Confirm that the SSH setup is successful:
ssh -T git@github.com
# If configured correctly, you’ll see a message like:
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Step 6: Clone the Private Repository
Now you can clone the private repository:
- Copy the SSH URL of the repository (e.g.,
git@github.com:username/repository.git
). - Run the
git clone
command
git clone git@github.com:username/repository.git
Troubleshooting
- 404 Error After Setting Up SSH:
Ensure the repository URL is correct and that your GitHub account has access to the repository. - Permission Denied (Public Key):
Check if the SSH key is added to the agent and your GitHub account. Runssh-add
again if needed.
By following these steps, you should be able to clone any private repository without issues. SSH authentication not only makes the process smoother but also enhances security, making it a valuable skill.