Skip to content

Instantly share code, notes, and snippets.

@jdmallen
Last active December 19, 2024 17:10
Show Gist options
  • Select an option

  • Save jdmallen/27cc54a7183b437c01779d68d72eb5e2 to your computer and use it in GitHub Desktop.

Select an option

Save jdmallen/27cc54a7183b437c01779d68d72eb5e2 to your computer and use it in GitHub Desktop.
My .bashrc file that I use in Windows alongside the PowerShell profile, which loads a common SSH agent for git
# Starts an SSH agent for use with git.
# I designed this to share the same agent between PowerShell and Git Bash.
# Make sure you set $GIT_SSH at the User or System level to point to
# whichever ssh-agent.exe PowerShell is using, or else this won't work.
function start_agent {
/c/Windows/System32/OpenSSH/ssh-agent.exe > /dev/null
# Output agent's PID to file
ps -W | grep ssh-agent | awk '{print $4}' | head -n 1 > ~/.ssh/pid
/c/Windows/System32/OpenSSH/ssh-add.exe ~/.ssh/id_rsa.pri # or wherever your private keys are
# Add as many private keys as you like, but be warned: ssh will try to
# establish a connections with each loaded identity, even the wrong ones.
# After so many fail, it will quit, even if you have the correct identity
# loaded, several private keys down the line.
# If that happens, simply make your ssh connections with the -i flag; e.g.,
# `ssh -i ~/path/to/id_rsa [email protected]`
}
# Look for PID file
PID_FILE=~/.ssh/pid
if [[ -f "$PID_FILE" ]]; then
if (ps -W | grep $(cat $USER_PROFILE/.ssh/pid) > /dev/null); then
{
echo "SSH agent with PID $(cat $USER_PROFILE/.ssh/pid) found."
}
else
{
echo "No SSH agent found. Starting new SSH agent."
start_agent;
}
fi
else
echo "No SSH agent found. Starting new SSH agent."
start_agent;
fi
# Calling this will ensure that bash always starts in your project directory.
# Change the path below as needed.
proj() {
cd /d/_projects
}
proj
clear
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment