Skip to content

Instantly share code, notes, and snippets.

@jherax
Forked from bsara/git-ssh-auth-win-setup.md
Last active September 4, 2024 19:07
Show Gist options
  • Save jherax/979d052ad5759845028e6742d4e2343b to your computer and use it in GitHub Desktop.
Save jherax/979d052ad5759845028e6742d4e2343b 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/username/) called .ssh.
    You can run something like: mkdir -p ~/.ssh
  2. Create the following files if they do not already exist (paths begin from the root of your user home folder):
    • touch ~/.ssh/config
    • touch ~/.bash_profile
    • touch ~/.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

You can use your favorite text editor: nano, vim, subl, atom, code
For example, run code ~/.ssh/config

Then, add the following lines to .ssh/config which should be found in the root of your user home folder:

AddKeysToAgent yes
Host github.com
 Hostname github.com
 IdentityFile ~/.ssh/id_rsa

Enable SSH Agent Startup Whenever Git Bash is Started

Run your editor, e.g. code ~/.bash_profile and add the following lines:

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

Run your editor, e.g. code ~/.bashrc and add the following snippet:

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

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

thanks, that was helpful

@maccaroo
Copy link

Thanks. Greatly appreciated.

@silasquaresma
Copy link

Top! Thanks!

@sneuder
Copy link

sneuder commented Jun 19, 2023

Sr, thank you so much. It really worked, and I modified to have multiples accounts and multiple repo origin

@torcoste
Copy link

Thanks!

@khgoldberg
Copy link

Perfect. Thank you!

@ersurajkumar1999
Copy link

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond

Please proved any solution

Thanks
Er Suraj Kumar

@amikosolucoes
Copy link

Sr, thank you so much. It really worked, and I modified to have multiples accounts and multiple repo origin

Can you share, pls?

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