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.

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.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 merge repo-b/master --allow-unrelated-histories git remote add -f repo-b git@github.com :vaibhavmule/repo-b.gitgit 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.

Screenshot of commits of Repo C

Exercise for learning to merge two git repositories into one:

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.

or it can be your city or country name. Merge mars repo to forked repo-c.

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.