From Subversion to Git in a morning
Join the DZone community and get the full member experience.Join For Free
from when i arrived in this new company as a consultant, i pushed git as the emergent version control system, knowing its power from my experience in the open source world.
the company i am in had a complex layout for its codebases: a central subversion server with some application repositories, plus an internal repository set up via svn:externals which was shared between the application one. moreover, other vendors subversion repositories were set up via svn:externals, such as the one of zend framework 1 and doctrine 1.2.
here is the story of how in a single morning we migrated our repositories (not a large number of codebases) from subversion to git. i won't include much configuration commands, since there are already very good howtos that i now know by heart and which you can follow to setup your own git server. what i'll tell you in this article are our experience in migrating and the whole story of how we did it from scratch (an additional, empty virtual server).
first: setting up gitosis
the first step was finding a way to host a central git repository, to keep as a point of reference. git does not actually need a central server due to its distributed nature, but our workflow does.
we chose gitosis, the leader git hosting application, for the installation on an ubuntu server machine (a virtual one); gitosis is available in the ubuntu repositories and it creates a gitosis system user.
we gave a name to the machine that every /etc/hosts in the development boxes now contains. the name chosen was homer , the greek bard. the boxes were all linux machines, which simplified much the search for client software: the git command line client.
we created rsa keys for each user/machine via ssh-keygen and uploaded the first public one via ssh to initialize gitosis. once you have a public key in gitosis configuration, you can clone the gitosis-admin repository and put other public keys there and manage the configuration. when you're finished, you simply push your changes: gitosis uses git to version the configuration of its git repositories, which is very cool and in the spirit of an open source product.
as the final part of the setup, we configured (with git config --global ) the user.name and user.email properties for every author, along with the color.ui to auto to display colors in git status output.
second: creating the applications repositories
git-svn is the package to install in ubuntu to make the git svn command available. with this extension, you can talk download the whole history of a subversion repository in your own git local one.
if you want to use the users translation (from subversion's user to git authors), prepare a users.txt file like described in the related howto. note that if you configure the porting tool to use this, you must include in the list all the users that have done even only one commit.
after the configuration and git svn fetch , we had a full git repository on our local machine, with a git log equal to our old svn log : all the revisions had been kept and translated.
the next step was git remote add origin gitosis@homer:repositoryname.git to define a remote to push to. with this configuration, we executed git push origin master the first time, and then git push which will push all the matching branches by default.
third: emulating svn:externals
the problem with porting subversion repositories via git-svn is that svn:externals properties are ignored (at least by default). so for our internal repository shared between projects, we have to substitute the svn:externals property with a git submodule (after porting it too, of course).
submodules are git repositories of their own, much like in subversion. however they do not update silently, since when you add a submodule, you really take out a specific commit. you must do a pull manually in the submodules folder when you want to update them.
the only information saved in the master repository is the commit of the submodule which we are arrived to. when you edit a submodule within your master repo working copy, you must first execute git checkout master in it to move the working copy on the master branch, since by default it is freezed on the particular commit.
to save your work within the submodule you then push the commit, then commit also the folder of the submodule in the main repo, which will change the name of the commit (for example from ab5f.. to 67da..) which the current main commit depends on. it's a bit noisy when done the first time, but it is very clean and keeps the submodule effectively separate from the master repository.
another issues we encountered was that doctrine 1 and zend framework 1 do not have git repositories (doctrine 1 has one, but without any tags actually), apart from unofficial mirrors. so we now mirror them with git-svn on our server, and have a local copy of them as a git repository to keep as a submodule. i'm not a fan of mixing svn:externals in git repositories, so in this solution we have an additional layer of indirection, but a cleaner setup.
doctrine has also sfyaml in its svn:externals definition, so we setup our doctrine 1 git mirror with another submodule containing a small mirror of sfyaml.
by the way, the whole solution of using our own git mirrors to serve submodules is lightning fast since now git clone takes 10 seconds, while svn checkout had always taken some minutes to finish.
if you want to imitate our setup, remember that working with various submodules is tricky and involves many commands in sequence. we added a couple target tto our phing buildfile:
- submodules-setup, which executes git submodule init and git submodule update in repositories with submodules present; git submodule add instead has been executed only one time and its results have been saved in the .gitmodules file. it's amazing how git does not have magic metadata features like subversion properties but relies on itself to save the .git* configuration files.
- submodules-pull, which executes git pull in each of the submodules folders in case we need to update them.
now we have a git repository for each of the development machines, plus a central server where we push our branches for collaboration. we do a lot of atomic commits instead of monolithic ones, which have the defect of being difficult to revert. we made branches without fear, and we stash changes, switch branch and reapply them like a charm. git was really worth the hassle of porting our codebases and i definitely recommend you to think about switching.
Opinions expressed by DZone contributors are their own.