DEV Community

Cover image for Git | Connecting to GitHub and Pushing Changes Using SSH on Windows
Hassan BOLAJRAF
Hassan BOLAJRAF

Posted on

Git | Connecting to GitHub and Pushing Changes Using SSH on Windows

Note
You can check other posts on my personal website: https://hbolajraf.net

Connecting to GitHub and pushing changes using SSH on a Windows machine involves several steps:

  1. Generate SSH Key Pair
  2. Add SSH Key to GitHub Account
  3. Configure SSH for GitHub
  4. Clone Repository or Add Remote URL
  5. Commit and Push Changes

Step-by-Step Instructions

1. Generate SSH Key Pair

  1. Open Git Bash on your Windows machine.
  2. Generate a new SSH key pair by running:
   ssh-keygen -t rsa -b 4096 -C "hassan.bolajraf@gmail.com"
Enter fullscreen mode Exit fullscreen mode

Replace hassan.bolajraf@gmail.com with your GitHub email address. When prompted, save the key to the default location (/c/Users/your_username/.ssh/id_rsa) and optionally set a passphrase.

2. Add SSH Key to GitHub Account

  1. Copy the SSH key to your clipboard. You can use the following command to do this:
   cat ~/.ssh/id_rsa.pub | clip
Enter fullscreen mode Exit fullscreen mode
  1. Log in to your GitHub account.
  2. Go to Settings > SSH and GPG keys.
  3. Click New SSH key.
  4. Paste the copied SSH key into the key field and give it a descriptive title.
  5. Click Add SSH key.

Image description

3. Configure SSH for GitHub

  1. Ensure your SSH agent is running. Start the SSH agent by running:
   eval "$(ssh-agent -s)"
Enter fullscreen mode Exit fullscreen mode
  1. Add your SSH private key to the SSH agent:
   ssh-add ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

4. Clone Repository or Add Remote URL

If you haven't cloned your repository yet, you can do so using SSH:

  1. Go to your repository on GitHub.
  2. Click on the Code button and copy the SSH URL (it looks like git@github.com:username/repository.git).
  3. Clone the repository:
   git clone git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

If you already have a repository cloned using HTTPS and want to switch to SSH, you can change the remote URL:

  1. Navigate to your repository directory in Git Bash.
  2. Change the remote URL:
   git remote set-url origin git@github.com:username/repository.git
Enter fullscreen mode Exit fullscreen mode

5. Commit and Push Changes

  1. Make your changes and stage them for commit:
   git add .
Enter fullscreen mode Exit fullscreen mode
  1. Commit your changes:
   git commit -m "Your commit message"
Enter fullscreen mode Exit fullscreen mode
  1. Push your changes to GitHub:
   git push origin main
Enter fullscreen mode Exit fullscreen mode

Replace main with the name of your branch if you're working on a different branch.

Additional Tips

  • Verify SSH Connection: You can test your SSH connection to GitHub by running:
  ssh -T git@github.com
Enter fullscreen mode Exit fullscreen mode

This should return a success message like Hi username! You've successfully authenticated.

  • SSH Key Management: If you manage multiple SSH keys, you can create a ~/.ssh/config file to specify which key to use for GitHub:
  Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_rsa
Enter fullscreen mode Exit fullscreen mode

What next ?

By following these steps, you should be able to connect to GitHub and push changes using SSH on your Windows machine.

Top comments (0)