Press enter or click to view image in full size
You never know if you need to combine two projects into one. If you ever want to, it should be relatively simple and straightforward to do it.
Use Cases:
- If there are two similar libraries and both of the maintainers come together to combine it.
- And sometimes, you don’t have a choice but to have a single repository for the project.
I have used Repo A and Repo B and Repo C for demonstrating the purpose.
- Repo A
- Repo B
- Repo C: This is the Repo C, where I want to merge Repo A and Repo B.
Steps to merge git repositories
I assume you are in the directory, where you want to combine the repositories, for me, it is Repo C.
git remote add -f repo-a git@github.com:vaibhavmule/repo-a.gitThis command will add a remote URL of repo-a.
git merge repo-a/masterNow the merging starts, it is as simple as merging any branch. This command will merge all the files and folders from repo-a/master.
fatal: refusing to merge unrelated historiesOops, in the real-world scenarios, it is not as simple as you think, you have to add —allow-unrelated-histories to get it to work.
git merge repo-a/master --allow-unrelated-historiesBy this time, you have successfully merged the master branch of repo-a to repo-c, for the reward, you are ready to grab a coffee.
Wait, you might have got merge conflicts, resolve that, and get your coffee.
Repeat the same steps for Repo B,
git remote add -f repo-b git@github.com:vaibhavmule/repo-b.git
git merge repo-b/master --allow-unrelated-historiesBy this time, I was smarter, and I knew that I’m going to get an error: fatal: refusing to merge unrelated histories, so added allow-unrelated-histories beforehand.
I’m aware that I’m going to get some merge conflicts, so, I resolved it.
You should git log in your terminal, you will be able to see commit history as expected.
Press enter or click to view image in full size
Exercise for learning to merge two git repositories into one:
Press enter or click to view image in full size
I’m from Repo A and B in the README.md file looks good. I thought of adding repo named mars, and README file would have contained I am from Mars.
But this is the exercise which you can do.
- Fork repo-c.
- Create a new repo named
marsor it can be your city or country name. - Merge
marsrepo to forked repo-c. - Create the pull request to repo c.
You see, the merging of one or multiple repositories into single repositories is simple. In the process of merging repositories, the only thing that’s going to take time is merging conflicts.
Follow me on twitter if you liked these kind of articles.