Read my latest article: Planet Argon Blog (posted Wed, 17 Feb 2010 15:11:00 GMT)

git-svn is a gateway drug

Posted by Robby Russell Fri, 11 Apr 2008 03:28:00 GMT

11 comments Latest by mitjak Mon, 08 Mar 2010 02:05:11 GMT

As we’re migrating away from Subversion to Git, I’m having to learn a lot about git-svn. Andy has posted a few articles on this topic, but I wanted to share a quick tip that I find myself forgetting.

Working with Subversion branches

While you’re hopefully already familiar with how great local branches are with Git, you might not know that you can connect local branches to remote branches in your Subversion repository. This allows those of us who are using Git locally to work against Subversion branches.

I’m going to assume the following:

  • Your team is using Subversion
  • Your team already has a branch that you’re working in
  • Your team is following Subversion directory conventions (branches/, tags/, and trunk/)
  • You have Git installed (with SVN extensions)

Checkout the Subversion project with Git

Please visit Andy’s tutorial, Git SVN Workflow, for a more detailed explanation of the following commands.

First, we’ll initialize your new local Git repository with git-svn.


  git svn init -s http://svn.yourdomain.com/repos/project_name

Now, you’ll change directories to your new Git repository.


  cd project_name

Let’s fetch all previous revisions into your local repository1.


  git svn fetch

Great, once this is done… you’re master (local) branch is linked to trunk/.

Mapping a local repository to a remote branch

Assuming that your team is working in a Subversion branch on the current iteration of work. Our team has a naming convention for branches for each iteration. For example, if we’re in Iteration 18, we’ll write this as ITER-018 everywhere (Basecamp, Lighthouse, Subversion, etc…). At the start of each iteration, we create a new branch with this naming convention.

For ITER-018, the Subversion branch would be located at:

  • http://svn.yourdomain.com/repos/project_name/branches/ITER-018

If you were to do a git branch -r, you should see ITER-018 show up in the list. Now, the one thing that wasn’t clear when I first read the git-svn documentation was that you can’t just checkout that branch with one command. In fact, this has tripped me up a few times.

First, you’ll need to checkout a new local branch. I’ve opted to come up with my own convention for local branches and in this case, I’ll name it iter_018.


  git co -b iter_018

So, now I’m in the iter_018 branch, which is local. I’m currently still mapped to trunk/, which isn’t what we want. However, all we need to do is reset where Git is currently pointed to. We can run git reset to point this to the ITER-018 branch.


  git reset --hard ITER-018

That’s it! Now, the local iter_018 branch will point to branches/ITER-018 in your Subversion repository. This will allow you to work with your existing repository branch and still reap the benefits of local Git repositories.

What about master?

Good question. The git reset command that you ran will ONLY apply that that individual local branch. So, master is still pointing to trunk/. This will allow you to have several local branches that map to remote branches.

Next Steps…

If you’re working with Git already.. great!

If you’re working in an environment that using Subversion, git svn provides you the ability to start exploring Git without making your entire team switchover. Perhaps your a consultant and working for a client that uses Subversion… no problem!

We’re still using Subversion for past client projects and are considering GitHub, which just launched (to the public) today for future projects. A few of us are already using GitHub for open source projects.

Fun.. I just saw the following tweet pass by as I began to wrap up this post.

rails on github
Uploaded with plasq’s Skitch!

Check out Rails on GitHub!

The Gateway Drug… Git reminds me of Cake

Questions?

I know that I glossed over a few things, so feel free to post questions and/or tips for others who are looking to dabble with Git.

1 You’ll likely have problems if you don’t have a Git authors file specified in your git config.

Subscribe to my RSS feed Enjoying the content? Be sure to subscribe to my RSS feed.
Comments

Leave a response

  1. Avatar
    JeffG Fri, 11 Apr 2008 04:54:00 GMT

    If you do
    git branch -a
    you’ll see ITER-018 as a remote. You can create a local branch based on (that tracks) the remote with
    git svn co -b iter_018 ITER-018
    so you don’t have to do the reset.

  2. Avatar
    JeffG Fri, 11 Apr 2008 04:55:22 GMT

    Make that
    git co -b iter_018 ITER-018

  3. Avatar
    Robby Russell Fri, 11 Apr 2008 16:35:05 GMT Recommend me on Working with Rails

    @JeffG,

    Thanks for the tip!

  4. Avatar
    Sebastian Sat, 12 Apr 2008 08:28:13 GMT

    Nice post!

  5. Avatar
    Eric Tue, 15 Apr 2008 20:27:29 GMT

    Thanks for the tip!

    Isn’t there a way to retrieve trunk, branches & tags directly, and bind trunk with master, tags with tags and branches with branches?

    This would be a perfect git clone!

  6. Avatar
    Graham Sat, 26 Apr 2008 11:08:26 GMT

    Eric – have a look at the -T, -t and -b options for git-svn. I think they basically do what you’re after.

    I’ve got a few apps stuck in subversion, including projects with plugins installed via svn:externals (which don’t map easily into git submodules). I wrote myself a script (git-me-up) to automate cloning them into local git repos. You can find git-me-up on GitHub. Notes on how it works are on my blog: http://effectif.com/2008/4/24/easy-git-svn-for-rails

    Loving the cake video. It’s an oldie, but a goodie.

  7. Avatar
    Jason Smith Sat, 13 Sep 2008 18:38:48 GMT

    @Robby

    Great Post! Exactly what I am looking for. Thanks!

  8. Avatar
    Joe Fiorini Fri, 31 Oct 2008 16:34:33 GMT

    @Robby

    Thanks for the post. I have a question: when I git-svn rebase, I tend to see a_ _lot of merge conflicts! Do you ever see these problems when working with a team that uses svn?

    Thanks!

  9. Avatar
    austin.gilbert@gmail.com Tue, 01 Dec 2009 00:07:11 GMT

    What about using git-svn with a Subversion repository NOT using the standard branching layout? Is there anyway to get a local branch redirected to the remote SVN branch?

  10. Avatar
    Michael Twentyman Fri, 11 Dec 2009 23:44:11 GMT

    @austin.gilbert: Yes.

    git svn clone -t tags -b branches -T trunk https://hosted.repo.com/svn/

    Replace the standard tags/branches/trunk above with your custom paths.

  11. Avatar
    mitjak Mon, 08 Mar 2010 02:05:11 GMT

    nice article. plz fix “you’re” and “your” kthx

Share your thoughts... (really...I want to hear them)

Comments