It turns out that the answer is much simpler if you're simply trying to glue two repositories together and make it look like it was that way all along rather than manage an external dependency. You simply need to add remotes to your old repos, merge them to your new master, move the files and folders to a subdirectory, commit the move, and repeat for all additional repos. Submodules, subtree merges, and fancy rebases are intended to solve a slightly different problem and aren't suitable for what I was trying to do.

Here's an example Powershell script to glue two repositories together:

# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init

# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
git commit --allow-empty -m "Initial dummy commit"

# Add a remote for and fetch the old repo
# (the '--fetch' (or '-f') option will make git immediately fetch commits to the local repo after adding the remote)
git remote add --fetch old_a <OldA repo URL>

# Merge the files from old_a/master into new/master
git merge old_a/master --allow-unrelated-histories

# Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later
mkdir old_a
dir -exclude old_a | foreach { git mv $_.Name old_a }

# Commit the move
git commit -m "Move old_a files into subdir"

# Do the same thing for old_b
git remote add -f old_b <OldB repo URL>
git merge old_b/master --allow-unrelated-histories
mkdir old_b
dir –exclude old_a,old_b | foreach { git mv $_.Name old_b }
git commit -m "Move old_b files into subdir"

Obviously you could instead merge old_b into old_a (which becomes the new combined repo) if you’d rather do that – modify the script to suit.

If you want to bring over in-progress feature branches as well, use this:

# Bring over a feature branch from one of the old repos
git checkout -b feature-in-progress
git merge -s recursive -Xsubtree=old_a old_a/feature-in-progress

That's the only non-obvious part of the process - that's not a subtree merge, but rather an argument to the normal recursive merge that tells Git that we renamed the target and that helps Git line everything up correctly.

I wrote up a slightly more detailed explanation here.

Answer from Eric Lee on Stack Overflow
Top answer
1 of 10
422

It turns out that the answer is much simpler if you're simply trying to glue two repositories together and make it look like it was that way all along rather than manage an external dependency. You simply need to add remotes to your old repos, merge them to your new master, move the files and folders to a subdirectory, commit the move, and repeat for all additional repos. Submodules, subtree merges, and fancy rebases are intended to solve a slightly different problem and aren't suitable for what I was trying to do.

Here's an example Powershell script to glue two repositories together:

# Assume the current directory is where we want the new repository to be created
# Create the new repository
git init

# Before we do a merge, we have to have an initial commit, so we'll make a dummy commit
git commit --allow-empty -m "Initial dummy commit"

# Add a remote for and fetch the old repo
# (the '--fetch' (or '-f') option will make git immediately fetch commits to the local repo after adding the remote)
git remote add --fetch old_a <OldA repo URL>

# Merge the files from old_a/master into new/master
git merge old_a/master --allow-unrelated-histories

# Move the old_a repo files and folders into a subdirectory so they don't collide with the other repo coming later
mkdir old_a
dir -exclude old_a | foreach { git mv $_.Name old_a }

# Commit the move
git commit -m "Move old_a files into subdir"

# Do the same thing for old_b
git remote add -f old_b <OldB repo URL>
git merge old_b/master --allow-unrelated-histories
mkdir old_b
dir –exclude old_a,old_b | foreach { git mv $_.Name old_b }
git commit -m "Move old_b files into subdir"

Obviously you could instead merge old_b into old_a (which becomes the new combined repo) if you’d rather do that – modify the script to suit.

If you want to bring over in-progress feature branches as well, use this:

# Bring over a feature branch from one of the old repos
git checkout -b feature-in-progress
git merge -s recursive -Xsubtree=old_a old_a/feature-in-progress

That's the only non-obvious part of the process - that's not a subtree merge, but rather an argument to the normal recursive merge that tells Git that we renamed the target and that helps Git line everything up correctly.

I wrote up a slightly more detailed explanation here.

2 of 10
221

Here's a way that doesn't rewrite any history, so all commit IDs will remain valid. The end-result is that the second repo's files will end up in a subdirectory.

  1. Add the second repo as a remote:

    cd firstgitrepo/
    git remote add secondrepo username@servername:andsoon
    
  2. Make sure that you've downloaded all of the secondrepo's commits:

    git fetch secondrepo
    
  3. Create a local branch from the second repo's branch:

    git branch branchfromsecondrepo secondrepo/master
    
  4. Move all its files into a subdirectory:

    git checkout branchfromsecondrepo
    mkdir subdir/
    git ls-tree -z --name-only HEAD | xargs -0 -I {} git mv {} subdir/
    git commit -m "Moved files to subdir/"
    
  5. Merge the second branch into the first repo's master branch:

    git checkout master
    git merge --allow-unrelated-histories branchfromsecondrepo
    

Your repository will have more than one root commit, but that shouldn't pose a problem.

🌐
Gfscott
gfscott.com › blog › merge-git-repos-and-keep-commit-history
How to merge multiple Git repos and keep their commit history / Graham F. Scott
By default, a successful merge command creates a commit. I like using the --no-commit flag so I can confirm the new file structure before committing the changes, but it's optional. # ./everything # youtube/monorepo-prep = <remote repo name>/<branch name> # --allow-unrelated-histories = It's OK to smoosh these repos together # --no-commit = (Optional) Stage instead of committing $ git merge youtube/monorepo-prep --allow-unrelated-histories --no-commit
Discussions

How do you merge two Git repositories? - Stack Overflow
See osdir.com/ml/git/2009-07/msg01576.html 2012-09-16T14:35:17.497Z+00:00 ... This answer may be confusing because it has B as the merged subtree when in the question it was A. Result of a copy and paste? 2012-09-20T11:32:58.057Z+00:00 ... If you're trying to simply glue two repositories together, submodules and subtree merges are the wrong tool to use because they don't preserve all of the file history ... More on stackoverflow.com
🌐 stackoverflow.com
Synchronizing Two Git Repositories with Different Commit Histories
XY problem. I’m sure you or somebody can figure out a script to do this. But why? This is a wacky workflow and this feels like one of those cases where there’s probably a better solution to the real problem, if we knew what the real problem was. More on reddit.com
🌐 r/git
22
0
May 22, 2025
Is there a way to merge two repositories and maintain each's file history?

https://stackoverflow.com/questions/2949738/git-merge-different-repositories

More on reddit.com
🌐 r/git
2
10
November 11, 2016
Merging multiple repositories into one
The approach I've taken to do this is to first ensure that the repositories have distinct filenames. For instance, you could move all of the files in each repository into a subdirectory named after that repository. Make sure that's committed in each of them. (An alternative approach is to actually rewrite all the commits in each repository so they are done as if they were in that subdirectory.) Then you can add all of these repositories as remotes to an existing repository. You can use one Git repository as the remote for another Git repository — you don't actually need a "remote server" of any kind. Then you can fetch and merge from each of those remotes. You could even do one big octopus merge to bring them all in with a single commit. And finally you can remove those remotes. The end result is a single repository with each of the original repositories' files, and each of the original repositories' history. Each history has its own "initial commit", but Git's perfectly fine with that. I last did this many years ago, so maybe there's a simpler or better way nowadays. More on reddit.com
🌐 r/git
2
1
November 16, 2023
🌐
Medium
medium.com › @checko › merging-two-git-repositories-into-one-preserving-the-git-history-4e20d3fafa4e
Merging two Git repositories into one preserving the git history | by Nyah Check | Medium
September 26, 2018 - $ git remote add -f second_repo `link_to_second_repo` $ git merge --allow-unrelated-histories second_repo/master · Fix any merge conflicts and complete the merge as follows ... Now we have successfully merged two git repositories into one. We preserved the commit histories and file histories of all files; and now can continue refactoring or doing other things related to our project.
🌐
Medium
alex-v.medium.com › how-to-merge-git-repositories-and-keep-history-587f973f06a0
How to merge Git repositories and keep history | by AlexV | Medium
April 16, 2025 - #!/bin/bash # Example: ./merge_repos.sh # Variables REPO1_BRANCH_NAME="<branch name>" REPO2_BRANCH_NAME="<branch name>" MERGE_BRANCH_NAME="<branch name to create a PR>" REPO1_NAME="<repo 1 name>" REPO2_NAME="<repo 2 name>" REPO1_PATH="<repo 1 full path>" REPO2_PATH="<repo 2 full path>" # Navigate to the first repository cd "$REPO1_PATH" || { echo "Directory $REPO1_PATH not found"; exit 1; } # Checkout the specified branch git checkout "$REPO1_BRANCH_NAME" # Filter the repository into a subdirectory git filter-repo --to-subdirectory-filter "$REPO1_NAME" # Navigate to the second repository cd "$
🌐
DEV Community
dev.to › itminds › how-to-split-and-merge-multiple-git-repositories-and-keep-the-history-2938
How to split and merge multiple git repositories while keeping the history - DEV Community
September 3, 2020 - Run the following command with powershell in the repository root folder: git filter-repo --path [PROJECT1]/ --path [PROJECT2]/ --prune-empty always --tag-rename '':'[OLDREPONAME]-'.
🌐
Mozilla Hacks
hacks.mozilla.org › home › articles › merging two github repositories without losing commit history
Merging two GitHub repositories without losing commit history – Mozilla Hacks - the Web developer blog
August 29, 2022 - Hopefully, you now know how to exclude subdirectories using the mv command, set and view shell configuration, and merge the file contents of a git repo into a new repository while preserving the entire commit history using only basic git commands.
Find elsewhere
🌐
GitHub
github.com › robinst › git-merge-repos
GitHub - robinst/git-merge-repos: Program for merging multiple Git repositories into one, preserving previous history, tags and branches · GitHub
Program for merging multiple Git repositories into one, preserving previous history, tags and branches - robinst/git-merge-repos
Author   robinst
🌐
Tech TLDR;
techtldr.com › git-merg-two-or-more-repositories-and-keeping-history
Git - Merge Two or More Repositories Together and Keep History - Tech TLDR;
February 16, 2021 - git remote add some-old-repo https://github.com/user/some-old-repo.git git pull --allow-unrelated-histories some-old-repo master git status -s Code language: JavaScript (javascript) At this point, changes from that repo should be you your master branch. Regular git stuff. If you are not sure how to do it, you should probably hold off on merging repos together until you get more comfortable with git.
🌐
Jeff Kreeftmeijer
jeffkreeftmeijer.com › git-combine
Combine Git repositories with unrelated histories
August 9, 2021 - To combine the two repositories, first add the second repository as a remote to the first. Then, run git fetch to fetch its branch information: ... Then, with the remote set up, merge the second repository’s history into the first by using the --allow-unrelated-histories flag:
🌐
Funky Si's Blog
funkysi1701.com › home › posts › merging two projects into one git repository
Merging Two Projects Into One Git Repository - Funky Si's Blog
March 10, 2025 - However, if you look closely at the Git history of those files, you will see that the history has been preserved from the original repository. This is a remarkably simple way to preserve the history of commits from one repository to another. It is a great way to merge two projects into one repository and keep the history of the commits. A similar process could be followed if you want to create a new repository and merge other repositories ...
🌐
Medhat
medhat.dev › blog › merging-2-git-repos-with-persisting-commit-history
Merging 2 git repos with persisting commit history
August 3, 2020 - You will find all content of the ... the other repo, then you run the some command for the other repo: git merge ProjectB/master --allow-unrelated-histories...
Top answer
1 of 16
2999

If you want to merge branch some-branch from project-a into project-b:

cd path/to/project-a
git checkout some-branch

cd path/to/project-b
git remote add project-a /path/to/project-a
git fetch project-a --tags
git merge --allow-unrelated-histories project-a/some-branch
git remote remove project-a

Taken from: git merge different repositories?

This method worked pretty well for me, it's shorter and in my opinion a lot cleaner.

In case you want to put project-a into a subdirectory, you can use git-filter-repo (filter-branch is discouraged). Run the following commands before the commands above:

cd path/to/project-a
git filter-repo --to-subdirectory-filter project-a

An example of merging 2 big repositories, putting one of them into a subdirectory: https://gist.github.com/x-yuri/9890ab1079cf4357d6f269d073fd9731

Note:
The --allow-unrelated-histories parameter only exists since git >= 2.9. See Git - git merge Documentation / --allow-unrelated-histories

Update:
Added --tags as suggested by @jstadler in order to keep tags.

2 of 16
666

Here are two possible solutions:

Submodules

Either copy repository A into a separate directory in larger project B, or (perhaps better) clone repository A into a subdirectory in project B. Then use git submodule to make this repository a submodule of a repository B.

This is a good solution for loosely-coupled repositories, where development in repository A continues, and the major portion of development is a separate stand-alone development in A. See also SubmoduleSupport and GitSubmoduleTutorial pages on Git Wiki.

Subtree merge

You can merge repository A into a subdirectory of a project B using the subtree merge strategy. This is described in Subtree Merging and You by Markus Prinz.

git remote add -f Bproject /path/to/B
git merge -s ours --allow-unrelated-histories --no-commit Bproject/master
git read-tree --prefix=dir-B/ -u Bproject/master
git commit -m "Merge B project as our subdirectory"
git pull -s subtree Bproject master

(Option --allow-unrelated-histories is needed for Git >= 2.9.0.)

Or you can use git subtree tool (repository on GitHub) by apenwarr (Avery Pennarun), announced for example in his blog post A new alternative to Git submodules: git subtree.


I think in your case (A is to be part of larger project B) the correct solution would be to use subtree merge.

🌐
JDriven Blog
blog.jdriven.com › 2021 › 04 › How-to-merge-multiple-git-repositories
How to merge git repositories preserving git history - JDriven Blog
April 21, 2021 - cd ../MonoRepo (1) git remote add -f RepoA ../RepoA (2) git merge -m "Integrating RepoA" RepoA/prepare_monorepo --allow-unrelated-histories (3) git remote rm RepoA (4) git push (5)
🌐
Vvse
vvse.com › blog › blog › 2017 › 04 › 16 › merge-git-repositories-into-a-new-repository-while-preserving-the-commit-history
Merge git repositories into a new repository while preserving the commit history - VVSE-Blog
April 16, 2017 - cd AlphaBeta git remote add --fetch iOS ../AlphaBeta_iOS/ git remote add --fetch Android ../AlphaBeta_Android/ Merge the commit histories of the two remote repos into the new “AlphaBeta” repo
🌐
Build5Nines
build5nines.com › git-merge-repositories-with-history
Git: Merge Repositories with History – Build5Nines
May 14, 2026 - When merging two Git repositories ... the git-filter-repo tool exists that allows you to easily move the contents of a Git repository to a new subfolder within the same repository while preserving history....
🌐
codestudy
codestudy.net › blog › merge-two-git-repositories-without-breaking-file-history
How to Merge Two Git Repositories Without Breaking File History: A Complete Guide — codestudy.net
To keep things organized, move the source repo’s files into a dedicated subdirectory (e.g., source-repo-files/). Use git mv to preserve history (regular mv will break file tracking):
🌐
Meziantou's blog
meziantou.net › merging-2-git-repositories-into-one.htm
How to Merge Two Git Repositories While Preserving History - Meziantou's blog
June 9, 2025 - Learn how to merge two Git repositories into one while preserving their commit histories, ensuring a unified and traceable codebase.
🌐
GitHub
gist.github.com › msrose › 2feacb303035d11d2d05
How to combine two git repositories. · GitHub
Note that this won't preserve a ... for your case. So to summarize: you want to use git rebase instead of git merge --allow-unrelated-histories...