简体   繁体   中英

Migrating a git repository to a branch of an existing SVN repository

Recently, I have been working on a Java project for my company. For this project, they have chosen to use SVN as a version control system. In the beginning, however, I did not have any access to this SVN repository. Therefore I have been working locally on my own computer on some snapshot of this repository, which was provided to me by the lead developer. Since I have a lot of experience with git (and none with SVN), I have been using git to track the changes I have made to the project.

At the moment, the time has come to integrate my changes into the existing SVN repository. According to the lead developer, I have to push my changes to a personal branch of the SVN repository, so that he can check my changes before merging them into the master branch.

What would be the best strategy to achieve this? Would it be a good idea to generate a set of patches from git that are compatible with SVN? This would allow me to check out the latest version from SVN and to incremently try to apply each of the git commits I have made.

Another idea that came up, was to import my git repository into a local SVN repository and then to try to migrate this local SVN repository into the branch of the remote SVN repository. I have no idea, however, if this could work at all.

Any help would be greatly appreciated!!

If there is a slightest chance of further work with that project, I would strongly recommend using git-svn. This way you retain much of the power of git locally (except for merges - rebases only to/from svn), while still collaborating via svn.

The steps could be like following, but I am writing from memory and have no svn to check against.

First, clone the svn project. -s assumes standard ./trunk ./branches/* ./tags structure:

git svn clone -s <path to SVN> sync_repo
cd sync_repo

Now we need to find the git point to which you will be applying your work. Ideally, you should know the svn revision, snapshot of which the Team Lead provided you with initially, with which you do:

 git svn find-rev r<svn revision>
 git branch my_git_reintegration <SHA1 we've just found, otherwise svn/trunk or some other reasonable revision>

Next we need to pull your changes from your local git:

git remote add my_origin <path to your original git repo>
git fetch my_origin

And rebase your changes on top of your new branch:

git rebase my_git_reintegration my_origin/master 

if you were lucky when choosing the my_reintegration point, you should have no merge conflicts during the rebase. After the rebase we'll move the my_reintegration accordingly:

git push . HEAD:my_reintegration 

At this point you have my_reintegration containing your git changes, but possibly on top of an outdated svn commit. Next, let's create a new svn branch you will be pushing your contributions to. I'll assume you will branch off trunk:

git checkout origin/trunk 
git svn branch my_svn_integration_branch
#those might be not necessary, but just in case 
git svn fetch

And now let's rebase our original changes onto this branch and push it to svn:

git rebase origin/my_svn_integration_branch my_git_reintegration
#possibly resolve conflicts
#just for a safety, check the svn branch we target - should be my_svn_integration_branch
git svn info
#go!
git svn dcommit

at this point you should have your changes at svn's my_svn_integration_branch , you may delete your original git repository and keep using this one instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM