Creating a New Subversion Branch from an Existing Local Git Branch

March 03, 2010 · 1 min read

I frequently have to work with Subversion repositories, and as a Git user I rely on git-svn to bridge the two worlds. My usual workflow is to do development in local Git branches, then check out the integration branch, merge my changes, and git svn dcommit to push the code to Subversion.

Sometimes, though, I need to share an in-progress local branch with a Subversion user before it's ready to merge into the mainline. Every time this comes up I find myself hunting for the correct sequence of commands, so here they are for future reference.

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

The key idea: you create a new branch in Subversion, fetch it into Git, then rebase your local work onto it so that git svn dcommit pushes to the correct place.

Credit goes to Bjoern Steinbrink and Cameron for the comments that pointed me in the right direction.

I've also wrapped this up as a shell script. Download it, make it executable, and pass it the name of the local branch you want to push:

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

This assumes you told git svn clone where to find your Subversion branches when you first set up the repository. If you didn't, your mileage may vary.

These posts are LLM-aided. Backbone, original writing, and structure by Craig. Research and editing by Craig + LLM. Proof-reading by Craig.