I would like to improve on the accepted answer
Downsides of using .bashrc
with eval ssh-agent -s
:
- Every git-bash will have it's own ssh-agent.exe process
- SSH key should be manually added every time you open git-bash
Here is my .bashrc
that will eradicate above downsides
Put this .bashrc
into your home directory (Windows 10: C:\Users\[username]\.bashrc
) and it will be executed every time a new git-bash is opened
and ssh-add
will be working as a first class citizen
Read #comments to understand how it works
# 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
Also this script uses a little trick to ensure that provided environment variables are shared between git-bash instances by saving them into an .env
file.