Django Development Using git

A little background

Not very long ago I was fortunate to attend a talk given by Randall Schwartz on git, the distributed source control system developed by Linus Torvalds and the rest of the Linux kernel developers. After that talk and after watching the Google Video talk given by Linus I decided to dip my toe into the waters by installing git on my Mac and writing a few patches for Django.

Installation on a Mac using MacPorts

With MacPorts installed, installing a recent version of the svn variant of git is simple enough:

> sudo port install git-core +svn

Test to make sure git-svn works since it depends on a lot of Perl libraries. If you don't get something like the following check the git and git-svn docs:

> git-svn --version
git-svn version 1.5.2.3 (svn 1.4.4)

Creating a git repository from Django trunk

There are a couple ways to do this. I chose to use the git-svn clone command to pull down each revision from the Django repository up to the current version. Note that this will take awhile -- approximately 90 minutes depending on your network connection -- though it is interesting to watch the evolution of Django scroll by. While this was running I would occasionally see interesting parts of Django being updated and looked up repository numbers using Django's Trac interface to see what was going on.

> git-svn clone http://code.djangoproject.com/svn/django/trunk git-django

When this is done you'll end up with a new directory called git-django that will be the same as Django's subversion repository but can be managed with git.

Taking git for a drive

You can now change to the git-django directory. The next obvious thing to do is to create a branch to start working on something.

The following command is used to view the current list of branches, though on a fresh clone you should only see the branched called master at this point. The one with the asterisk by it is the current working branch:

> git branch

To create a new branch and to make that branch active:

> git branch mybranch
> git checkout mybranch

Note, you can optionally do the above 2 steps in a single step:

> git checkout -b mybranch

It would follow that you would make your modifications to Django and test things out.

One huge unexpected plus for me was that once I set up my git repository as my active Django install via site-packages, switching between branches doesn't require updating site-packages since git updates the directory in place. So git makes it super easy to keep your active patches and trunk separate and switch between them using git checkout.

Once you have some code ready, you can create a patch by committing your changes, rebasing your tree on Django's trunk, and producing a patch.

To commit all changes to the working branch:

> git commit -a

To pull down the latest changes in Django's trunk repository:

> git-svn rebase

To produce a patch by viewing the differences between the current branch and the master branch, outputting the file to the desktop to be uploaded to Trac:

> git diff master > ~/Desktop/mypatch.diff

That's about all the commands I used so far to create a few Django patches during the Django sprint (1, 2). Happy hacking!

About this entry

Date Posted:
September 17th 2007 at 12:09:00 PM

Tagged:
django, git

Previous Entry:
3 tips for web designers using Django

Next Entry:
Installing Django on Leopard (Mac OS 10.5)