Monday, January 10, 2011

git <-> svn

You may guess now I like working with git. I am always amazed how easily I can match the abilities of this software with the several, often changing needs that arise during my work or hobbies. Oh right, it cannot help in skating faster...
And then there is Subversion. I should not say I hate it but I think it is a slow dinosaur, ugly, fat and slow-moving. But still, many projects use it so if you want to participate in them, you've got to follow the flow.
And there comes git-svn to the rescue. It enables you to use your beloved git in a subversion-based project easily. This is how I've set up my workplace for Speed Dreams development, using git for effective feature branching, mergeing, rebasing and such git candy:
mkdir sd
git svn init
git svn fetch -r3096 (of course you can choose another SVN revision)
git svn rebase
and voila you have a nice and shiney git repository that mimics the SVN repo.
Anytime you want to update your repo (like doing 'svn up'), just issue
git svn rebase
The above process is explained in more detail here (thanks, Flavio), along another entry here.

Now onto feature branching:

I quite like the Story branch pattern, so basically what I do is:

1) Find a ticket to work on in the Speed Dreams Trac system, let's say it is ticket #100.
2) git co -b 100_ticket_short_title this creates and switched to a new git branch that I will use for this ticket only
3) hack this & that
4) git add -p
5) git ci -m 'Commit message that really tells something. Re #100' - so I always try to include the ticket number in the commit message, good for Trac coverage.
reiterate 3-4-5 until ticket is finished
6) git diff --cached (review all the work done)
7) git rebase -i HEAD^8 (or so, view last 8 commits and squash them together if needed - this to get rid of the side effects of very frequent git commits)
Now before committing to SVN let's make sure it won't cause any conflicts:
8) git svn fetch && git svn rebase (OK maybe git svn fetch isn't needed)
If still 'green', then commit to SVN:
9) git svn dcommit
Now switch back to the master branch and update it, too:
git co master
git svn rebase
Set the ticket status fixed and delete the branch, if you like. I prefer to keep it around for some weeks in case the ticket is reopened after the initial tests, but you can get rid of it or create a new branch then - really just a question of taste.

The only thing you should really take note is: you cannot do a git svn rebase if you have any uncommitted changes. Fabio's blog says he uses git stash to put away his changes, does the rebase, then does git stash apply to get his changes back. I prefer to commit my changes instead, do the rebase and then work on the usual way. Which one you choose is up to your taste - both approaches work fine.

This is the real git magic - you can choose the way you work!