Post

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

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:

    1
    
    ssh-keygen -t rsa -b 4096 -C "hassan.bolajraf@gmail.com"
    

    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:

    1
    
    cat ~/.ssh/id_rsa.pub | clip
    
  2. Log in to your GitHub account.
  3. Go to Settings > SSH and GPG keys.
  4. Click New SSH key.
  5. Paste the copied SSH key into the key field and give it a descriptive title.
  6. Click Add SSH key.

ScreenShot1

3. Configure SSH for GitHub

  1. Ensure your SSH agent is running. Start the SSH agent by running:

    1
    
    eval "$(ssh-agent -s)"
    
  2. Add your SSH private key to the SSH agent:

    1
    
    ssh-add ~/.ssh/id_rsa
    

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:

    1
    
    git clone git@github.com:username/repository.git
    

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:

    1
    
    git remote set-url origin git@github.com:username/repository.git
    

5. Commit and Push Changes

  1. Make your changes and stage them for commit:

    1
    
    git add .
    
  2. Commit your changes:

    1
    
    git commit -m "Your commit message"
    
  3. Push your changes to GitHub:

    1
    
    git push origin main
    

    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:

    1
    
    ssh -T git@github.com
    

    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:

    1
    2
    3
    4
    
    Host github.com
      HostName github.com
      User git
      IdentityFile ~/.ssh/id_rsa
    

What next ?

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

This post is licensed under CC BY 4.0 by the author.