In order to securely push, pull, and clone between your local Git installation and a remote GitHub or GitLab repository, you must first create an SSH key pair that can be used to both identify yourself and authenticate your local Git installation. with the remote GitHub or GitLab. server you are trying to connect to.
GitHub SSH KeyGen Tools
There are several ways to create SSH keys for Git. In the past, I often used PuttyGen as my GitHub SSH KeyGen tool of choice, especially if I was working in a Windows environment. But for Ubuntu and Windows 10 or 11 users, there is a much better GitHub SSH KeyGen tool that comes pre-installed on the OS. it bears its name well ssh-keygen.
To create SSH keys for GitHub and Git, simply open a terminal window in Ubuntu or PowerShell in Windows and run the ssh-keygen order.
SSH KeyGen on Windows and Linux
On Ubuntu, the GitHub SSH KeyGen command is invoked like this:
[email protected]~/.ssh$ ssh-keygen -o -t rsa -C "[email protected]"
On Windows, the SSH KeyGen command is invoked like this:
PS C:sshkeygenexample> ssh-keygen -o -t rsa -C "[email protected]"
By the way, SSH support on Windows has been around since 2018, so as long as your installation is up to date, you don’t need Putty, PuttyGen, or Plink to use the GitHub SSH KeyGen tools.
The GitHub SSH KeyGen tool generates two files that correspond to the public SSH key and the private SSH key. SSH keys are named is_rsa and id_rsa.pub.
[email protected]~/.ssh$ ls is_rsa id_rsa.pub
Git SSH key locations
the ssh-keygen The command will ask you if you want to put the generated keys in a custom folder. Reject this option and accept the defaults.
On Linux and Windows, SSH keys should go to a folder named .ssh located at the root of the user’s home directory. For example, on Windows where the user who ran the GitHub SSH KeyGen command was named Owner, the public and primary keys would be listed from the following folder:
PS C:UsersOwner.ssh> dir Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 1/28/2022 8:02 PM 2602 id_rsa -a---- 1/28/2022 8:02 PM 572 id_rsa.pub
Copy the public SSH key
The contents of the public key file named id_rsa.pub needs to be copied and pasted into the GitHub SSH configuration window, so cat the file if you’re on Ubuntu, or open the file in a text editor on Windows and copy the key.
GitHub SSH Setup
With the public key value copied to the clipboard, log in to GitHub and navigate to your account settings.
In your account settings you will see an SSH and GPG link. Open this link, then add a new SSH key.
Paste the public key value into the text field and give the key a unique name. Save your changes and your GitHub SSH key setup is almost complete.
Clone GitHub repositories with SSH
Every GitHub repository has a green Coded button that provides CLI, HTTP, and a GitHub SSH URL to use with the git clone command. The SSH link for the repository to clone in this GitHub SSH KeyGen example is:
To clone your repository, simply open a terminal window in Ubuntu or a command prompt in Windows and run the following Git clone command:
PS C:sshkeygenexample> git clone [email protected]:cameronmcnz/github-made-real-easy.git Cloning into ‘github-made-real-easy’… Authenticity of GitHub SSH KeyGen Example host ‘ (22.214.171.124)’ can’t be established. Do you still want to connect(yes or no)? yes Receiving objects: 100% (222/222), 2.23 MiB | 3.41 MiB/s, done. Resolving deltas: 100% (111/111), done.
There will be a warning about SSH key authentication. Just type “yes” when the prompt occurs and allow the repository to clone itself.
Push to GitHub with SSH KeyGen keys
Once the SSH connection to GitHub is successfully established, all subsequent push, pull, and fetch operations will run over a secure connection using the keys generated by the SSH keygen operation. No additional flags or options are required.
So an SSH push to GitHub from a repository that has already been cloned over SSH would look like this:
PS C:sshkeygenexample> git pull origin Everything up to date!
And this is how you securely connect Git to GitHub with the keys generated by SSH KeyGen.