Git | Connecting to GitHub and Pushing Changes Using SSH on Windows
Hassan BOLAJRAF
Posted on July 29, 2024
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:
- 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:
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:
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:
eval "$(ssh-agent -s)"
- Add your SSH private key to the SSH agent:
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:
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:
git remote set-url origin git@github.com:username/repository.git
5. Commit and Push Changes
- Make your changes and stage them for commit:
git add .
- Commit your changes:
git commit -m "Your commit message"
- Push your changes to GitHub:
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:
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:
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.
Posted on July 29, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.