DEV Community

Cover image for How to Link git to GitHub via SSH on Windows
Rowland
Rowland

Posted on

How to Link git to GitHub via SSH on Windows

As a developer, learning about github can feel great at first, I mean who doesn't need a secure cloud storehouse for their codes? The importance and use of git and GitHub cannot be overemphasized but there come a point in time where you just get tired and bored of always;

  1. Heading to github.com,
  2. Locating your repo
  3. Adding new files to the repo
  4. Clicking and dragging the files from your PC into GitHub
  5. And finally writing a commit message.

All these just to push your code to GitHub, what if you needed to make just a minor change? You'd have to go through that mini hell again? Actually, you don't have to because all these steps can be shortened to;

git add .
git commit -m "minor fix"
git push
Enter fullscreen mode Exit fullscreen mode

Yeah that's right, just these 3 commands above does all that work for you and fast.

But before you acquire such power, there's a price you'd have to pay. and that is, you'd have to setup a connection between git and GitHub first which luckily isn't that hard at all. I am going to share those steps in this post.

 

Step 1: Download and install git

Alright, the first step is to have git installed in your PC, if you already have it, great! If not just click here and download a version suitable for you PC

git website for downloading git

After download, start the install wizard and follow the prompts to install.

Upon installation of git, 3 other applications would also be automatically installed in your PC: Git cmd, Git GUI and Git Bash. These are just the different ways of interacting with git.

Configuring git

Open Git Bash, which should look similar to the image below

Git Bash for windows

Run these commands:

git config --global user.name "Your Name"

#This name will be associated with your commits 
Enter fullscreen mode Exit fullscreen mode
git config --global user.email "Your email Address"

#This email will be associated with your commits 
Enter fullscreen mode Exit fullscreen mode

You can verify that these has been set by running

git config --list
Enter fullscreen mode Exit fullscreen mode

 

Step 2: Generate SSH Key

Installing and configuring git was so easy right, now to an easy but not so easy step, generating an SSH key;

What is an SSH key and why do we need it?

To answer that let's first understand what SSH even mean.
SSH stands for Secure Shell, and it's a network protocol (like HTTPS but different) that allows secure communication between devices over an insecure connection.

Meanwhile an SSH key is a pair of cryptographic keys (a public key and a private key) used to authenticate a secure connection between your computer and a remote server (in this case, GitHub). Put simply, it's a file containing encrypted texts used for authetication.
We need it to authenticate to GitHub without our username or password.

To generate the key, open Git Bash and run these commands:

ssh-keygen -t rsa -b 4096 -C "your-email@example.com"

#Replace "your-email@example.com" with the email address associated with your GitHub account.
Enter fullscreen mode Exit fullscreen mode

Let's breakdown some of the command arguments above:

ssh-keygen - This generates a new SSH key pair (a public key and a private key)

-t rsa - This sets the type of SSH encryption to rsa (Rivest-Shamir-Adleman) which is the most widely used. Other types includes;
ECDSA (Elliptic Curve Digital Signature Algorithm)
ED25519 (Edwards-curve Digital Signature Algorithm 25519)

-b 4096 - This sets the number of bits of the generated key to 4096. The other possible value for rsa encryption is 2048 which is less secure.

-C your-email@example.com - This adds a comment to the key (usually your email address)

When prompted after running the above command, press Enter to accept the default file location for the file containing the generated SSH key.

Next, you can optionally set a passphrase for extra security or simply leave the field blank and press Enter twice.

Lastly, copy the public key to your clipboard with the following command.

clip < ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

or

cat ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

This will display the public key on your Git Bash terminal, then you can manually highlight and copy the public SSH key

 

Step 3: Add SSH Key to Github and link to Github repository locally

Now for the third and final step, do these:

  1. Log in to your GitHub account.
  2. Go to Settings > SSH and GPG keys.
  3. Click New SSH key.
  4. In the Title field, add a descriptive name (e.g., "git ssh")
  5. Paste your public key into the Key field. (The one you copied earlier)
  6. Click Add SSH key.

Adding ssh key to github

Confirm successfull connection

To confirm the connection between git and GitHub, open Git Bash and run the command:

ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

if connection was successful you'll a message similar to:

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
Enter fullscreen mode Exit fullscreen mode

Else simply reply yes multiple times when prompted. Afterwards, run the first command again to reconfirm.

Linking to a GitHub repo from VS code terminal

Congratulations on Linking your git to your GitHub via SSH. Goodnews is you only need to go through all those linking steps once. Now comes the easiest part.

Goto GitHub and create a new repo or open an existing repo
Click on SSH

GitHub new repo
For a new repo without any files that looks like the above pic:
Click on SSH and copy the url

For an already existing repo with files, open the repo, click Code > click on the ssh tab and copy the url
Open your project folder in VS and click on Terminal > New Terminal
run the following command:

git clone git@github.com:rowleks/test-repo.git
Enter fullscreen mode Exit fullscreen mode

Next navigate to the recently cloned repo (folder) on your PC project folder and open the newly cloned repo in VS code

Open the VS code terminal and run the following commands

git add .
Enter fullscreen mode Exit fullscreen mode
git commit -m "Initial commit"
Enter fullscreen mode Exit fullscreen mode
git branch -M main
Enter fullscreen mode Exit fullscreen mode
git push origin main
Enter fullscreen mode Exit fullscreen mode

And that's it.

Subsequently all you need to do to push to GitHub from that repository are the commands:

git add .
git commit -m "commit message"
git push
Enter fullscreen mode Exit fullscreen mode

Thank you for reading! I hope this helped.

Top comments (0)