How to merge two or multiple git repositories into one without losing git log

Initiation

Github has many hidden gems like git submodules, subtree merges. Git is a directed acyclic graph when two graphs together then it makes one big graph. Merging two subtrees have pros and cons as per the use case like

#You can merge two or multiple graphs or subtree together.

# Merging has some side effect, when you import the git file or source code using a subtree merge all of the files show up as newly added files. You can see the history of commits for those files in aggregate (i.e. you can view the commits in the DAG) but if you try to view the history for a specific file in your sub-project all you’ll get is one commit for that file – the subtree merge.

Let’s start the process with an example. 

Prerequisite

#Git  CLI ( Powershell or Terminals )

# Two or three repo for merge 

Steps to merge git repositories:

Type 1:

I  have considered the below Repository for example:

Repository R1
Repository R2
Repository R3: This is repository R3, in which we’ll merge repository R1 and repository R2.

Step 1:  Move in the current project which has an R2 repository

cd /prject-name or repository-name

Step 2:  Create a remote  URL for the R1 repository

git remote add -f repository-r1 git@github.com:adeshsuryan/repository-r1 .git

Note:  you need to add GitHub SSH URL with a user name, avoid  HTTPS URL for same. 

Step 3: Now merge the code  with  simple merge  command 

git merge repository-r1/master --allow-unrelated-histories

Note: 1) Use only merge and avoid the –squash.

2) use   –allow-unrelated-histories  to persist logs.

 

Step 4:  Check the git  commit logs 

git log

 

Leave a Reply

Your email address will not be published.