Skip to content

Instantly share code, notes, and snippets.

@stormwild
Forked from Peregrinox/git bash ssh agent.md
Created May 19, 2023 11:01
Show Gist options
  • Save stormwild/e3562b29988cfa0fdc4f15c5884ee0a8 to your computer and use it in GitHub Desktop.
Save stormwild/e3562b29988cfa0fdc4f15c5884ee0a8 to your computer and use it in GitHub Desktop.
configure your git bash to run agent on start

https://stackoverflow.com/questions/18404272/running-ssh-agent-when-starting-git-bash-on-windows

https://help.github.com/articles/working-with-ssh-key-passphrases/#auto-launching-ssh-agent-on-git-for-windows

I needed this to launch the ssh agent on my windows git bash sessions.

edit or create .profile and add:

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

and restart your shell

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment