Git

How Do You Merge Two Git Repositories?

Git allows developers to build large and complicated projects in an organized manner easily. While working on Git, different members work on multiple repositories that they are required to combine later without losing the history. So, for this corresponding purpose, they need to merge one repository with another one.

This article demonstrates the procedure of merging two Git repositories.

How to Merge Two Git Repositories?

In order to merge two repositories in Git, first, move to the Git root directory and choose desired repositories. Suppose the root directory contains two repositories named “repo1” and “repo2” that are required to merge each other. To do so:

  • Navigate to repo2 and add the remote URL of repo1 in it.
  • Then, download the content of repo1 into repo2.
  • After that, merge both repositories with the help of the “git merge <repo1-remote>/<branch-name>” command.

Check out the given provided steps for practical demonstration!

Step 1: Switch to Git Repository

First, redirect to the local directory by using below-stated command:

$ cd "C:\Git"

Step 2: View Repository Content

Run the “ls” command to view the list of files and repositories in the root directory:

$ ls

The below output displays all existing files and repositories of the root directory. Choose the desired two repositories that need to merge. For instance, we have selected the “demo_Repo” and “Repo1” repositories:

Step 3: Move to Desired Repository

Then, navigate to the repository where you want to merge the other repository through the provided command. In our case, it is “demo_Repo” local repository:

$ cd "C:\Git\demo_Repo"

Step 4: Add Remote URL

For adding the other selected repository’s path as a remote URL, type out the following command:

$ git remote add origin "C:\Git\Repo1"

Here, the “origin” is our remote URL name and the target repository is “Repo1”:

Step 5: Fetch “Repo1” Repository

Execute the “git fetch” command to download the content of “Repo1” into the “demo_Repo” repository:

$ git fetch origin

Step 6: Merge Repositories

Finally, merge the content of both repositories with the help of below-provided command:

$ git merge origin/master

As a result, the default text editor will open up on screen. Here, add a commit message, save changes and then close the editor:

It can be observed that the merging operation has been performed successfully:

Step 7: Verify Changes

Lastly, to ensure whether the branches have merged or not, view the commit history by running the “git log” command:

$ git log

The below output indicates that the “master” branch of the “Repo1” repository has been merged with the similar branch of the “demo_Repo” repository:

We have efficiently elaborated the procedure of merging two Git repositories.

Conclusion

To merge two Git repositories, go to the Git root directory and select the two repositories that you want to merge. Then, move to one of them and add the path of the second repository to it. Next, fetch the second repository’s content into the first repository. Lastly, run the “git merge <second-repo-remote>/<branch-name>” command to merge both repositories. This article illustrated the method of merging two Git repositories.

About the author

Sharqa Hameed

I am a Linux enthusiast, I love to read Every Linux blog on the internet. I hold masters degree in computer science and am passionate about learning and teaching.