Using Git locally within a Subversion branch

Posted on Thu 19 January 2012 in Tools

I’m not a big fan of Subversion but I’m madly in love with Git! So much that when I have to use Subversion, I create a Git repository within my locally checked out Subversion branch. This is really easy to do, and only takes four simple steps:

  1. Navigate to the directory that contains your Subversion branch copy and use the git init command to create a repository.
  2. Create a .gitignore file and add a filter to ignore .svn directories (plus everything else that should be ignored, of course).
  3. Use git add and git commit to create an initial commit with the .gitignore file and all code.
  4. Configure Subversion to ignore the .git directory.

Now you’re ready to work some Git magic! I won’t go into details, but here are a few things that I find very useful:

  • Use the git reset command to undo local changes. (Yeah, I know you can use Subversion’s revert command too.)
  • Use the git stash command to temporarily save changes.
  • Create commits every now and then as “check points”. This is definitively not how you should use Git, but in this context it can save a lot of time. Especially if you work with the Visual Studio WinForms designer, which happily gets into an unrecoverable state every now or then! (Just had to get that off my chest!)
  • Create separate commits for each Subversion update you do. While such a commit is unlikely to be particularly coherent (after all, it can include work from several of your co-workers), you now have a way to pinpoint when bugs were introduced. The git bisect command is immensely useful for this purpose, in particular together with a small test runner script.
  • Use the git checkout command to quickly create branches to experiment or try out ideas.
  • Use branching to facilitate “offline work”. For example, if you worked on a copy of the code somewhere else but accidentally did a Subversion update in the main branch copy, you can:

    1. Stash the changes from the update,
    2. create a new branch,
    3. copy your code into that branch,
    4. create a commit,
    5. switch back to the main branch, and finally
    6. merge from the separate branch using the git merge command.
  • Use the distributed nature of Git to create a separate offline repository, and then use git push or git pull to synchronize between the repositories.

Would you like to add anything to the list?