Updated: 2023-03-31

Generally, these are what I did:

Common problems

Netwrok folder not accessable

ssh: Could not resolve hostname personal: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

It could be that access to folder .ssh in drive H: is not available. Check that drive H: is accessable.

Warning: Remote Host Identification has changed

You might not able to access to your remote account and get this message:

WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!

IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.

You need to remove the old key in known_hosts file in folder .ssh by running this command in Git bash

ssh-keygen -R github.com

The next git connection will ask you if you trust the new SSH key.

openSSH

Windows 10 include openSSH. But I can’t make it work with git so I use git-with-openssh installed via scoop. This will install Git and openSSH together. After installation, it’s important to check where is your ~/.ssh root folder via Git bash. This is the folder where you will save your SSH-key.

Since I use Emacs, I add this code to use git-bash in shell. This is taken from StackExchange.

  (if (equal system-type 'windows-nt)
      (progn (setq explicit-shell-file-name
                   "C:/Users/ybka/scoop/apps/git-with-openssh/current/bin/bash.exe")
             (setq shell-file-name explicit-shell-file-name)
             (setq explicit-bash.exe-args '("--login" "-i"))
             (add-to-list 'exec-path "C:/Users/ybka/scoop/apps/git-with-openssh/current/bin")
             (setenv "SHELL" shell-file-name)
             (add-hook 'comint-output-filter-functions 'comint-strip-ctrl-m)))

  (defun git-bash () (interactive)
         (let ((explicit-shell-file-name "C:/Users/ybka/scoop/apps/git-with-openssh/current/bin/bash.exe" ))
           (call-interactively 'shell)))

Then as suggested from StackExchange, I created ~/.bash_profile (root for Git-bash) and add the codes below to display shell prompt properly in Emacs.

  if [ -n "$INSIDE_EMACS" ]; then
  export PS1='\[\033[32m\]\u@\h \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ '
  fi

To be on the safe side, I also add git-with-openssh the PATH in Windows Environment. You can find guides for doing it in many places eg. here. In my pc, the path is:

   C:/Users/ybka/scoop/apps/git-with-openssh/current/bin/

Create SSH key

If you haven’t created an SSH-key then do so by running the code below in your Git bash. Else you can read more here.

$  ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/ybka/.ssh/id_rsa): id_rsa_personal

The file id_rsa_personal will be placed at inside /c/Users/ybka/.ssh/ if you don’t specify the path explicitly. Two files will be created ie.

Paramters specification:

Then add your newly created SSH-key to ssh-agent to manage your key. If your ssh-agent haven’t been started then you can do so silently and has it running in the background. You will get an output something like > Agent pid 4353. Run this command to start ssh-agent for Windows in PowerShell:

start-ssh-agent.cmd

or run silently with eval $(ssh-agent -s). Then adding your ssh-key with:

  ssh-add ~/.ssh/id_rsa_personal

For Windows user, you can auto-launching the ssh-agent by following the guide here. Basically adding file ~/.profile or ~/.bashrc in your git ssh root which you find via Git bash. The content of ~/.bashrc file is:

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
elif [ "$SSH_AUTH_SOCK" ] && [ $agent_run_state = 1 ]; then
ssh-add
fi

unset env

Copy your public SSH-key either manually or by running clip which will copy it to clipboard then add it to your GitHub account.

  clip < ~/.ssh/id_rsa_personal.pub

You can test the connection and verify your loaded SSH-key

  # Check connection
  ssh -T git@github.com

  # Verify
  ssh-add -l -E md5
ssh  git 
comments powered by Disqus