How to clone git repo with SSH key

Introduction

In initial days when GitHub was allowing https​ based URL clone, and committing code with https​ with user authentication, we were using the command like: git clone https://repo_url​. It still works, but once you clone the code and try to commit, it fails.

I decided to clone with ssh​  and what are the steps I taken to clone the repository with ssh​. Post clone what i did to permanent add the .pem​ file in git config.

Let's start from generating a private key first to add in the GitHub account. 

1. Create a private key file.

Note: For windows users, you need PuTTy installed on your system, or you can use git bash​  if git is installed.

Open the terminal PuTTy or git bash or linux terminal, and run the given command:

ssh-keygen -t rsa -b 4096 -C "[email protected]"


You may use different algorithms on your choice.

ssh-keygen -t rsa -b 4096
ssh-keygen -t dsa
ssh-keygen -t ecdsa -b 521
ssh-keygen -t ed25519

The next step will ask you for key password, location of saving the key file.

This will generate 2 files, private (key/pem) and public (.pub). till now all good. Now we have to look for the .pub​  file content, which is basically the public key, which will further in GitHub.

Navigate to settings, marked in yellow. And go to SSH and GPG key section as:


Click in New SSK key, marked in yellow, and paste the contents of the .pub​ file in the next open forms, followed by key name.


2. Clone repository with SSH key.

There's a lot of resources available for this, just for an example you may refer:

https://stackoverflow.com/questions/4565700/how-to-specify-the-private-ssh-key-to-use-when-executing-shell-command-on-git

Why I need to go through all this. If you willing to get help from #chatGPI or any other tools, these tools have their own logics, how they are trained, not all time correct.

Let's see the command used for cloning repository with ssh​.


sudo git -c core.sshCommand="ssh -i private_key_path/privatekey" clone git@repository-url.git


This above command will clone the repository in some folder. go to the cloned folder and run git status or sudo git status

This will give you message like:

On branch main
Your branch is up to date with 'origin/main'.
nothing to commit, working tree clean


But when you will try to git pull, it will not work giving error below, as your ssh key file is not set properly.

[email protected]: Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.


3. Setting private key path in .git/config​file

Go to cloned project directory and follow next command.

Use the command like:

git config --global core.sshCommand "ssh -i /home/user/.ssh/github-key"

or

git config --local core.sshCommand "ssh -i /home/user/.ssh/github-key"


---local = Local in specific folder.

---global = Global git setting on system.


Now you will be able to run git pull​, git push​  everything. This solution is 100% working and tested multiple times on different OS.


Hope you find this helpful!! 

How to clone git repo with SSH key
Ram Krishna February 11, 2025
Share this post
Sign in to leave a comment
Kubernetes and Containerization: Best Practices and Tutorials for Orchestration