Git-Svn: For People Who Won't Let Go of Git

I’ve been using Git-Svn for awhile now at work because after using Git every day at my old job, I just couldn’t go back. I’m hooked, and the prospect of using TortoiseSVN made me sad. So I hammered away trying to install, configure, and run Git-Svn on Cygwin. At some point I wrote up my experiences and shared them with my teammates. The result: we have three people successfully using Git-Svn and, very recently, started a project using full, pure Git! Git-Svn is in pretty wide use now and most of this can be dug up online pretty easily. But I’ll post it here in the hopes that it helps someone out at some point.


Why Git?
I’m not answering this. Seriously. Git has been around since 2005. I know that for me, no amount of talking or reading convinced me git was better than any other VCS. Then I tried it. Try it.

Why git-svn?

Better question. The status quo around here is to use Subversion for version control. There are worse things. SVN usually seems like the best thing to use until you use Git. As it turns out, git-svn is a full-fledged SVN client wrapper. That is, whatever Subversion can do, git-svn can do.
The result, when done properly, is a git client on your machine to do all the local branching, merging, and committing that you want. When you’re ready, a simple command will push your changes to the Subversion repository.

Why Listen to Joe? He Looks Weird.

Also a fair question and observation. I am not a git expert. I’ve simply used it for a couple of years and think it’s the best product out there for version control. As of this writing, I’ve been using git-svn for nearly six months and have found it to be a great experience. What’s more, you likely won’t have to bang your head against the wall trying to figure out how to use it because I’ve taken the time to do that for you. This Wiki page is far from comprehensive, but I’ll happily answer any questions you have and even try to fix any issues that you encounter.

Getting Started

To start, you’ll need Git. You’ll preferably want a version >= 1.7.0. Git can be installed via Cygwin and this is the route I’ve chosen. All of my examples are based on Git version 1.7.9 on Cygwin running on Windows 7.
But you can also download a client for your system from the Git website. There’s even a TortoiseGit for those of you who want an easy transition from TortoiseSVN.

Cloning Your Repository

git svn clone http://path/to/your/repo

BEWARE: If your repository is huge or you have a huge number of commits this is going to take a LONG time! One way to get around this is to clone just the latest commit:


git svn init http://path/to/your/repo
git svn fetch -r $REVISION_NUMBER
Both approaches will continue to track your history from this point forward.

Workflow

Your typical git workflow will fit just fine in the git-svn world. However, you’ll want to keep a couple of extra commands handy for managing the transition from your git commits to the central SVN repo. Here is my typical hacking scenario:


git checkout master
git svn rebase
git checkout -b feature-branch

#Hack, hack, hack

git ci . -m "GRT-999: Finishing the hackathon."

git checkout master
git svn rebase

git checkout feature-branch
git rebase master
git svn dcommit
The last statement is functionally equivalent to ‘git push’ and will move your local changes to the repository. git-svn will make a Subversion commit for every one of your commits, which means you might generate a backup of builds if you’re a frequent git committer (like me). However, you can also avoid that and roll all of your commits up into one subversion commit by doing the following:

git ci --amend --all

Extras

  • Here’s what I add to my .git/config file to make working with git even easier:

    [alias]
    st = status
    ci = commit
    co = checkout
    df = diff
    [color]
    branch = auto
    diff = auto
    interactive = auto
    status = auto

  • You’ll find that you’re using the above git-svn commands quite often. I set up a few quick bash aliases to save me all the typing:

    alias grm='git rebase master'
    alias gcm='git co master'
    alias gsd='git svn dcommit'
    alias gsr='git svn rebase'
    alias gs='git stash'
    alias gsp='git stash pop'
    alias st='git status'
  • It’s a good idea to keep a .gitignore file (which is how git keeps track of, well, ignored files) in your repository so that other people using git-svn can benefit. To set it up for the first time, you can use the following command to parse all of your SVN ignores and push them into .gitignore:

    git svn show-ignore >> .gitignore


    BEWARE: This command takes a VERY LONG time, but it only has to be run once. From that point on you can edit the .gitignore file using Vim/your favorite editor.