Yeah, I know it’s backwards.

When I start working on a new wild code idea, I often start out with a Git repository. If the idea goes no where, it’s an easy delete. If it turns out to be great, well, I already have a repo going and ready to share. On top of the ease of use and personal nature of a Git repository, I am also a bit of a Git fanboy so by default I often choose Git.

Anyway, one such Git project turned into something I needed to put into the work repository. As you might have guessed, the work repository is a subversion repository.

There are many posts on how to go from subversion to Git, but I couldn’t find a single one on how to go from Git to subversion - meaning start the project out in Git and then push it in to svn. The key being I wanted to keep all the Git commit history.

I kind of figured out how to do it, but I couldn’t figure out how to get the history to import. Since the history doesn’t import, this is a rather convoluted way of just committing the files to an empty subversion repository. However, in the hopes that someone will comment and say “hey buddy you’re just missing the –import-the-comments-too flag”, here is how I did it:

  • First create a new svn repository with the usual tag, trunk and branches folders.
  • Locally, in the directory above your Git project do:
    $ git svn clone --trunk=trunk \
    --branches=branches --tags=tags my_git_project_folder

This command attaches the svn repository to your Git project (so you can push to it).

  • Move to the svn trunk (now a branch in git)
    $ git checkout trunk

You’ll get a notice like

Note: moving to "trunk" which isn't a local branch
If you want to create a new branch from this checkout, you may do so
(now or later) by using -b with the checkout command again. Example:
  git checkout -b <new_branch_name>
HEAD is now at 6774cdd... [your last commmit comment]
  • Now merge the master (your current code) into the trunk (the branch you are now on)
$ git merge master

You’ll see a bunch of output…

  • Push the newly modified trunk back to the svn server
    $ git svn dcommit

If all went well, you should see something like

r2190 = 39282a1a373300e2f5bba5c92949171fb747ecc5 (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk

And you’re done.

The sad part about this is if you now look at the svn history you’ll only see one item. You lose all of the Git history. So, in a sense, this technique is a bit pointless, and akin to just adding the files right to subversion. However, it’s fun.