The problem
We have two git repositories “AlphaBeta_iOS” and “AlphaBeta_Android” that we would like to merge them into one new repository “AlphaBeta”. Each existing repository should get its own sub folder named “iOS” and “Android”. The history of both repositories should be preserved.
|--AlphaBeta
|
|--iOS
|
|--Android
The recipe
-
Clone the git repos “AlphaBeta_iOS” and “AlphaBeta_Android” to “/path/to/projects”
cd /path/to/projects git clone https://<...>/AlphaBeta_iOS git clone https://<...>/AlphaBeta_Android
-
Rewrite the file paths (rename root folder “AlphaBeta” to “iOS” and “Android”)
Case 1: There is a single root folder in the repositories named “AlphaBeta”
We rename them to “iOS” and “Android”:Case 2: There is not a single root folder but multiple files or folders on the root level.cd /path/to/projects/AlphaBeta_iOS git filter-branch --force --tree-filter 'mv AlphaBeta/ iOS/ || true' --tag-name-filter cat -- --all cd /path/to/projects/AlphaBeta_Android git filter-branch --force --tree-filter 'mv AlphaBeta/ Android/ || true' --tag-name-filter cat -- --all
We have to insert a root folder “iOS” and “Android”:NOTE: if you are using macos you have to replace the “\t” in the sed command withcd /path/to/projects/AlphaBeta_iOS git filter-branch --index-filter \ 'git ls-files -s | sed "s-\t\"*-&iOS/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new \ git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD cd /path/to/projects/AlphaBeta_Android git filter-branch --index-filter \ 'git ls-files -s | sed "s-\t\"*-&Android/-" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new \ git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD
ctrl+v, TAB
-
Create a new git repo “AlphaBeta”
git init AlphaBeta
-
Add the two repos as remote repos and fetch their content
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
git merge iOS/master --allow-unrelated-histories git merge Android/master --allow-unrelated-histories
That’s it.