Setting up GitBash with SSH on Windows
Introduction
Git is a everyday tool for me. I remember reading about Distributed Version Control from Joel Spolsky years ago, when we decided to switch from SVN to GIT.
Although I use it daily, most of the time it is - set and forget, when it comes to SSH keys to repositories. So whenever I get a new laptop or need to re-initialize repo, I have to re-teach myself the steps to get it working. So this is a bit of a guide, to help me navigate and get back on track when im lost next time.
When we first start GitBash we are started in a home directory and it looks something like below.
If we want to clone a git repository we would type something like the below
git clone git@github.com:username/test.git
Create a New SSH Key
When we do this we are faced with Permission denied error. So we need to setup a SSH key locally and on the git server. Substitute with your email address.
ssh-keygen -t ed25519 -C "User.Name@test.com"
If your using a legacy system that doesn’t support ED25519 use below;
ssh-keygen -t rsa -b 4096 -C "User.Name@test.com"
This will create 2 key files locally one publickey with postfix of .pub and one passphrase protected key with no postfix.
Local Setup
Although this key is registered we need to run the ssh-agent and add the key to it.
# start the ssh-agent in the background
$ eval "$(ssh-agent -s)"
> Agent pid 59566
Add your SSH private key to the ssh-agent. If you created the key with a different name, replace the id_rsa with the name of your key.
ssh-add /c/NotBackedUp/id_rsa
Remote Setup
Not it is registered locally, we also need to give the key to the repository. So we sign into github and add the key
Navigate to Settings > SSH and GPG Keys
Click on New SSH Key
and copy and paste the contents of the .pub file extension that was created earlier.
Now we can attempt to clone the directory again.
git clone git@github.com:username/test.git
Yay, it is working. Well, Yes and No, if you close gitbash and reopen it, these setting will all be forgotten so you would need to follow the “local” steps Each time you relaunch gitbash. Not really ideal.
Automate the launching of Agent and Keys
In gitbash if we setup a .ssh directory and a loading script we can have gitbash auto associate the keys and load the agents.
mkdir -p ~/.ssh
We can also create the following files if they don’t exist
touch ~/.ssh/config
touch ~/.bash_profile
touch ~/.bashrc
If you open up ~/.ssh/config you can add the following text
Host github.com
Hostname github.com
IdentityFile ~/.ssh/id_rsa
The Host and HostName may differ if you have different repository URL’s you use at work.
Now we need to make sure the SSH Agent Starts whenever Git Bash is started. So we edit the ~/.bash_profile
test -f ~/.profile &&.~/.profile
test -f ~/.bashrc &&.~/.bashrc
and lastly edit the ~/.bashrc
# 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
Test to see if it all works, close gitbash and re-open. Navigate to a repository and run;
cd test
git pull test
It will ask you for your passphrase and will authenticate.