Published on

Setting Up SSH and Git with Github on Window 10

Setting Up SSH and Git can be tricky on Windows compared to Linux or Mac, but if you follow the steps in this article, your should have no problems using Git on Windows.

This article will show you the steps to install and setup SSH and Git on Windows 10 then connect with a remote Git server (github).

What is SSH?

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

SSH works via two keys, the public key and the private key. While the private key should always stay private and safe, the public key is what we use to authenticate ourselves on the remote server.

The private key allows you to get access to the remote server (github) that have your public key registered, so your access can only be stolen if the attacker has access to your private key.

Note: SSH should be preintalled on new Windows 10 machines, but if you don't have it, you can install it with the following command:

powershell
Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'

What is Git?

Git is free version control system, which is used to track changes in your code. Git is a distributed version control system (DVCS) that allows you to store your code in a central repository on remote server (for example Github, Gitlab, or Bitbucket) and share it with others.

if you don't have git installed, you can install it follow this article here.

You can also install Git via winget

powershell
winget install -e --id Git.Git

Steps for Setting Up SSH and Git With Github on Windows 10

Step 1: Create SSH Key

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

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

Output:

generate-ssh-key-on-window-10

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 that you can share with the remote server.

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 specify a 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 change the key used by your ssh-agent, you must first start the service. The service will be disabled on Windows 10 by default. You can enable it by running the following command:

powershell
Set-Service -Name ssh-agent -StartupType Automatic
Set-Service -Name ssh-agent -Status Running

Note: PowerShell should be run as Administrator to be able to execute the command.

Or enabling SSH agent from Services

  • Open Services (Start Menu -> Type "Services")
ssh-agent-services-on-window-10
  • Select OpenSSH Authentication Agent and click on it.
select-ssh-agent-services-on-window-10
  • Set StartupType to Automatic
set-startup-type-services-on-window-10
  • Start the service
start-ssh-agent-services-on-window-10

That's it

You should now be able to execute ssh-keygen, ssh-agent and ssh-add from PowerShell without any problem.

Step 2: Add SSH Key to Github

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

powershell
notepad C:\Users\your_username\.ssh\id_rsa.pub
add-ssh-key-to-github-on-window-10

and copy the content of the file into your clipboard.

copy-ssh-key-to-github-on-window-10

Now open the Github website and go to your github account Settings.

add-ssh-key-to-github-on-window-10

scroll down your page and click on SSH and GPG keys.

add-ssh-key-to-github-on-window-10

Now you see the Add SSH key button.

add-ssh-key-to-github-on-window-10

Give the title of your SSH key and paste the content of your clipboard and click on Add SSH key.

add-ssh-key-to-github-on-window-10

Congratulations, you have successfully added your SSH key on Github and now your able to get and push code to Github without any password!

Note: There should also be a C:\Users\your_username\.ssh\id_rsa file in your Windows user folder. This is your private key, don't share this around!

Step 3: Setup Github in your Shell

Now it's time to setup Git on your computer. 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.

powershell
git config --global user.name "your_git_name"
git config --global user.email "your_git_email"

Example:

git-config-on-window-10

That's it

All configuration is done, now you can check your configuration is correct by running the following command:

powershell
ssh -T git@github.com

Output:

ssh-check-on-window-10

Step 4: Use Git

Now you're ready to use Git. From now you can clone repositories from Github and push code to Github. Here is a quick reference:

powershell
# 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 article. I hope it helped you with the setup. If you need help or have questions let me know on Whatsapp +91 9560403991.