Setting up SSH and Git on Windows 10
Tags: windows, ssh, git
Published: 2019-07-18 23:12:29

Welcome to my first official guide on Dev.to. Today I want to explain how you can setup SSH and Git on your Windows 10 computer.

**Note**: This is not about 100% securing your keys but about how to generate keys for use with GitHub.

Thanks to [garethdd](https://dev.to/garethdd) for his constructive feedback.

## What is SSH?

SSH stands for **Secure Shell** and is an awesome way to authenticate yourself on remote servers (for example the Github server) without typing in a password everytime.

SSH works via two keys, the **Private Key** and the **Public Key**. While the private key should always stay private and safe, the public key can be shared around the internet without any problems.

The private key allows you to get access to servers that have your public key registered, so your access can only be stolen if the attacker somehow gets your Secret Key so keep it safe!

SSH should be preinstalled on new Windows 10 machines.

## What is Git?

Git is a free version management tool that helps you to versionize your code and potentially save it on a remote server (for example Github, Gitlab or Bitbucket).

You can install Git from here:
https://git-scm.com/download/win

You can also install Git via [chocolatey](https://chocolatey.org/):

```sh
choco install git -Y
```

## Create a SSH Key

The first step is to generate a new SSH key. Use cmd or Powershell and run the following command:

```sh
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
```

You can but don't need to give it a passphrase since you should **never** share your secret key around but using one will secure your keys. Keep in mind that everybody can have as many private keys as they want.

This generates a new private SSH key with rsa encryption and 4096 bits. It also generates a public key from the secret key which you can share around.

There will be a new folder and files in your Windows user folder.

In general you can create as many keys as you want. The **id_rsa** key is the default key generated by ssh and will be automatically be used by your ssh-agent if you don't tell it to use another key.

## What is an ssh-agent?

An ssh-agent is the agent process used to actually authenticate yourself with ssh. There are a few out there (PuTTY with Pageant for example) but for this example we'll use the ssh-agent provided by the native and default Windows 10 ssh-agent.

If you want to you can use PuTTY and Pageant to make your keys even more secure. Read this post on [Digital Ocean](https://www.digitalocean.com/community/tutorials/how-to-use-pageant-to-streamline-ssh-key-authentication-with-putty) for more information.

If you want to change the key used by your ssh-agent, you must first start the service. The service will be disabled on Windows 10 by default. Search for Services and open the Services settings and look for the "OpenSSH Authentication Agent" and Activate it:

![](https://thepracticaldev.s3.amazonaws.com/i/4vw72d4y7h1spzr7t45s.png)

Now you will be able to access the ssh-agent from your console via `ssh-agent`.

For this example we're going to try to load another key called `example` into our agent and use it instead of the `id_rsa` key. To do this you can run the following command:

```sh
ssh-add example
```

Now you will have both keys available for this session.


## Register your SSH Key on Github

The next step is to register your generated SSH key on Github. For that, run the following command:

```sh
type C:\Users\your_user_name\.ssh\id_rsa.pub
```

and copy the output string into your clipboard. Now go to your [Github keys settings](https://github.com/settings/keys) and add a new SSH key with your public key and save it.

Congratulations! You now are able to get and push code to Github without any password!

**Note**: There should also be a `C:\Users\your_user_name\.ssh\id_rsa` file. This is your private key, don't share this around!

## Setup Github in your Shell

Now it's time to setup Git on your machine. After installing it from the link above, open a new cmd or Powershell window. Now we need to set your public Git name and Git email address. This will always be public when pushing code.

Luckily Github gives you a privatized email address for use. Go to https://github.com/settings/emails and you will find a `@users.noreply.github.com` email address for your account. Copy this email address.

Next register your name and email in Git:

```
git config --global user.name "Your Name"
git config --global user.email your_email@users.noreply.github.com
```

Congratulations! Now all your Commits will be registered as being commited from your Github user.

## Signing your GitHub commits (Optional Step)

To sign your commits you first must install the [GPG command line tools](https://www.gnupg.org/download/). After you installed the GPG toolkit, you can run the following command to generate a new gpg key:

```sh
gpg --full-generate-key
```

This will ask you what kind of key you want. Go for RSA and RSA.

![](https://i.imgur.com/ZicWfXM.png)

Now you need to enter a bit length. The recommendation is **4096 bits**.

After that you can specify a expiration length or if the key should never expire. Pick as you want. Expiring keys are more secure in general because you have to renew them every now and then.

Now enter your personal informations to verifying your identity with your gpg key.

When you're done you will be asked for a passphrase. Give it a secure passphrase and you will be done with your gpg-key generation.

After that you will be able to find your key in your users `.gnupg` folder as specified in the success message.

If you want to list your gpg keys, simply run

```sh
// short version
gpg --list-secret-keys

// long version
gpg --list-secret-keys --keyid-format LONG
```

Your GPG key you can share with Github is the key coming after `sec rsa4096/` so for example in

```
/Users/hubot/.gnupg/secring.gpg
------------------------------------
sec   4096R/3AA5C34371567BD2 2016-03-10 [expires: 2017-03-10]
uid                          Hubot 
ssb   4096R/42B317FD4BA89E7A 2016-03-10
```

the gpg key would be `3AA5C34371567BD2`

To get your public key block, simply run

```sh
gpg --armor --export YOUR_GPG_KEY
```

which will output your public GPG Key Block. Copy it and paste it to your GitHub Account [here](https://github.com/settings/keys).

From now on your commits will be signed when commited.

## Use Git

Now you're ready to actually use Git. From now you can clone repositories via `git clone` or push new code to Github. Here is a quick reference:

```sh
# Clone a repository to the current directory
git clone [REPOSITORY_CLONE_URL]

# Create a new commit with a message
git commit -m "Your commit message"

# Add files to the commit
git add .
git add ./filename.ext

# Push your commits to Github
git push origin master
git push origin [YOUR_BRANCH_NAME]

# Reset your repo to the last version
git reset --hard

# Create a new branch
git checkout -b [YOUR_BRANCH_NAME]

# Switch branches
git checkout [YOUR_BRANCH_NAME]
git checkout master

# Reset a single file
git checkout ./filename.ext
```

-----

### Conclusion

Thanks for reading this post. I hope it helped you with the setup. If you need help or have questions let me know!