logo
. . .

How to Connect Git to GitHub Using SSH Key

In the realm of version control and collaborative development, Git serves as a cornerstone tool, and GitHub stands tall as a leading platform for hosting repositories and fostering collaboration. To maximize security and streamline your workflow, connecting Git to GitHub via SSH key is a prudent choice. This blog post will walk you through the straightforward process, empowering you to securely manage your projects on GitHub.

Why SSH Key?

SSH (Secure Shell) keys provide a secure method for authenticating your identity without the need for repeatedly entering passwords. By utilizing SSH keys, you enhance security while seamlessly interacting with GitHub repositories.

Step 1: Generate SSH Key

Begin by generating an SSH key pair on your local machine. Open your terminal or command prompt and execute the following command, replacing “your_email@example.com” with your GitHub-associated email:

ssh-keygen -t rsa -b 4096 -C “your_email@example.com”
This command prompts you for the desired location to save the key pair and an optional passphrase for added security.

Step 2: Add SSH Key to GitHub

Next, you’ll need to add your SSH public key to your GitHub account. Retrieve the public key by running:

pbcopy < ~/.ssh/id_rsa.pub
For Windows and Linux users without pbcopy, manually open id_rsa.pub and copy its contents. Then, navigate to your GitHub account settings, select “SSH and GPG keys,” and click “New SSH key.” Paste the copied key into the designated field and assign a recognizable title.

Step 3: Test SSH Connection

To verify successful SSH key configuration, establish a connection with GitHub:

ssh -T git@github.com
Upon execution, you should receive a confirmation message, signaling that you’ve authenticated with GitHub via SSH.

Step 4: Update Git Remote URL (Optional)

If you’ve previously configured Git with an HTTPS remote URL, transitioning to SSH is a breeze. Simply update the remote URL with the SSH variant using:

git remote set-url origin git@github.com:username/repository.git
Replace username/repository with your GitHub username and repository name.

Conclusion

With these simple steps, you’ve seamlessly connected Git to GitHub using SSH key authentication. Embrace the enhanced security and convenience this setup offers, empowering you to collaborate efficiently and safeguard your projects with confidence.