Unrelated git histories when moving code from SVN to Git

23 views Asked by At

I have a problem in handling 2 different git histories. Its described below:

  1. I converted a SVN repo to git repo using the svn2git utility and code is now available as git repo in my local with all the history of commits as well. I only converted a particular branch in SVN to Git along with all its commits.

  2. The target GIT repo to which I am trying to push this branch and all its commits already has code in it with several branches and commits forming another linear history.

The current master branch in git repo is cloned to another branch for safekeeping.

Now, when I push the changes from my local machine to a new branch named SVN2GitMigration in the target git repo, the push is successful. However, due to the existence of 2 different linear histories, I am now unable to raise a Pull Request from the SVN2GitMigration branch (history originating from SVN repo via SVN2Git conversion) to the master branch in git.

Is it safe to run git pull origin <branch_name>-allow-unrelated-histories on each branch in the target git repo before pushing my changes from local to the target git repo? I need to be absolutely sure that the content in existing branches in target git repo are not overridden by running git pull origin <branch_name>-allow-unrelated-histories.

Are there any other approaches to overcome this complex issue? I understand this is due to the fact that I'm dealing with 2 different SCM tools and 2 completely unrelated histories with no common base.

Please suggest safest means to overcome this issue. The end goal is to simply merge 1 branch in my local repo to the master branch in Git.

The expectation is to maintain a branch and its history from the old SVN repo with the new git repo while still preserving the other branches in git without impacting them. Eventually SVN2GitMigration branch must be merged to master in Git by overcoming the problem of unrelated histories without introducing any dangerous conflicts.

1

There are 1 answers

1
TTT On BEST ANSWER

You're close. The tweak is you need to perform the merge of the unrelated histories locally on only your branch; not on the other branches. For example,

git switch SVN2GitMigration
git pull origin master --allow-unrelated-histories
git push

Depending on whether you've done anything to branch SVN2GitMigration since your last push, you might need to force push, but if you're just adding 1 new merge commit, you shouldn't need to.

After these steps you should be able to create the PR from SVN2GitMigration into master without the error. The PR should show all of the new files you are adding to the repo. If some of those files already exist you'll have merge conflicts. If that happens I'd advise you to consider moving/renaming those files unless they truly are the same files.

As long as you didn't have merge conflicts, all other existing branches probably won't need to even know about your merge. After your PR is completed future branches from master will simply already have your new changes included.