How to Merge Repositories in Git

Abdul Jabbar Mar 11, 2025 Git Git Merge
  1. Method 1: Merging a Repository into Another
  2. Method 2: Using a Subdirectory for Merging
  3. Method 3: Cloning and Merging
  4. Conclusion
  5. FAQ
How to Merge Repositories in Git

Merging repositories in Git can seem daunting, especially if you’re new to version control. However, with the right guidance, you can seamlessly combine separate repositories into a master repository using the command line.

This tutorial will walk you through the process, ensuring you have a clear understanding of each step. Whether you’re consolidating projects or simply reorganizing your codebase, mastering this skill can significantly enhance your workflow. Let’s dive into the methods you can use to merge repositories effectively.

Method 1: Merging a Repository into Another

One of the most straightforward ways to merge repositories is by using Git commands directly. This method allows you to integrate the entire history of one repository into another, preserving all commits and branches. Here’s how you can do it:

  1. First, navigate to your master repository using the command line.

    cd path/to/your/master-repo
    
  2. Next, add the repository you want to merge as a remote.

    git remote add repo-to-merge path/to/your/other-repo
    
  3. Fetch the changes from the newly added remote repository.

    git fetch repo-to-merge
    
  4. Now, you can merge the fetched repository into your current branch.

    git merge repo-to-merge/main --allow-unrelated-histories
    
  5. Finally, resolve any conflicts that arise, stage your changes, and commit them.

    git add .
    git commit -m "Merged repo-to-merge into master"
    

Output:

Merge made by the 'recursive' strategy.

In this method, we first navigate to the master repository. By adding the other repository as a remote, we can fetch its content without altering the master repository immediately. The --allow-unrelated-histories option is crucial when merging repositories that do not share a common history. After merging, it’s essential to resolve conflicts, if any, and commit the changes to finalize the merge.

Method 2: Using a Subdirectory for Merging

If you want to keep the merged repository organized, you can merge it into a subdirectory of the master repository. This approach is particularly useful when you want to maintain the structure of the original repositories. Here’s how to do it:

  1. Start by navigating to your master repository.

    cd path/to/your/master-repo
    
  2. Create a new directory for the repository you want to merge.

    mkdir new-subdir
    
  3. Add the other repository as a remote.

    git remote add repo-to-merge path/to/your/other-repo
    
  4. Fetch the contents of the remote repository.

    git fetch repo-to-merge
    
  5. Merge the repository into the newly created subdirectory.

    git merge repo-to-merge/main --allow-unrelated-histories --strategy=ours
    
  6. Move the contents into the new subdirectory.

git mv * new-subdir/
  1. Commit your changes.

    git commit -m "Merged repo-to-merge into new-subdir"
    

Output:

Renamed 'file1' to 'new-subdir/file1'
Renamed 'file2' to 'new-subdir/file2'

This method allows you to keep your master repository tidy while still integrating another repository’s content. By using the --strategy=ours option during the merge, you can avoid conflicts and ensure that the master repository retains its state. After moving the files into the new subdirectory, a simple commit finalizes the process.

Method 3: Cloning and Merging

If you prefer a more hands-on approach, you can clone the repository you want to merge and then manually integrate it into your master repository. This method gives you complete control over what gets merged. Here’s how to do it:

  1. Clone the repository you wish to merge.

    git clone path/to/your/other-repo temp-repo
    
  2. Navigate into the cloned repository.

    cd temp-repo
    
  3. Prepare the files for merging.

    git checkout main
    
  1. Copy the files from the cloned repository to your master repository.

    cp -r * path/to/your/master-repo/
    
  2. Navigate back to your master repository.

    cd path/to/your/master-repo
    
  3. Stage and commit the changes.

    git add .
    git commit -m "Merged files from temp-repo"
    

Output:

Files successfully copied and committed.

By cloning the repository, you can review and selectively choose which files to integrate into your master repository. This method is particularly useful when you want to avoid merging unnecessary files or branches. After copying the files, a simple commit finalizes the merge.

Conclusion

Merging repositories in Git is a valuable skill that can streamline your development process. Whether you choose to merge directly, use a subdirectory, or clone and integrate, each method has its advantages. By following the steps outlined in this tutorial, you can effectively manage your repositories and maintain a clean and organized codebase. Remember to always back up your repositories before performing merges to avoid any potential data loss. Happy coding!

FAQ

  1. What is the purpose of merging repositories in Git?
    Merging repositories allows you to combine different codebases, preserving their history and structure, which can be useful for collaboration or project consolidation.

  2. Can I merge repositories without losing commit history?
    Yes, using the right Git commands, such as --allow-unrelated-histories, allows you to merge repositories while retaining their commit history.

  1. What should I do if I encounter merge conflicts?
    If you encounter merge conflicts, Git will mark the files with conflicts. You’ll need to manually resolve these conflicts, stage the changes, and commit the resolved files.

  2. Is it possible to merge repositories with different branch structures?
    Yes, you can merge repositories with different branch structures, but be prepared to handle potential conflicts and discrepancies in the code.

  3. Can I revert a merge if something goes wrong?
    Yes, you can revert a merge using the git revert -m 1 <commit> command, which allows you to undo the merge while preserving the history.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Merge