Last active
May 3, 2024 18:57
-
-
Save domhel/a66fbd5e60da9ab396517f315e30848e to your computer and use it in GitHub Desktop.
Combine many git repos into one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Use-case: | |
| # - We have multiple git repositories but want to merge them together for simplicity / using a monorepo / etc. | |
| # - We can not lose the git history and tags | |
| # - The new structure can be arbitrary | |
| # | |
| # Solution: | |
| # - Create a tmp directory and clone all previous repos | |
| # - Install git filter-repo via pip | |
| # - In each of these repos, use the git filter-repo command to move all files to any new directory inside the repository | |
| # - cd anywhere and create your new repository | |
| # - merge all repos together | |
| mkdir tmp | |
| git clone a.git | |
| git clone b.git | |
| git clone c.git | |
| python3 -m pip install --user git-filter-repo | |
| # place the following inside .bashrc or .zshrc or whatever | |
| export PATH="$HOME/.local/bin:$PATH" | |
| cd a | |
| # let's say we want repo a to be in folder new-repo/apps | |
| git filter-repo --to-subdirectory-filter apps/a #the last argument can be any path you want. it will be the relative new path from the new repository | |
| cd ../b | |
| git filter-repo --to-subdirectory-filter apps/b | |
| cd ../c | |
| git filter-repo --to-subdirectory-filter packages/c #using packages just so we have sth. different here | |
| cd ../.. | |
| mkdir new-repo | |
| git init | |
| git remote add a ../tmp/a | |
| git fetch a --tags | |
| git merge --allow-unrelated-histories a/main # or a/<branch> whatever branch you want to use now | |
| git remote remove a | |
| git remote add b ../tmp/b | |
| git fetch b --tags | |
| git merge --allow-unrelated-histories b/main # or a/<branch> whatever branch you want to use now | |
| git remote remove b | |
| git remote add c ../tmp/c | |
| git fetch c --tags | |
| git merge --allow-unrelated-histories c/main # or a/<branch> whatever branch you want to use now | |
| git remote remove c | |
| # Done! Now your new repo will look like this | |
| tree -L 2 | |
| #. | |
| #├── apps | |
| #│ ├── a | |
| #│ └── b | |
| #└── packages | |
| # └── c |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment