git ssh passphrases on windows

on *nix machines, the passphrases could be saved with the keychain at ease.

on windows machines, the key could be added to ssh-agent. then while the ssh-agent is running, it will save the hassle to keep asking for passphrases during git operations.

here are the steps:

in git bash, add below to have the key added to ssh-agent on bash start:

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 #the path to the private key; ssh-add ~/.ssh/gitlab
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then     
     ssh-add #the path to the private key; ssh-add ~/.ssh/gitlab
fi 
unset env

Ref: https://docs.github.com/en/free-pro-team@latest/github/authenticating-to-github/working-with-ssh-key-passphrases

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s