Git :)

With the weather being a bit rubbish the past few weekends I have been geeking out (in addition to playing Uncharted 1 & 2) and been learning wpf, mvvm pattern and I decided to start using an open source source code control system. After some deliberation I picked Git (I did look at subversion and mercurial), it seems to have gained popularity and I wanted to see what all the fuss was about and if it was written and used by Linus for the linux kernel then it should be good enough for me :)

After some investigation I found msysgit which is basically git on windows. It effectively installs a bash shell on your windows box with all the git commands. I also decided to use Unfuddle as an online store of my source code. Here are the steps I took to get setup, mainly so that I can find them again if I need to do this later…

Installing msysgit was pretty straight forward, I accepted the defaults and it installed just fine. (This was after discovering the right package to download, as that is not immediately obvious, but it’s here, the git package, rather than the msysgit package)

Setting up git to work with unfuddle and setting up ssh keys etc was the next step, and also setting up the repo to work with Visual Studio, which is basically setting up a .gitignore file to tell git not to include certain files and filetypes in the source code control

Once git is up and running you should then set up some global config, an ssh-key can be created using the following:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]ssh-keygen –C “your@email.com” –t rsa[/sourcecode]

This will create the key in a folder called .ssh in your user folder. You then use this key to add to unfuddle (or github or wherever) so that you can be authorised against the remote repo.

Then set your username and email:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config -–global user.name “Your Name”

git config –-global user.email "Your@Email.com”[/sourcecode]

In your user folder there should be a .profile file, if there isn’t then you can use

[sourcecode language="bash" gutter="false" 1="tab-size="4"" toolbar="true"]touch .profile [/sourcecode]

which will create a file without a filename and only an extension (you can't do this in explorer) Then add this to it, which starts up the SSH Agent when bash is run, so that you only have to enter the ssh key password once.

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]SSH_ENV=$HOME/.ssh/environment

function start_agent {

echo "Initialising new SSH agent...";

/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"

echo succeeded

chmod 600 "${SSH_ENV}"

. "${SSH_ENV}" > /dev/null

/usr/bin/ssh-add;

}

# Source SSH settings, if applicable

if [ -f "${SSH_ENV}" ]; then

. ${SSH_ENV} > /dev/null

#ps ${SSH_AGENT_PID} doesn't work under cywgin

ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {

start_agent;

}

else

start_agent;

fi

[/sourcecode]

This question on SO tells you how to change git’s default editor, I changed it from vi to notepad. (I use notepad2, but it’s installed to take over from notepad, so opening notepad.exe opens notepad2)

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config –-global core.editor “notepad.exe”[/sourcecode]

This question on SO tells you how to change the default merge tool to p4merge, which is my merge tool of choice:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git config --global merge.tool p4merge

git config --global mergetool.p4merge.cmd ‘p4merge.exe \”$BASE\” \”$LOCAL\” \”$REMOTE\” \”$MERGED\”’[/sourcecode]

Then that should be the environment set up, after that its just a case of either creating a new repo or cloning one from unfuddle.

This page has the basic info on how to do it:

1) If you are starting a new repo, create it in unfuddle, then create a folder locally for it then:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git init[/sourcecode]

1b) you might also want to create a .gitignore file in the repo folder, so that you can tell git to only commit your source code, mine looks like this (you can tell where I ‘borrowed’ it from):

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]# .gitignore for .NET projects

# Thanks to Derick Bailey

# http://www.lostechies.com/blogs/derickbailey/archive/2009/05/18/a-net-c-developer-s-gitignore-file.aspx

# Additional Thanks to

# - Alexey Abramov

# Standard VS.NET and ReSharper Foo

obj

bin

*.csproj.user

*ReSharper*

*resharper*

*.suo

*.cache

*Thumbs.db

*.scc

# Other useful stuff

*.bak

*.cache

*.log

*.swp

*.user

_compareTemp

_notes

aspnet_client

httpd.parse.errors

# Office Temp Files

~$*

# If you have a deploy folder

deploy

deploy/*

# Exclude ALL DLLs?

#*.dll[/sourcecode]

2) Associate the repo with the unfuddle remote repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git remote add unfuddle git@subdomain.unfuddle.com:subdomain/abbreviation.git

git config remote.unfuddle.push refs/heads/master:refs/heads/master[/sourcecode]

2a) If you already have code in the repo you can clone it

3) Finally, write some code, and when you are happy and want to commit the changes to your local repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git add *

git commit[/sourcecode]

4) Then push the changes to the remote unfuddle repo:

[sourcecode language="bash" gutter="false" firstline="1" toolbar="true"]git push unfuddle master[/sourcecode]

Comments are closed