Brian Waters
Brian Waters

Reputation: 629

Can't merge existing repo when migrating two SVN repos to Git

I originally had a repo on SVN called repo A. Let's call it's initial commit "commit #1". I made multiple commits to this repo to bring me to commit #3. At this point, I copied the SVN repo to a new location on SVN and continued developing in my copied repo, repo B. Meanwhile, I also continued committing to repo A.

The below describes what the SVN repos looked like:

Repo A: 1-2-3-4a-5a-6a
             \
Repo B:       4b-5b-6b

Now, at this point, I decided to move both repos to Git. My intention was to clone the two repos and then re-join them in to a single repo, maintaining the separate commits as separate branches sprouting from commit #3 above.

So, I performed the following steps:

git svn clone -s -A svn.authors --no-metadata http://subversion.foo.local/repoA_svn/path repoA
git svn clone -s -A svn.authors --no-metadata http://subversion.foo.local/repoB_svn/path repoB
cd repoA
git branch repoA-branch # Branch for first repo
git reset --hard <highest_common_hash> # To have only common history on master
git checkout -b repoB-branch # Branch for second repo
git pull ../repoB master

However, once I try to perform the pull operation, I am getting the following error:

fatal: refusing to merge unrelated histories

Whilst migrating from SVN, because I had moved repoB's location on SVN previously to separate it from repoA, the link to repoA is lost.

So I have the two Git repos migrated from SVN looking like the below:

Repo A: 1-2-3-4a-5a-6a
.
Repo B:       4b-5b-6b

As you can see, the link has been broken, thus the unrelated histories error.

Does anyone know how to add the commits of repoB to repoA, given we are missing the historical link?

I have tried the --allow-unrelated-histories switch during the pull, which allows me to add the commits of repoB, but they are all squashed in to one, and I want to preserve the history of these commits.

Upvotes: 0

Views: 49

Answers (1)

eftshift0
eftshift0

Reputation: 30212

To make it simple and not go back to having to do anything on svn, I would do this:

# create a new third repo
git init /home/myuser/blahblah
cd /home/myuser/blahblah
# add thr other two git repos as remotes
git remote add repo1 whatever-url-for-repo1
git remote add repo2 whatever-url-for-repo2

Now, each one of those two repos has a master, right? And repo2/master started from the third revision of repo1/master, right?

git checkout first-revision-of-repo2/master # checkout this revision by id
git reset --soft third-revision-of-repo1/master # this revision is also by its id
git commit -m "Same comment as repo2/4b"

At this point you have created 4b'. It's the same content of 4b but it has revision 3 as its parent

# now we roll all the other revisions from 4b to the tip of repo2/master
git cherry-pick revision-4b..repo2/master # original ID of revision 4b on repo2/master

At this point you now have the history of repo2/master on a new branch (still unnamed, you are working on detached HEAD) and it is related to repo1/master as it was supposed to exist originally. Have fun with it.

Upvotes: -1

Related Questions