Git, Windows, private keys and the command line
Posted on 2015-03-11
Getting Git to work on Windows is starting to get easier. Especially with GUIs like SourceTree or TortoiseGit.
For hardcore people like me, a command line interface is almost necessary and that’s where it gets trickier. When installing git, you get the command line interface and most features work. But when you want to use public/private key pairs (awesome features usually supported by the likes of GitHub or BitBucket), the basic installation doesn’t work. Git tries to use ssh.exe, which tries to load keys via ssh-agent, which doesn’t work on Windows.
So putty comes to the rescue!
plink.exe and pageant.exe are required for this setup.
The first step is to tell git to use plink.exe for SSH. It needs an environment variable named
The second step is to load your keys with pageant.exe before calling your commands.
I personally modified my PowerShell profile to do the following:
The first part is to make sure that
~ resolves to my home directory in PowerShell.
The second is to check if plink.exe is available on the
PATH and then update my environment variable to point to it.
The last step is to list all private keys in my
.ssh folder and tell pageant.exe to load them.
Since I start PowerShell automatically anytime I start my computer, it asks for the private key passwords and then any other tab I open doesn’t ask again.
This can also be set up the regular way, by modifying your environment variables directly and setting up a startup script for your Windows user account.
git pull and
git push will start working like it’s no big deal!