Git for Windows: Mastering PublicKey Authentication Made Easy

Introduction
Public key authentication is a more secure and convenient method compared to password authentication for accessing Git repositories over SSH. This article will guide you through the process of setting up public key authentication for Git on Windows, ensuring that you can securely and efficiently manage your repositories.
Prerequisites
Before you begin, ensure that you have the following:
Git for Windows
: This should be installed on your system. You can download it from
Git’s official website
.
SSH Client
: Git for Windows comes with an SSH client, but if you prefer another, make sure it’s installed and configured.
Basic Understanding of SSH
: Familiarity with SSH (Secure Shell) and how it works will be helpful.
Step 1: Generating SSH Key Pair
Open Git Bash
Open the Start menu, type
Git Bash
, and launch the application.
Generate SSH Key
In the Git Bash window, enter the following command to generate a new SSH key pair:
ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
-t rsa
: Specifies the type of key to create. RSA is the most common.
-b 4096
: Specifies the number of bits in the key. 4096 is a strong, recommended size.
-C “your_email@example.com”
: Adds a comment to the key, usually your email address.
Press
Enter
to start the key generation process.
Save the Key
The system will ask you where to save the key. By default, it suggests
~/.ssh/id_rsa
. You can press
Enter
to accept the default location or specify a new path.
Add Passphrase (Optional)
Next, you will be prompted to enter a passphrase. This adds an extra layer of security. If you choose to use a passphrase, you will need to enter it every time you use the key. For now, you can leave it blank by pressing
Enter
.
Verify Key Generation
Once the key pair is generated, you can verify it by listing the contents of your SSH directory:
ls ~/.ssh
You should see two files:
id_rsa
(private key) and
id_rsa.pub
(public key).
Step 2: Copying the Public Key to Clipboard
Use the following command to copy the public key to your clipboard:
clip < ~/.ssh/id_rsa.pub You can verify that the key is copied by pasting it into a text editor. Step 3: Adding the Public Key to Your Git Account GitHub Log in to your GitHub account. Click on your profile picture in the top right corner and select Settings . In the left sidebar, click on SSH and GPG keys . Click on New SSH key or Add SSH key . Give your key a descriptive name. Paste the copied public key into the Key field. Click Add SSH key . GitLab Log in to your GitLab account. Click on your profile picture in the top right corner and select Settings . In the left sidebar, click on SSH Keys . Paste the copied public key into the Key field. Optionally, add a title for your key. Click Add key . Bitbucket Log in to your Bitbucket account. Click on your profile picture in the top right corner and select Personal settings . In the left sidebar, click on Security . Scroll down to SSH keys and click Add key . Give your key a label and paste the copied public key into the Key field. Click Add . Step 4: Testing the Connection GitHub In Git Bash, type the following command to test the connection: ssh -T git@github.com If successful, you should see a message like: Success! You now have access to GitHub's git repositories. GitLab Use the following command to test the connection: ssh -T git@gitlab.com Bitbucket Use the following command to test the connection: ssh -T git@bitbucket.org Step 5: Configuring SSH for Multiple Accounts If you need to manage multiple Git accounts, you can set up SSH aliases for each account. Create SSH Config File Open Git Bash and create or edit the