In my last post I set up my iMac with Git and MacVim. (Which you should read because I didn’t want to repeat a lot of those steps here.) Well, it’s not easy for me to code on the move with my iMac, so it’s time to set up my laptop which runs Vista! (I know I know, I should use Windows 7…)
Installing gVim
ANYWAY, first I want to install gVim. Following this guide, I went with the default options on the installer except I additionally checked “Create .bat files for command line use”. This way I can access Vim and gVim from the command line.
Installing Git and using Github
I then followed the Git installation guide for Windows Github wrote up. The only changes I made were a) checking “Git Bash Here” and “Git GUI Here” for installing those context menu entries (right click menu), and b) choosing “Run Git from the Windows Command Prompt” because, you know, that can be useful down the road. :P
Following the guide, I ran into the same setup steps I took on my iMac. When I had to add my Github API to the Git config through Git Bash, I did a quick Google search to figure out you can press “Insert” to paste whatever is on the clipboard right into Git Bash. Since the Tumblr template repository already exists, we can just do the following from the root:
$ git clone git@github.com:bergren2/codeendcode.git
This clones the repo into the folder codeendcode in my root, which is excellent. Sadly, though, I had to type in my SSH key again. :/ This wasn’t a problem on the Mac because they stored the passphrase for us with Keychain. But hey, we’re on Windows now. Let’s fix that!
Setting up ssh-agent
Edit: I made another attempt at setting up ssh-agent which you should check out below. (“Setting up ssh-agent… again!”) I think it’s a better approach!
Using this UCLA guide as a reference, I recognized I had to get bash to use ssh-agent when I booted it up for the first time. To fix that I just added the following to ~/.bashrc:
export SSH_AUTH_SOCK=/tmp/.ssh-socket
ssh-add -l 2>&1 >/dev/null
if [ $? = 2 ]; then
# Exit status 2 means couldn't connect to ssh-agent; start one now
ssh-agent -a $SSH_AUTH_SOCK >/tmp/.ssh-script
. /tmp/.ssh-script
echo $SSH_AGENT_PID >/tmp/.ssh-agent-pid
fi
function kill-agent {
pid=`cat /tmp/.ssh-agent-pid`
kill $pid
}
I did this using Vim, but you might want to try out the shiny gVim we installed earlier so you have an easier time using copy and paste. :) Next I added an environment variable by going to Control Panel > System > Advanced Settings > Environment Variables. There I added a variable with the name SSH_AUTH_SOCK and value /tmp/.ssh-socket. After that you’ll want to reopen Git Bash and do the following:
$ ssh-add ~/.ssh/id_rsa
You’ll be asked to enter your passphrase one last time and then tadaa, you’re good to go!
Setting up ssh-agent… again!
Apparently Github had a guide too on using ssh-agent, which I decided to follow instead. So now my ~/.bashrc instead contains the following:
SSH_ENV="$HOME/.ssh/environment"
# start the ssh-agent
function start_agent {
echo "Initializing new SSH agent..."
# spawn ssh-agent
ssh-agent | sed 's/^echo/#echo/' > "$SSH_ENV"
echo succeeded
chmod 600 "$SSH_ENV"
. "$SSH_ENV" > /dev/null
ssh-add
}
# test for identities
function test_identities {
# test whether standard identities have been added to the agent already
ssh-add -l | grep "The agent has no identities" > /dev/null
if [ $? -eq 0 ]; then
ssh-add
# $SSH_AUTH_SOCK broken so we start a new proper agent
if [ $? -eq 2 ];then
start_agent
fi
fi
}
# check for running ssh-agent with proper $SSH_AGENT_PID
if [ -n "$SSH_AGENT_PID" ]; then
ps -ef | grep "$SSH_AGENT_PID" | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
fi
# if $SSH_AGENT_PID is not properly set, we might be able to load one from
# $SSH_ENV
else
if [ -f "$SSH_ENV" ]; then
. "$SSH_ENV" > /dev/null
fi
ps -ef | grep "$SSH_AGENT_PID" | grep -v grep | grep ssh-agent > /dev/null
if [ $? -eq 0 ]; then
test_identities
else
start_agent
fi
fi
During my switch, though, I ran into an issue where ssh-agent had problems using the correct port because it was being used by… ssh-agent. 0_o Not a problem! I simply looked up the other ssh-agent’s PID using ps and then called kill <PID> to put it out of its misery.