Next.js starter your AI actually understands. Ship internal tools in days not weeks. Pre-order $199 $499 → [Get it now]

How to combine repositories in Git

Combining multiple Git repositories into one — often called a monorepo migration — requires merging their histories while preserving every commit, author, and date from each source repository. As the creator of CoreUI with 25 years of open-source development experience, I’ve combined separate framework-specific packages into unified repositories and the process requires care to avoid history loss or path conflicts. The standard approach is to add each source repository as a remote, fetch its history, move its files into a subdirectory, and merge the histories with --allow-unrelated-histories. Each source repository ends up in its own subdirectory so their files don’t collide.

Create a new repository and merge two existing repositories into it.

# Create the combined repository
mkdir combined-repo && cd combined-repo
git init

# Add repo-a as a remote and fetch
git remote add repo-a https://github.com/org/repo-a.git
git fetch repo-a

# Create a subdirectory branch for repo-a
git checkout -b import-repo-a repo-a/main

# Move all files into a subdirectory to avoid conflicts
mkdir packages/repo-a
git mv $(ls | grep -v packages) packages/repo-a/
git commit -m "chore: move repo-a into packages/repo-a"

# Switch back to main and merge
git checkout main
git merge import-repo-a --allow-unrelated-histories -m "chore: import repo-a with history"

--allow-unrelated-histories is required when merging branches with no common ancestor — which is always the case when combining separate repositories.

Merging a Second Repository

Repeat the process for each additional repository.

# Add repo-b as a remote
git remote add repo-b https://github.com/org/repo-b.git
git fetch repo-b

# Move repo-b files into its subdirectory
git checkout -b import-repo-b repo-b/main
mkdir packages/repo-b
git mv $(ls | grep -v packages) packages/repo-b/
git commit -m "chore: move repo-b into packages/repo-b"

# Merge into main
git checkout main
git merge import-repo-b --allow-unrelated-histories -m "chore: import repo-b with history"

After merging both, git log --oneline shows the combined history of all three repositories (the original plus both imports), with original commit SHAs and messages preserved.

Verify the Combined History

Check that both sets of history are present.

# See commits from all sources
git log --oneline --graph | head -30

# Check files are in the right places
ls packages/
# repo-a/
# repo-b/

# Find all commits from the original repo-a authors
git log --author="[email protected]" --oneline | head -10

Remove Import Branches and Clean Up

git branch -d import-repo-a import-repo-b
git remote remove repo-a
git remote remove repo-b

# Push the combined repository
git remote add origin https://github.com/org/combined-repo.git
git push -u origin main

Removing the temporary import branches keeps the branch list clean. The history is preserved in main — the branches are no longer needed.

Best Practice Note

After combining, update each package’s CI/CD pipelines, README links, and documentation to reference the new monorepo paths. Use a monorepo tool like Nx or Turborepo to manage independent builds, tests, and versioning across packages. For the reverse operation — extracting a subdirectory back into its own repository — see how to split repository in Git.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.

Answers by CoreUI Core Team