How to merge two or multiple git repositories into one

Vaibhav Mule
Sep 22, 2018 · 3 min read
Image for post
Image for post
P.S. Not endorsing Chromebooks.

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.

  • 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.git

This command will add a remote URL of repo-a.

git merge repo-a/master

Now 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 histories

Oops, 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-histories

By 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-histories

By 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.

Image for post
Image for post
Screenshot of commits of Repo C

Exercise for learning to merge two git repositories into one:

Image for post
Image for post
The README.md file content merged from repo-a and repo-b.

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 mars or it can be your city or country name.
  • Merge mars repo 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.

AltCampus

Ideas and opinions from the technical folks in hills …

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store