Skip to content

Instantly share code, notes, and snippets.

@modjke
Last active May 3, 2019 15:55
Show Gist options
  • Save modjke/2b42487dba2c1b184ea6cef8c8ccdde3 to your computer and use it in GitHub Desktop.
Save modjke/2b42487dba2c1b184ea6cef8c8ccdde3 to your computer and use it in GitHub Desktop.
Enable ssh-agent automatic launch and sharing between multiple git-bash instances (windows, git-bash)
# Enable ssh-agent automatic launch and sharing between multiple git-bash instances
#
# Env vars used
# SSH_AUTH_SOCK - ssh-agent socket, should be set for ssh-add or git to be able to connect
# SSH_AGENT_PID - ssh-agent process id, should be set in order to check that it is running
# SSH_AGENT_ENV - env file path to share variable between instances of git-bash
SSH_AGENT_ENV=~/ssh-agent.env
# import env file and supress error message if it does not exist
. $SSH_AGENT_ENV 2> /dev/null
# if we know that ssh-agent was launched before ($SSH_AGENT_PID is set) and process with that pid is running
if [ -n "$SSH_AGENT_PID" ] && ps -p $SSH_AGENT_PID > /dev/null
then
# we don't need to do anything, ssh-add and git will properly connect since we've imported env variables required
echo "Connected to ssh-agent"
else
# start ssh-agent and save required vars for sharing in $SSH_AGENT_ENV file
eval $(ssh-agent) > /dev/null
echo export SSH_AUTH_SOCK=\"$SSH_AUTH_SOCK\" > $SSH_AGENT_ENV
echo export SSH_AGENT_PID=$SSH_AGENT_PID >> $SSH_AGENT_ENV
echo "Started ssh-agent"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment