Friday, 12 August 2011

Pushing a git repository into an existing subversion repository

I worked on a project using git as the version control system. Eventally I needed to share it with other developers, who had an existing corporate Subversion repository. They didn't want to be bogged down in the minutae of learning a new version control system.

So I had to push the code in my git repo up to the svn repo.

There are two options:
  1. Take a baseline of the code and just comit that to svn, losing all version history. It's simple; it's quick; it works. Ten points for getting the job done. No points for elegance. And minus several thousand points for losing revision history.
  2. Serialise the development history in git and use that to re-vivify the history within Subversion. Many, many, points for doing the right thing. Minus quite a few for the faff it takes to work out how to do it.
Clearly, (2) is the way to go.

For your delight, this is how I finally managed to do it. Hopefully it'll help you avoid similar head scratching and Googling.

How to push a git repository into svn

Obviously, we'll want to invoke the git-svn command. The trick is how to arrange your git repo so that it's tracking the subversion repository correctly.

Step 1: Create the landing point in the svn repo

svn mkdir svn://DEST/repo/projectname/{trunk,branches,tags}

It's worth noting that in this example, I was happy to just clone the mainline of development, and ignore any git branches.

As you can infer, the corporate svn repo has many top-level directories that are all themselves "mini-repos". This is why I had to push the git history into an existing svn repo, rather than just create a new svn repo.

Step 2: Create a git svn clone tracking svn

git svn clone svn://DEST/repo/projectname/trunk dest

Now we have a git repo that tracks the destination svn landing point for the import operation.

Step 3: Track the git repo we want to import

cd dest
git remote add -f source /path/to/git/source/repo

Now, if you inspect the git history (for this I used the excellent GitX (L) ), you'll see a whole series of commits from the original git repo and, disconnected from this, the master HEAD plus a git-svn HEAD pointing to the original (single) svn commit we cloned.

Step 4: Rebase the original git repo onto git-svn

Here's where the secret magic lies. I seems like there are many ways to go from here. This was the only one I found to work. Of course, I tried many ways that failed. Once I found one that worked, I stopped trying. So if there's a better way I'd love to hear it, but this seems to work well.

git rebase --onto remotes/git-svn --root source/master

At this point, I realised that my git history wasn't strictly linear; I had worked on a few machines, so the history of trunk wove arond a bit.

This meant that what I had expected to be a straightforward operation (that's what you'd expect with a SVN hat on) required a few rebase fix-ups along the way:

gvim foo # fix merge conflict
git add foo
git rebase --continue
# ... rinse and repeat

These were required because branches in the source repo from work on different machines that got merged together to form the "source" trunk line of development didn't flatten into a rebase without a few tweaks.

In general, the conflicts were small and weren't hard to fix.

Step 5: Admire your work

git log

You should now see that the entire master development line of the source git repo has been replayed into the master of your working repo, stacked on top of the git-svn point.

Again, GitX (L) helped to visualise this.

Step 6: Push up to svn

Now that we've arranged everything above git-svn, it's a simple case of:

git svn dcommit

To push the changes up into svn.


Anonymous said...

Thank you soooo much

Anonymous said...

Amazing! Just what I needed. Thanks for sharing with the rest of us.

jay paul said...

Very informative and well written post! I would suggest this blog to my friends also

Samsung - 11.6" Notebook 2 GB Memory and 64 GB Solid State Drive

Samsung - 15.6" Notebook 8 GB Memory - 500 GB Hard Drive

Anonymous said...

Thank you, I can finally stop heavy sighing!!!

alison benhar said...

Nice article.I was searching for some good tips on increasing backlinks and PR. Glad to know this. Thanks for sharing such a great information with us.

oracle hcm said...

Interesting One. Hope can find most as these. Also check the website IGOFUSION One of the best online Oracle training institute.

Oracle Fusion Cloud HCM technical Course Training

preethi Shetty said...

Needed to compose one simple word yet thanks for the suggestions that you are contributed here, please do keep updating us...
DevOps Training in Chennai | DevOps Online Training in Chennai

NareshIT said...

Good post,thanks for sharing the valuable information..keep on sharing the latest updates. Best software Training institute in Bangalore

kiran mai said...

This Blog Is Very Useful

Microservices Online Training

vinod kumar said...

Amazing! Just what I needed. Thanks for sharing with the rest of us.

jyothi kits said...

Needed to compose one simple word yet thanks for the suggestions that you are contributed here, please do keep updating us...
DataGuard Online Training

Devops Online Training

Unknown said...

This is a very very good post. This post gives truly very quality information. Thanks for sharing!!
DevOps Online Training

Ananya Krishnan said...

Good job in presenting the correct content with the clear explanation. The content looks real with valid information. Good Work

DevOps is currently a popular model currently organizations all over the world moving towards to it. Your post gave a clear idea about knowing the DevOps model and its importance.

Good to learn about DevOps at this time.

devops training in chennai | devops training in chennai with placement | devops training in chennai omr | devops training in velachery | devops training in chennai tambaram | devops institutes in chennai | devops certification in chennai | trending technologies list 2018

jagadeesh said...

best article with nice information thank you
DevOps Training in Hyderabad
Salesforce Training in Hyderabad
SAP ABAP Online Training
SEO Training in Hyderabad

Bannu Babu said...

Nice Article ..Thanks for providing information that was worth reading & sharing
ielts coaching in Hyderabad
Machine Learning Course in Hyderabad
Power bi training Hyderabad
Python training in Hyderabad

Digital Floats said...

Great Article. Thanks for sharing info.

Digital Marketing Course in Hyderabad

Digital Marketing Training in Hyderabad

AWS Training in Hyderabad

Workday Training in Hyderabad

Unknown said...

Nice post. It is very useful and informative post.

CEH Training In Hyderbad

Sanoritha said...

Hi, It is Excellent blog for reading this.
Digital Marketing Training Institute in Hyderabad
Digital Marketing Training in Hyderabad
Digital Marketing Training in Ameerpet
Digital Marketing Course in Hyderabad Digital Marketing Course in Ameerpet Best Digital Marketing Course in Ameerpet
best digital marketing training institute in Hyderabad
Best digital marketing training in hyderabad
Best Digital Marketing Training in Ameerpet
Best Digital Marketing Course in Hyderabad
Best Digital Marketing Course in Ameerpet

shivani said...

Excellent blog I visit this blog it's really awesome. The important thing is that in this blog content written clearly and understandable. The content of information is very informative.
Workday HCM Online Training!
Oracle Fusion Financials Online Training
Oracle Fusion HCM Online Training
Oracle Fusion SCM Online Training