Using Git and OpenSSH for Windows Development

I have a linux server I’m hosting over at Linode that I use to host this blog along with a git repository for my development projects. I found that there are many guides on configuring git for use with your own remote repositories on linux, but getting things set up on windows was a little different.
A few notes:
- I’m using Windows Vista, though the set up should be very similar (if not the same) for XP or Windows 7.
- I won’t cover installing git on the server, since there are already many resources on that around the web. If you’re running one of the Debian linux families it’s as simple as sudo apt-get install git
- I also won’t cover how to generate a key for use with openssh, but you can find instructions here: Public key authentication
Configuring OpenSSH:
First you need to download and install Msysgit. I’m using version Git-1.6.4-preview20090730.exe. During the installation I accepted all of the defaults. You can choose other options, but be sure to select OpenSSH as your SSH executable.
If you don’t have an ssh authentication key for working with your server yet, generate your key and upload the public key to the server. There are instructions here: OpenSSH Public Key Authentication
Now you want to configure your OpenSSH so that it’s easy to work with the server. You’ll need to create a new directory to hold the configuration files. If you used the default installation path for msysgit, open the folder C:\Program Files\Git\etc and create a new folder inside of it named “ssh”. Vista requires admin authentication to write to this folder, so you’ll see the UAC prompts if you have that turned on. Now you should have a ssh configuration folder here: C:\Program Files\Git\etc\ssh.
I found that it’s much more convenient to use OpenSSH if you create a config file to hold all of the server settings. That way when you access the server you can use a convenient name like “linode” instead of the ip address. Run Notepad or another text editor as admin (required on Vista so we can save it to the ssh directory). The contents of the file should look like this (fill in the information in [brackets] with your own server information):
Host [yourhostname] HostName [serverIpAddress] User [serverLoginName] Port [sshPort] IdentityFile [privateKeyPath]
yourhostname:
this can be any name you want to use when you access the server via ssh
serverIpAddress:
the ip address of the server where your git repository is located
serverLoginName:
the user name you use to access your server via ssh
sshPort:
the port your server is using for ssh (This is not required if you are using the default port)
privateKeyPath:
the path to the private key file you generated previously (likely called id_rsa or something similar). If the path has any spaces in it, you need to enclose it in quotes.
Save the file in your ssh directory with the name “ssh_config”. Be sure the filename does not have an extension (notepad tends to append .txt – you don’t want that).
Now you should be able to test your open ssh setup. Open Git Bash (part of msysgit) and type:
ssh [yourhostname]
and you should be logged into your server. If you private key was password protected, you’ll be prompted for the password. The only issue I had initially was that the path to my private key had a space in it, but once I added quotes around the path, it all went smoothly.
Setting Up a Project
There are two steps to setting up a project for use with git. First you need to create a folder on the server that will act as the git repository for your project. You can choose whatever path you prefer, but it should not be in a public folder. Create the folder you want to use as the repository on the server and then in the command prompt type:
git --bare init
It’s that easy to set up the server side, now for the windows project. If you’re using Visual Studio, go ahead and create a project and let Visual Studio put in in a folder for you. If you already have a project set up, you can skip that step. Now you’ll want to tell Git to ignore some of the build folders and Resharper stuff if you’re using that. You do that by using Notepad or another text editor to create a file inside the project folder named .gitignore. Here’s what mine looks like:
obj bin _ReSharper.* *.csproj.user *.resharper.user *.resharper *.suo *.cache *~ *.swp
If you’re using some other development tools, these files may vary. Full documentation of the gitignore syntax is available on the man page. Now open a Git Bash prompt and navigate to the folder where your project is on the windows box. Once you’re inside the folder, initialize it as a git repository:
git --bare init
In order to sync the repository with the server, you’ll need to add a remote origin. In order to do that you’ll need the [yourHostName] you used from the ssh_config file above and the path to the folder you are using on the server (that you initialized with git –bare init). Enter yourHostName without the brackets. Add the connection by using git remote add:
git remote add origin ssh://[yourHostName]/pathToTheServerFolder
Now all that’s left is to add your project to the git repository and push the files to the server:
git add . git commit -m "initial commit" git push origin master
To pull files from the server you’ll use:
git pull origin master
There’s much more to using git, but that should get you started!

Leave a Reply