PowerShell - Using Git with SSH Keys on Windows 10
Using Git on Windows is pretty straight forward. You can just install Git for Windows and use either the Git GUI app or Git Bash console.
But what if you use PowerShell as your default console of choice and don't want to switch? Furthermore, what if you're connecting via SSH keys?
Well with the help of a couple of modules you can setup PowerShell to automatically import SSH keys on load.
Install software and modules
- Download Git for Windows - https://git-scm.com/download/win
- Install Git, selecting all the defaults is fine. Adding GIT to PATH environment variable is recommended.
- Open a PowerShell console as administrator, and type:
PowerShellGet\Install-Module posh-git -Scope CurrentUser -AllowPrerelease -Force
Install-Module posh-sshell -Scope CurrentUser
# NOTE: If the AllowPrerelease parameter is not recognized, update your version of PowerShellGet:
# Install-Module PowerShellGet -Scope CurrentUser -Force -AllowClobber
- You may be asked to trust packages, just accept that and continue on.
posh-git is a PowerShell module that integrates Git and PowerShell. It Includes:
- Prompt for Git repositories - shows current branch information in the prompt
- Tab completion for git commands.
posh-sshell is a PowerShell module that provides utilities for working with SSH connections. We're mainly using it to ensure the Ssh-Agent is running.
Setup PowerShell profile
Now that everything is installed. You'll want a way to automatically import the modules on load. You can configure your PowerShell profile to import those modules in each new session.
- Open your PowerShell profile in notepad by running the following command in the console:
# $profile.CurrentUserAllHosts - is the profile used by all consoles including integrated terminals like VS Code.
# $profile.CurrentUserCurrentHost - is the profile used by the current terminal you are using.
- Add the following code to it and save your changes.
- Close your current console and open a new one. If you navigate to a directory that is a git repository you should now see your prompt update like below:
- Any SSH keys in
C:\Users\[Username]\.ssh\id_rsa.pubare automatically loaded. You can add more by using ssh-add command.
- If the directory or keys don't exist, then you can use ssh-keygen to generate a new key, this will automatically create the relevant folders and files.
And that's it. Now when you open a new PowerShell console, you should be able to connect to remote sources using SSH. If your key has a passphrase then you will be prompted for that only when the ssh-agent service starts.
PowerShell exception - 'git is not recognized as the name of a command'
Ensure you have installed Git for Windows and that the path to git is in your PATH environment variable.
Unable to authenticate to remote git server
If it's the first time you are connecting to the remote host you may need to trust it. You can run the following command to test the connection:
ssh -T firstname.lastname@example.org
# Replace connection with the service you are connecting to.
You may see a warning that the authenticity of the host can't be established. Type: 'yes' to accept connection. You should now be able to connect if your ssh key is valid.