DEV Community

Cover image for Connecting to Github with SSH (Windows)
Vinit Gupta
Vinit Gupta

Posted on

Connecting to Github with SSH (Windows)

Earlier, GitHub used to allow browser login. Those were the simpler times.

But now, to push your code to GitHub, you need to generate SSH Keys. These SSH Keys are your login credentials - for personal projects and open source contributions.

While building a project recently, I tried pushing my code to GitHub but I could not. And I got frustrated😭 like everyone of you might have gotten one day.

And then I discovered this article which was a tough to navigate, but solved my problems : SSH Keys and Github

I immediately thought of sharing this with all of you to make it simpler. 📢📢

Step 0 : Prerequisites

For using Git on your windows, it is very important to have GitBash installed in your system.

🚨 If you haven't already installed GitBash, follow this GitBash Guide to install it.

Now that you have GitBash installed on your Windows, we can begin.

Step 1 : Generate a new SSH Key

Generating an SSH key is simple, followed by storing and using it.

  • Run Git Bash
  • Generate a new SSH key by pasting the following snippet
// Replace "your_email@example.com" with your GitHub email ID
$ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Enter fullscreen mode Exit fullscreen mode
  • When you’re prompted with the following message :
Enter a file in which to save the key
Enter fullscreen mode Exit fullscreen mode

press Enter to save the key in the default location (/c/Users/username/.ssh/id_rsa). Your public key will also get saved here.

  • Now copy the SSH key to your clipboard using the following command :
$ clip < ~/.ssh/id_rsa.pub
Enter fullscreen mode Exit fullscreen mode

Step 2 : Adding the copied SSH Key to your GitHub account

Now that the key is generated, you need to add it to your GitHub account.

  • Go to Settings in your Github account to add the SSH public key.

  • Under SSH keys tab, select New SSH key.

  • Give a title and paste the key in the text area.

Step 3 : Add private SSH key to the ssh-agent

Git bash tool comes with a ssh-agent which needs to be configured to use the SSH key that you create.

Follow the steps below for an easy configuration.

  • Open Git Bash
  • Create a new ~/.profile (or) ~/.bashrc file using the following command :
$ vi ~/.profile
Enter fullscreen mode Exit fullscreen mode
  • Paste below script into your bash file that opens on running the above command:
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

Enter fullscreen mode Exit fullscreen mode

This will auto launch the ssh-agent whenever you run your git bash shell.

  • Exit the editor by typing clicking on esc button on keyboard and write : :wq and press Enter.

The above file will be saved.

And there you have it - ready to contribute to open source projects from your Command Line.

📌But wait, we are the Good Developers, so we will test if it works properly.

Step 4 : Verification

  • Open a new Git Bash window.
  • Check if the identity has been added to the ssh agent.
$ ssh-add -l
Enter fullscreen mode Exit fullscreen mode
  • Check that the key is being used by trying to connect to git@github.com.
$ ssh -vT git@github.com

OpenSSH_8.0p1, OpenSSL 1.1.1c  18 August 2023
debug1: Reading configuration data
debug1: Offering public key
debug1: Server accepts key
debug1: Authentication succeeded (publickey).
Authenticated to ssh.github.com
Enter fullscreen mode Exit fullscreen mode

Do share📤 this with your fellow developers and do not forget to leave a comment 📝 if you enjoyed this guide.

Top comments (0)