Create a GitHub Repo and Clone It Locally Using SSH and TortoiseGit (Windows)
Techelopment
Posted on September 13, 2024
Cover photo by Roman Synkevych on Unsplash
The basics
Before we talk about ssh authentication and cloning, let's build the foundation for using git.
Git Setup for Windows
First download Git ( https://git-scm.com/download/win ) and install it. You can use all the default settings that the installation wizard offers you but remember to select the options for git bash:
TortoiseGit Installation
TortoiseGit is a tool that assists you with git operations (clone, commit, push, branch, …) through a simple and intuitive UI.
You can install TortoiseGit by downloading it from the official website https://tortoisegit.org/download/.
During the installation steps make sure to select “OpenSSH” as the default ssh client:
At the end of the installation you will be asked if you want to run the setup wizard (absolutely yes!), check the “First Start Wizard” checkbox and click “Close”.
This will launch the wizard to help you configure the basic settings of TortoiseGit. Make sure that TortoiseGit recognizes the Git we installed in the “ Setting up Git for Windows” section:
The wizard will also ask you if you want to set “Name” and “Email” for your commits. You decide whether to do this at this stage (I did 😉) or once you have finished the wizard in the TortoiseGit settings.
Git and TortoiseGit setup done! 🎉
Now let's move on to the SSH configuration to commit and push (and all other git operations) using public and private keys.
SSH
Now we will create a public key and a private key to use as an authentication method for working with our github repositories instead of the usual user credentials and password.
Public and private key generation
To generate your public and private key pair, open the prompt (Win+r, type cmd, then enter) and run the following command:
ssh-keygen -t ed25519 -C "your_email@example.com""your_email@example.com"
When asked to enter the file where to save the key, leave it blank and press Enter. You can choose a passphrase to make key generation even more secure (I didn't 😭):
C:\Users\user>ssh-keygen -t ed25519 -C "your_email@example.com")"your_email@example.com" )
Generating public/private ed25519 key pair.
Enter file in which to save the key (C:\Users\user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in C:\Users\user/. ssh/id_ed25519
Your public key has been saved in C:\Users\user/.ssh/id_ed25519.pub
In your user folder (C:\Users<user with which you logged into Windows>) you will find a folder called “.ssh” (if you don’t find it immediately, refresh the user folder) inside which there will be:
- public key — file“id_ed25519.pub”
- private key — file “id_ed25519” (⚠️ NEVER give to anyone!)
Using the public key
The public key will need to be configured in your GitHub account. To do this, log in to github.com, click on your user avatar icon in the top right and select “Settings” from the menu:
On the Settings page, select “SSH and GPG keys” from the side menu, and click on “New SSH key”:
A screen will open where you can enter your key information:
In the “Title” field, enter something that will help you remember what you are using the key for.
Open the .pub file from your C:\Users\user.ssh folder, copy the contents of the file and paste it into the “Key” field. Click “Add SSH key” to finally add your key to GitHub (GitHub may ask you to enter your password — don’t worry, it’s normal 😉)
Using the private key
To set up the private key we will need to use ssh-agent provided with the git installation (see “ Setting up Git for Windows ”). This tool is responsible for retrieving our private key in order to authenticate during commits and pushes (and other git operations).
Open Git Bashby searching for it from the Windows menu search:
Create a new file ~/.profile
with the following command:
vi ~/.profile
A blank file will open, press “i” on your keyboard to enable editing in the vi editor.
Paste the script below (trust me, it's an official github script ). This script will automatically run ssh-agent every time you open git bash:
env=~/.ssh/agent.env
agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "$env")
. "$env" >| /dev/null ; }
agent_load_env
# agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running
agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?)
if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then
agent_start
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi
unset env
To save and exit the editor you have to type in sequence Esc : wq (the w stands for write and the q for quit ) and enter:
Now let's check if our key is present in ssh-agent. Open a new git bash (so that our previous script can run) and run the following command.
ssh-add -l
If everything is ok, the command will print this message to the screen:
Now a test before we move on to creating our repo in GitHub and trying a clone! 🙂
To test if the connection with GitHub has been configured correctly, just run this command (exactly as shown below):
ssh -vT git@github.com
If everything worked correctly, after the various logs you will find a message similar to the one below which will confirm that everything is ok ✅:
Hi your_email! You've successfully authenticated
Creating a GitHub repo and clone
Log in to GitHub and from your dashboard click on “New” under “Top repositories”:
A page will open to configure the new repo. Give a name to your repo in the “Repository name” field, choose whether the repository should be public or private, check “Add a README file” (because every self-respecting repo has its README 😎) and click on “Create repository”:
To clone the repository:
- click on “Code” at the top right
- Click on the “SSH” tab
- Click on the 📑 icon to copy your repo link
Now that you have copied the cloning link you are ready (finally) to use TortoiseGit and clone your local repo.
Open the folder where you want to clone, right click and select “Git Clone…”
TortoiseGit will already offer you the link you have in memory (the one copied from GitHub) so you just have to press “OK” and wait for git to do the rest.
That's all! Enjoy your git 😊
Follow me #techelopment
Medium: @techelopment
Dev.to: Techelopment
facebook: Techelopment
instagram: @techelopment
X: techelopment
telegram: @techelopment_channel
youtube: @techelopment
whatsapp: Techelopment
Posted on September 13, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
September 13, 2024