When I'm coding I frequently have to work with Subversion repositories. I'm a Git user, so I use git-svn for this. Usually I do my work in local branches, using a fairly regular Git workflow, then checkout the integration branch, merge in my changes and git svn dcommit to push the code to Subversion.

Sometimes however I need to share changes that I've made on an existing local branch in my Git repository with a Subversion user before they're ready to integrate back into the mainline. It takes me a while to hunt down and work out exactly what to do whenever I want to do this, so here are the instructions for my future reference. Hopefully they help you too.

git checkout master
git svn branch <new_svn_branch_name>
git svn fetch
git branch -r # make sure <new_svn_branch_name> exists
git checkout -b tmp/svn-rebase-target <new_svn_branch_name>
git rebase --onto tmp/svn-rebase-target master <existing_git_branch_name>
# That should have checked out <existing_git_branch_name>.
git svn dcommit -n # This should say it'll commit to <new_svn_branch_name>.
git branch -D tmp/svn-rebase-target # clean up the temporary branch.
git svn dcommit

This time mainly I used comments on a blog post that asked how to do this. Thanks Björn Steinbrink and Cameron.

I've turned this into a bash shell script. Download it and make it executable. It takes one argument, the name of the local branch you want to push to Subversion:

./svn-push development/avoid-the-wombat-widgets

This assumes that when you cloned your repository from Subversion you told Git where to find our Subversion branches. I'm not sure what'll happen if you didn't do that. Caveat emptor and all that.

written by
Disagree? Found a typo? Got a question?
If you'd like to have a conversation about this post, email craig@barkingiguana.com. I don't bite.
You can verify that I've written this post by following the verification instructions:
curl -LO http://barkingiguana.com/2010/03/03/creating-a-new-subversion-branch-from-an-existing-local-git-branch.html.orig
curl -LO http://barkingiguana.com/2010/03/03/creating-a-new-subversion-branch-from-an-existing-local-git-branch.html.orig.asc
gpg --verify creating-a-new-subversion-branch-from-an-existing-local-git-branch.html.orig{.asc,}