Skip to content

Instantly share code, notes, and snippets.

@bsara
Last active February 6, 2026 09:37
Show Gist options
  • Select an option

  • Save bsara/5c4d90db3016814a3d2fe38d314f9c23 to your computer and use it in GitHub Desktop.

Select an option

Save bsara/5c4d90db3016814a3d2fe38d314f9c23 to your computer and use it in GitHub Desktop.
Setup SSH Authentication for Git Bash on Windows

Setup SSH Authentication for Git Bash on Windows

Prepararation

  1. Create a folder at the root of your user home folder (Example: C:/Users/uname/) called .ssh.
  2. Create the following files if they do not already exist (paths begin from the root of your user home folder):
    • .ssh/config
    • .bash_profile
    • .bashrc

Create a New SSH Key

Follow the steps in the section named "Generating a new SSH Key" found in the following documentation from GitHub: Generating a new SSH key and adding it to the ssh-agent

Configure SSH for Git Hosting Server

Add the following text to .ssh/config (.ssh should be found in the root of your user home folder):

Host github.com
 Hostname github.com
 IdentityFile ~/.ssh/id_ed25519

Enable SSH Agent Startup Whenever Git Bash is Started

First, ensure that following lines are added to .bash_profile, which should be found in your root user home folder:

test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc

Now, add the following text to .bashrc, which should be found in your root user home folder:

# 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;

  ssh-add ~/.ssh/id_ed25519;
}

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
@rajasiman
Copy link

I have been using this script for a very long time, thank you for the script @bsara, this script had been working fine until recently something changed in git for windows i guess? The ssh-agent was being spawned every single time a new bash window was opened.

My message has nothing to take away from your inputs. I did some googling, found this article and the code snippet from the article stopped the ssh-agent from being spawned every time a new bash window was launched. I am posting both the link to the article and the code snippet for future readers, cheers!

Instructions

  1. Follow all of the original steps until the step to paste the contents into .bashrc
  2. Instead of pasting the content from the original gist into .bashrc, use the following.

In the snippet below you can set the path to your key file in the if block like this ssh-add ~/.ssh/id_ed25519 where ~/.ssh/id_ed25519 is the path to your keyfile if you have a different location/name.

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
    agent_start
    ssh-add
    # ssh-add ~/.ssh/id_ed25519
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
    ssh-add
    # ssh-add ~/.ssh/id_ed25519
fi

unset env

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