Using below procedure, you shall be able to setup SSH-Agent to run automatically whenever GitBash is launched on Windows. The ssh-agent process will continue to run until you log out, shut down your computer, or kill the process. Also you may need to add your SSH keys separately unless already loaded or available as per your config.
However the main advantage is that no duplicate SSH-Agent process will be created for successive / multiple Gitbash sessions.
The automatic ssh-agent launch can be implemented in two ways i.e
- By using '$HOME/.bashrc' in this case it applies to your default bash shell/linux terminal (Method A : Bullet A1-A4)
- By using '/.config/git', git XDG-Dir, in this case it applies only to your GitBash prompt. (Method B : B1-B4)
Both methods have been described below, you can use either one of these as per your requirement.
On windows, generally the user home directory is "C:/Users/username/"
.
You can also use the following commands to find your home direcotry :
eval echo ~$USER
echo $HOME
Check if the following files exist in the users home directory, If files don't exist then you should create them :
- .bash_profile
- .bashrc
The resulting directory structure may look like this (this may differ depending upon your system/OS) :
~ C:/Users/username/
|-- .bashrc
|-- .bash_profile
Recommended:
If you are unsure about what you are doing, please help yourself with further reading under Reference Link section below.
Update the '.bash_profile' file in user home folder by adding the following two lines :
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
The above statements perform test to check if the mentioned file/s exist and if they do exist, then these statement shall further source these files. Hence this ensures that ~/.bashrc will be loaded for all your interactive shells sessions (both login and non-login shells sessions).
Update the '.bash_profile' file in user home folder by adding the following :
# Start SSH Agent
#----------------------------
SSH_ENV="$HOME/.ssh/environment"
function run_ssh_env {
. "${SSH_ENV}" > /dev/null
}
function start_ssh_agent {
echo "Initializing new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "succeeded"
chmod 600 "${SSH_ENV}"
run_ssh_env;
}
if [ -f "${SSH_ENV}" ]; then
run_ssh_env;
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_ssh_agent;
}
else
start_ssh_agent;
fi
Though the above steps are sufficient to automatically check & start your SSH-Agent everytime you launch your GitBash. However, If you also want to automatically add any keys to the SSH-Agent after startup, you can further add the following line to your above script inside the start_ssh_agent function:
ssh-add <path-to-your-private-key>;
For example, if your keys are in standard '.ssh' folder then you need to add:
ssh-add ~/.ssh/<key-file-name>;
So the final script looks like this (don't forget to replace '<key-file-name>'
with your ssh-key file name in the below script):
# Start SSH Agent
#----------------------------
SSH_ENV="$HOME/.ssh/environment"
function run_ssh_env {
. "${SSH_ENV}" > /dev/null
}
function start_ssh_agent {
echo "Initializing new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "succeeded"
chmod 600 "${SSH_ENV}"
run_ssh_env;
echo "Loading keys into ssh-agent..."
ssh-add ~/.ssh/<key-file-name>;
}
if [ -f "${SSH_ENV}" ]; then
run_ssh_env;
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_ssh_agent;
}
else
start_ssh_agent;
fi
Note: If you have set passphrase on your ssh-keys, you will be prompted to input passphrase before continuing to shell session.
By default the GitBash prompt settings / configuration come from shell script called 'git-prompt.sh'. This is usually hosted inside 'profile.d' directory inside the GitBash installation directory. Navigate to the installation path on your system for GitBash. On windows, this is generally found in the following directory :
C:\Program Files\Git\etc\profile.d\
On windows, generally the user home directory is "C:/Users/username/"
.
You can also use the following commands to find your home direcotry :
eval echo ~$USER
echo $HOME
Check if the following path (folders) exist in the users home directory, If the directories don't exist then create them :
*.config
* git
After above step, you should have the following directory path by now : "C:\Users\UserName\.config\git\"
.
Keep the below script ready which is also same as mentioned in the step A4 above :
# Start SSH Agent
#----------------------------
SSH_ENV="$HOME/.ssh/environment"
function run_ssh_env {
. "${SSH_ENV}" > /dev/null
}
function start_ssh_agent {
echo "Initializing new SSH agent..."
ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo "succeeded"
chmod 600 "${SSH_ENV}"
run_ssh_env;
}
if [ -f "${SSH_ENV}" ]; then
run_ssh_env;
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_ssh_agent;
}
else
start_ssh_agent;
fi
Same as the step A5 above. Refer Step A5 👆
Navigate to path described in above step B3 i.e "C:\Users\UserName\.config\git\"
If you already have an existing 'git-prompt.sh' at this location ("C:\Users\UserName\.config\git\"
) then leave it as-is.
If you dont have an existing 'git-prompt.sh' at this location then navigate to default 'git_prompt.sh' at C:\Program Files\Git\etc\profile.d\git_prompt.sh
and copy this file into "C:\Users\UserName\.config\git\"
location
Finally you should have the full path as "C:\Users\UserName\.config\git\git-prompt.sh"
~C:\Users\UserName\
|-- .config
|-- git
|-- git-prompt.sh
Open your 'git-prompt.sh' from your user home directory as described in above step B5 i.e "C:\Users\UserName\.config\git\git-prompt.sh"
.
Copy the automatic 'ssh-agent launch script' from step B4 into your 'git-prompt.sh' located here i.e "C:\Users\UserName\.config\git\git-prompt.sh"
. Finally save and close the 'git-prompt.sh'
That is it! Done!
If the above changes have been implemented correctly and have settled well, you should see the following everytime you launch GitBash :
Initializing new SSH agent...
succeeded
Apparantly you will see 'ssh-agent.exe' running in windows task manager / processes
Setup SSH Authentication for GitBash By Brandon Sara