Enhancing Git Security with SSH

SSH (Secure Shell) is a protocol used to securely communicate with remote servers. In Git, SSH is commonly used to authenticate and encrypt connections between your computer and remote repositories like GitHub, GitLab, or Bitbucket.

Using SSH enhances security by eliminating the need to enter a password every time you push or pull changes. Instead, it uses a secure key-pair system: a private key (kept on your computer) and a public key (stored on the server).

Setting Up SSH for Git

Step 1: Check for Existing SSH Keys

Before creating a new SSH key, check if you already have one:

  1. Open a terminal (or Git Bash on Windows).
  2. Run the command:
				
					ls -al ~/.ssh

				
			

If you see files like id_rsa and id_rsa.pub, you already have SSH keys. Otherwise, proceed to the next step.

Step 2: Generate a New SSH Key

If you don’t have an existing SSH key, generate one:

  1. Use the following command:

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

				
			
  • -t rsa: Specifies the key type as RSA.
  • -b 4096: Sets the key length to 4096 bits for extra security.
  • -C: Adds a label (your email) to identify the key.

When prompted:

  • Location: Press Enter to save it in the default directory (~/.ssh/id_rsa).
  • Passphrase: (Optional) Enter a passphrase for additional security or leave it blank.

Navigate to below directory 

C:\Users\username\.ssh

Open id_rsa.pub with notepad and copy the content

Step 3: Add the SSH Key to the SSH Agent

The SSH agent manages your private keys. Add your key to it:

Start the SSH agent

				
					eval "$(ssh-agent -s)"

				
			

Add the SSH key

				
					ssh-add ~/.ssh/id_rsa

				
			

Step 4: Add Your SSH Key to the Git Server

You now need to share your public key with your Git server (e.g., GitHub).

Display the public key:

				
					cat ~/.ssh/id_rsa.pub

				
			

Go to your Git hosting platform:

  • GitHub: Navigate to Settings > SSH and GPG keys > New SSH Key.
  • GitLab: Go to Settings > SSH Keys.
  • Bitbucket: Go to Personal Settings > SSH Keys.

Paste the public key, give it a name, and save.

Add copied content to the Github Account 
Click on New SSH Key

Paste the SSH Key in your Github account and click on Add SSH Key

Using SSH with Git not only secures your connection but also simplifies the authentication process. By following the steps outlined here, you can enhance your Git security and improve your workflow. For advanced users, managing multiple keys and periodically rotating them further strengthens your setup. Happy coding !❤️

Table of Contents

Contact here

Copyright © 2025 Diginode

Made with ❤️ in India