Wednesday, October 10, 2018

Merge two GIT repositories to ONE.

Recently I wanted to merge two GIT repositories into one.  The main reason for this was the two projects were different, but closely related, sharing much of the same code.  And I just thought it would be easier if everything was together.

So I did what any normal person does, I googled it.  Turned out this is easier to do that I thought it would be - the results are easy to find on google - but I thought I'd share my use case - not only might it help someone else - But it will help me when I want to do this again - I willn't have to search, I can just look at my blog :-)

This posting on stackoverflow asks the question perfect:

"Consider the following scenario:
I have developed a small experimental project A in its own Git repo. It has now matured, and I'd like A to be part of larger project B, which has its own big repository. I'd now like to add A as a subdirectory of B.
How do I merge A into B, without losing history on any side?"
The Best Answer:
If you want to merge project-a into project-b:
cd path/to/project-b git remote add projectgit remote add project-a path/to/project-a git fetch projectgit fetch project-a git merge
git merge --allow-unrelated-histories project-a/master # or whichever branch you want to merge git remote remove project-a 
This method worked pretty well for me, it's shorter and in my opinion a lot cleaner.
Note: The --allow-unrelated-histories parameter only exists since git >= 2.9. See Git - git merge Documentation / --allow-unrelated-histories
This also worked pretty well in my case, I ran into one conflict - so off to google again to find how to resolve that (again, GIT makes it pretty easy to deal with the conflict)

This needs to be read - but the basics of it the arrows in the file show where the conflict is, and from which branch the conflict came from.  And the equals is the dividing point.
So you might see something like
If you have questions, please
<<<<<<< HEAD
open an issue
ask your question in IRC.
>>>>>>> branch-a

Decide if you want to keep only your branch's changes, keep only the other branch's changes, or make a brand new change, which may incorporate changes from both branches. Delete the conflict markers <<<<<<<=======>>>>>>> and make the changes you want in the final merge. In this example, both changes are incorporated into the final merge:
If you have questions, please open an issue or ask in our IRC channel if it's more urgent.
Add or stage your changes: git add *
and commit the changes with a comment:
git commit -m "resolved merge conflict"

There are other cases covered by the github help competing changes to a file that deal with complete file conflicts

No comments:

Post a Comment