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:
- Generate SSH Key Pair
- Add SSH Key to GitHub Account
- Configure SSH for GitHub
- Clone Repository or Add Remote URL
- Commit and Push Changes
Step-by-Step Instructions
1. Generate SSH Key Pair
- Open Git Bash on your Windows machine.
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
Copy the SSH key to your clipboard. You can use the following command to do this:
1
cat ~/.ssh/id_rsa.pub | clip
- Log in to your GitHub account.
- Go to Settings > SSH and GPG keys.
- Click New SSH key.
- Paste the copied SSH key into the key field and give it a descriptive title.
- Click Add SSH key.
3. Configure SSH for GitHub
Ensure your SSH agent is running. Start the SSH agent by running:
1
eval "$(ssh-agent -s)"
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:
- Go to your repository on GitHub.
- Click on the Code button and copy the SSH URL (it looks like
git@github.com:username/repository.git
). 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:
- Navigate to your repository directory in Git Bash.
Change the remote URL:
1
git remote set-url origin git@github.com:username/repository.git
5. Commit and Push Changes
Make your changes and stage them for commit:
1
git add .
Commit your changes:
1
git commit -m "Your commit message"
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.