miscellaneous_tips:20_software_development:git_notes:configuring_ssh_in_git_for_windows

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


Martin Burnicki martin.burnicki@burnicki.net, last updated 2020-12-15

  • miscellaneous_tips/20_software_development/git_notes/configuring_ssh_in_git_for_windows.txt
  • Zuletzt geändert: 2021-01-18 17:20
  • von 127.0.0.1