Configuring SSH in git for Windows
Even though git for Windows basically also works in a standard command line window (cmd.exe
),
the easiest way to access a remote repo via SSH is from within a git bash
terminal window which comes with the git for Windows package.
If an SSH key already exists, the files with the associated private and public key (by default id_rsa
and id_rsa.pub
) can be copied
to the ~/.ssh/
directory as usual. The directory is the same as ${HOME}/.ssh/
, and corresponds to %HOME%\.ssh\
in native Windows notation,
e.g C:\Users\martin\.ssh\
.
If there's no existing SSH key available, run the command ssh-keygen
to generate a new pair of key files that will by default be created
in the ~/.ssh/
directory.
The ~/.profile
file (%HOME%\.profile
) can be set up to use the ssh-agent
program to make the SSH key available, whenever required.
When the first bash
window is opened after booting, the key is loaded and cached by ssh-agent
.
If the SSH key is protected by a password, the password only has to be entered once at this time.
Whenever another bash window is opened, the entries in ~/.profile
take care that the cached key can be used without additional interaction.
To use this feature, the following lines can be put into the ~/.profile
file. This can be created or edited using a simple text editor:
env=~/.ssh/agent.env agent_load_env () { test -f "$env" && . "$env" >| /dev/null ; } agent_start () { (umask 077; ssh-agent >| "$env") . "$env" >| /dev/null ; } agent_load_env # agent_run_state: 0=agent running w/ key; 1=agent w/o key; 2= agent not running agent_run_state=$(ssh-add -l >| /dev/null 2>&1; echo $?) if [ ! "$SSH_AUTH_SOCK" ] || [ $agent_run_state = 2 ]; then agent_start ssh-add elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then ssh-add fi unset env
References
- Superuser: How to make git not prompt for passphrase for ssh key on windows?
https://superuser.com/questions/1010542/how-to-make-git-not-prompt-for-passphrase-for-ssh-key-on-windows
- Github help: Working with SSH key passphrases
https://help.github.com/en/github/authenticating-to-github/working-with-ssh-key-passphrases
— Martin Burnicki martin.burnicki@burnicki.net, last updated 2020-12-15