Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save danielhass/e3daa43f718c62a4b7a6126f120df337 to your computer and use it in GitHub Desktop.
Save danielhass/e3daa43f718c62a4b7a6126f120df337 to your computer and use it in GitHub Desktop.
Reusing ssh-agent from Git Bash in Visual Studio Code - .bashrc
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
echo "Starting ssh-agent and adding key"
agent_start
ssh-add
echo "Setting Windows SSH user environment variables (pid: $SSH_AGENT_PID, sock: $SSH_AUTH_SOCK)"
/c/Windows/System32/cmd.exe //c "setx SSH_AGENT_PID $SSH_AGENT_PID"
/c/Windows/System32/cmd.exe //c "setx SSH_AUTH_SOCK $SSH_AUTH_SOCK"
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
echo "Reusing ssh-agent and adding key"
ssh-add
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 0 ]; then
echo "Reusing ssh-agent and reusing key"
ssh-add -l
fi
unset env
@danielhass
Copy link
Author

This .bashrc makes an instance of the Git on Windows included ssh-agent reusable across sessions and applications.

Its original version can be found here: https://vilimpoc.org/blog/2021/04/02/reusing-ssh-agent-from-git-bash-in-visual-studio-code/ and was developed by @nuket

Please find the reasoning for the update here: https://twitter.com/FillFeile/status/1537808523719020545

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