5th
2009
Nov
permalink

How to Convert a Local Branch in Git to a Remote SVN Branch

This is a little more obscure git-svn recipe I derived today. Thought I would share it.

Basically, I was prototyping a new feature on a local branch in git. Later, I decided I wanted to push that local branch into an svn branch so that my svn comrades could see it. So I started with a local branch following svn trunk with purely fast-forward commits. And the following did the trick.

Create the svn branch.

svn cp <url-to-svn-repo>/trunk <url-to-svn-repo>/branches/final-remote-name
git fetch        # Sync w/ remote svn repo.
git branch -r    # This should list final-remote-name.

Make a new local branch with your feature that follows the new svn branch.

git checkout local-feature-branch   # This is the local branch where your prototype feature is committed.
git checkout -b final-local-name    # This makes the local branch derived from your local feature branch that will eventually become the branch that tracks the remote svn branch.
git svn info     # This will say you are following trunk.
git rebase remotes/final-remote-name

Verify.

git log         # All of your feature commits should be on top, followed by the commit that created the svn branch, followed by trunk commits.
git svn info    # This should now say you are following the svn branch, final-remote-name.

If everything looks good, push all your feature commits to the remote svn branch.

git svn dcommit

You can optionally delete the local feature branch that is following trunk, so you don’t use it by accident.

git branch -D local-feature-branch

Basically all it amounts to is a rebase, with some svn trickery.

So again, all together now…

svn cp <url-to-svn-repo>/trunk <url-to-svn-repo>/branches/final-remote-name
git fetch
git checkout local-feature-branch
git checkout -b final-local-name
git rebase remotes/final-remote-name
git log         # Check your commits.
git svn info    # Check you are following the svn branch.
git svn dcommit
git branch -D local-feature-branch    # Delete old local branch.

I hope someone finds this useful.

Related: How do I make git-svn use a particular svn branch? - Stack Overflow

blog comments powered by Disqus