DEV Community

Siddharth Singh Bhadauriya
Siddharth Singh Bhadauriya

Posted on • Updated on

Combining two independent git repositories

Overview

You might be pondering why anyone would wish to combine two different git repositories into a single repository. Couple of weeks back, I came across a problem. I was working on a React JS project and for the frontend styling, I went for Material-UI.
60% of the project was done and then somehow ran into some styling and responsive issue in my project. Now I was ready to go for Chakra-UI in the same repo itself, but then again it would have messed up my central theme object, ending up only with bunch of cross frameworks error and lots of styling conflicts.
I had a few solutions on my hand, went for creating a new repository but this time the framework was Chakra-UI, so with number of logic and styling changes, I had the desired output.
I was about to rewrite/copy/paste the new code that I had written (in a new git repo) into my first original project repository, when it occurred to me that I could probably merge the two independent repositories on my machine to avoid whole editing fiasco.

Generalization

You have repository A with remote location remoteA, and repository B (which may or may not have remote location remoteB). You want to do one of two things:

  • Keep both repositories' commits, but replace the contents of A with the contents of B, and use remoteA as your remote location.
  • Use remoteA as the remote location to join the two repositories as though they were two branches that you wanted to merge.

Use Case: This may also be useful if you're working on project components in one git while working on template files in another git, and then wish to integrate the two.

Things to consider now:
Make sure your local and remote repositories are up to date with all the modifications you'll require before you begin. To unite the two master branches, the following procedures apply the general idea of altering the remote origin and renaming the local master branch of one of the repos.

Check out `git subtree`/`git submodule`. 
Before going through the steps below. 
This post is just a walkthrough of how 
I reached the solution of the stated problem.  
Enter fullscreen mode Exit fullscreen mode

Go on this Road

Change the remote origin of B to that of A:

$ cd someDirectory/somePath/leading/to/B
$ git remote rm origin
$ git remote add origin <url_to_remoteA>
Enter fullscreen mode Exit fullscreen mode

Rename the local master branch of B:

$ git checkout master
$ git branch -m master-new-stay
Enter fullscreen mode Exit fullscreen mode

Pull all the code of A from remoteA into your local B repo.

$ git fetch
$ git checkout master
$ git pull origin master
Enter fullscreen mode Exit fullscreen mode

The master branch of A is now the master branch of B. master-new-stay is the old master branch of B.

Now delete all the things that you pulled from remoteA.

$ git rm -rf *
$ git commit -m "Delete all the things."
Enter fullscreen mode Exit fullscreen mode

Now, merge master-new-stay into master and alongside add the --allow-unrelated-histories flag too.

git merge master-new-stay --allow-unrelated-histories
Enter fullscreen mode Exit fullscreen mode

git log should show all the commits from A, the delete commit, the merge commit, and finally all the commits from B.

Push everything to remoteA

git push origin master
Enter fullscreen mode Exit fullscreen mode

Voila

Your local copy of B has now become a consolidate repository, containing all of A and B's commits. The remote repository is remoteA. You don't need your local copy of A or the remote repository remoteB any longer.

I hope this has been helpful.

Top comments (0)