Reputation: 1010
I required to merge two repositories without loosing the history of either. I searched several forums but none could provide a complete solution. After performing several steps taken from various sources I could manage to join the missing dots and merge two separate repositories into one repository with their histories intact.
There are several other answers which talk about merging branches or just not with complete instructions/steps.
Upvotes: 5
Views: 6453
Reputation: 725
If both repos are on your machine, for example you have folders
A/.git
A/client-src
B/.git
B/server-src
simply go to one of them:
cd A
and type:
git remote add repo-b ../B
This creates a symbolic link from repo-a to repo-b ("repo-b" is just a symbolic name, you can choose anything you want) and then
git fetch repo-b
will combine the two repos. At this point, you will have both histories in one repository, but still in different branches. Branch "master" (the master of the folder you are in) will contain client code. Branch "repo-b/master" will contain the server code.
Then you only have to merge the two:
git merge repo-b/master --allow-unrelated
After this, you will have in Repo A a combined master branch. If you are sure there is no other meaningful information in Repo B (Tags, other branches), you can discard it and continue working with Repo A. Both histories will be intact.
For completeness, you should finally delete the now useless link from Repo-A to Repo-B:
git remote remove repo-b
Upvotes: 9
Reputation: 1010
Below are the steps
git clone <NEW REPO>
cd <NEW REPO FOLDER>
dir > README.MD --> this step is just to initiate the new repository.
git commit -m "initiate the new repository"
git remote add -f <RANDOM NAME A> <OLD BRANCH A>
git remote add -f <RANDOM NAME B> <OLD BRANCH B>
git pull origin master --allow-unrelated-histories
git push -u origin master
git merge --allow-unrelated <RANDOM NAME A>/master
git merge --allow-unrelated <RANDOM NAME B>/master
git push origin master
I believe this post should be of helpful for others.
Upvotes: 2