2010-02-15

Migrate a Git repo to a SVN one

We often see the opposite, convert a SVN repository into a Git one and try the breeze of a DVCS, but what if you want to convert a Git repo into a SVN one?

The process is not easy (and you need a "straight" Git repo: no merge, too complex branches and so), and the result is quite raw, but you're still able to convert the main part into SVN. Here are the steps:
  1. create an empty SVN repository: svn mkdir svn://path/to/repo/project/{trunk,branches,tags}
  2. clone it in a Git repo: git svn clone svn://path/to/repo/project --stdlayout . This will create a project directory, empty except for the .git information directory; master is the default Git branch created by git-svn that maps SVN trunk.
  3. cd project
  4. add the original Git repo as a remote: git remote add origin git://uri/to/git/repo/project.git
  5. fetch from it: git fetch origin
  6. create a local branch for the remote master: git checkout -b old_master origin/master . Note that master branch is already present, so we define another one.
  7. being on old_master we rebase this branch onto master: git rebase --onto master --root . Doing this, we have "moved" all our commits from old_master to master
  8. and push all the commits into the SVN repo: git svn dcommit
It's not that elegant, but it gets it done. I'd like to know if someone is aware of a better method, also to import tags and branches, that this process doesn't do.

UPDATE: added 'cd project' step 2

10 comments:

James said...

hey, out of curiosity why would you want to do this ? Doesn't this go against the decentralization of the Hacker Ethic ( http://en.wikipedia.org/wiki/Hacker_ethic )
good luck!

_J

Sandro Tosi said...

@James: simply because some teams in Debian uses SVN as VCS and I wanted to import the package history from Git to SVN.

Chris said...

What did you do about the author and date of each commit?

Suvir Jain said...

Dear Sir, you are a genius. It might not be elegant but I am glad i found it after breaking my head for the past few hours.

Pedro said...

@Chris you can add the --add-author-from option to git svn dcommit to have a From: header added to the svn revisions specifying the original author.

Vlad said...
This comment has been removed by the author.
GreyFox said...

Hi, I cannot reproduce your steps. When i get to the rebase, git prints out a very odd message:


warning: squelched 210 whitespace errors
warning: 215 lines add whitespace errors.
Falling back to patching base and 3-way merge...
error: Your local changes to 'MonitorVendite.BLL/MonitorVendite.BLL.csproj' would be overwritten by merge. Aborting.
Please, commit your changes or stash them before you can merge.
Failed to merge in the changes.


How is it possible to have local changes if I only checked out remote commits?

Sandro Tosi said...

@GreyFox: please ask this king of question on a user support forum (I don't have time to go deep into you problems and help you)

Unknown said...
This comment has been removed by the author.
FreeSwan said...

Guys look at here too...
I'm able to extract git to svn.
BTW initially svn2git and again git2svn.

http://git2svn.blogspot.com/