Skip to content

Instantly share code, notes, and snippets.

@Phundamentals
Last active July 6, 2023 00:30
Show Gist options
  • Save Phundamentals/68f212191330e857c6b0a34d53db90a2 to your computer and use it in GitHub Desktop.
Save Phundamentals/68f212191330e857c6b0a34d53db90a2 to your computer and use it in GitHub Desktop.
Load SSH agent on bash (interactive) startup, add default SSH key, enable signing commits
This is for the gist name only.
#
# Load SSH agent and add key if it's not running already.
# Source: https://docs.github.com/en/authentication/connecting-to-github-with-ssh/working-with-ssh-key-passphrases#auto-launching-ssh-agent-on-git-for-windows
#
# touch ~/.ssh/allowed_signers
# git config --global gpg.ssh.allowedSignersFile "/c/Users/.../.ssh/allowed_signers"
# echo "$(git config --global --get user.email) namespaces=\"git\" $(cat ~/.ssh/id_rsa.pub)" >> ~/.ssh/allowed_signers
# git log --show-signature
#
# START SSH AGENT ->
SSH_AGENT_ENV=~/.ssh/AGENT_ENV
agent_load_env () { test -f "${SSH_AGENT_ENV}" && . "${SSH_AGENT_ENV}" >| /dev/null ; }
agent_start () {
(umask 077; ssh-agent >| "${SSH_AGENT_ENV}")
. "${SSH_AGENT_ENV}" >| /dev/null ;
# This will set SSH_AUTH_SOCK on Windows for new processes, such as your IDE.
SETX SSH_AUTH_SOCK $SSH_AUTH_SOCK
}
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 -t 1200
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add -t 1200
fi
unset SSH_AGENT_ENV
# <- END SSH AGENT
[user]
...
signingKey = C:/Users/.../.ssh/id_rsa
...
[gpg]
format = ssh
[gpg "ssh"]
allowedSignersFile = C:/Users/.../.ssh/allowed_signers
[commit]
gpgSign = true
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment