Merging Git Repositories

While it goes against every recommendation out there, merging two separate git repositories together can be very useful when combining what used to be separate ideas into a single whole. It is important to note that this can be dangerous and lead to file loss and/or horribly mangled history, so make sure both repositories are backed up elsewhere safely. This works best when you are the only individual pulling from both repositories, or all collaborators understand they need to pull the repository into a clean directory.

To reiterate, the commands below (git filter-repo) are destructive to the repository's commit history and require a forced push to replace the rewritten history on any existing remotes. Make sure everyone using the repository is aware of what is about to happen.

The following steps will use the example of two projects named alpha and omega.

  1. cd alpha
  2. git remote add omega ../omega (or any other local path to omega)
  3. git fetch omega
  4. git merge omega/main --allow-unrelated-histories
  5. git push --force-with-lease

If you would like to make the task more complex and dangerous by moving files from their original locations in omega to, say, a subdirectory in alpha, you would need to complete these steps first.

  1. cd omega
  2. git filter-repo --analyze
  3. vim .git/filter-repo/analysis/renames.txt
  4. Take the list of every file in the report that you wish to preserve and add it to the next command.
  5. git filter-repo --path --path etc.
  6. Complete the above steps to merge.

updated: 2025-12-24