Setup SSH keys for GitHub and GitLab on Windows

1. Open PowerShell or Git Bash and run:


                # For GitHub
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_github
 
# For GitLab  
ssh-keygen -t ed25519 -C "your-email@example.com" -f ~/.ssh/id_gitlab
            

3. Ensure the SSH Agent Service is Running

If you are using Windows OpenSSH (default in Windows 10/11), you must ensure the ssh-agent service, which securely stores your keys in memory, is running.

Open PowerShell as Administrator and run:


                # Set the ssh-agent service to start automatically
Get-Service ssh-agent | Set-Service -StartupType Automatic
 
# Start the ssh-agent service
Start-Service ssh-agent
 
# Verify it's running
Get-Service ssh-agent
            

4. Create and modify: ~/.ssh/config

You need to tell SSH which key to use for which service. Check if the ~/.ssh/config file exists. If it doesn’t, create it using Notepad or your favorite editor:


                # In Git Bash:
touch ~/.ssh/config
notepad ~/.ssh/config
 
# Or in PowerShell:
New-Item -Path ~/.ssh/config -ItemType File -Force
notepad ~/.ssh/config
            

Add this configuration to the file:


                Host github.com
  HostName github.com
  User git
  IdentityFile ~/.ssh/id_github
 
Host gitlab.com
  HostName gitlab.com
  User git
  IdentityFile ~/.ssh/id_gitlab
            

5. Add keys to the SSH Agent:

Now, register your newly created keys with the SSH agent:


                ssh-add ~/.ssh/id_github
ssh-add ~/.ssh/id_gitlab
            

6. Copy public keys (one at a time):

You need to copy the contents of the .pub file to your clipboard.


                # Using Git Bash:
clip < ~/.ssh/id_github.pub
clip < ~/.ssh/id_gitlab.pub
 
# Or using PowerShell:
Get-Content ~/.ssh/id_github.pub | Set-Clipboard
Get-Content ~/.ssh/id_gitlab.pub | Set-Clipboard
            

After copying, log in to GitHub/GitLab and navigate to Settings > SSH Keys to add them.

7. Test the connection:


                ssh -T git@github.com
ssh -T git@gitlab.com
            

You should see a success message welcoming you!