Skip to content

Instantly share code, notes, and snippets.

@varqox
Created February 25, 2024 16:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save varqox/55ca0fcaeafa2f31c0351f97941b1597 to your computer and use it in GitHub Desktop.
Save varqox/55ca0fcaeafa2f31c0351f97941b1597 to your computer and use it in GitHub Desktop.
Merge 3 git repositories into one having the same (empty) initial commit
#!/bin/bash
set -exuo pipefail
rm -rf sim-project
git init sim-project
cd sim-project
git commit -m 'Initial commit' --allow-empty
INITIAL_COMMIT=$(git rev-parse @)
# sim
git clone https://github.com/varqox/sim sim-git
(cd sim-git && git filter-repo --to-subdirectory-filter sim)
git remote add sim sim-git/
git fetch sim main develop --no-tags
INITIAL_SIM_COMMIT=$(git rev-list --first-parent sim/main | tail -1)
git replace $INITIAL_SIM_COMMIT --graft $INITIAL_COMMIT
# simlib
git clone https://github.com/varqox/simlib simlib-git
(cd simlib-git && git filter-repo --to-subdirectory-filter simlib)
git remote add simlib simlib-git/
git fetch simlib main --no-tags
INITIAL_SIMLIB_COMMIT=$(git rev-list --first-parent simlib/main | tail -1)
git replace $INITIAL_SIMLIB_COMMIT --graft $INITIAL_COMMIT
# sip
git clone https://github.com/varqox/sip sip-git
(cd sip-git && git filter-repo --to-subdirectory-filter sip)
git remote add sip sip-git/
git fetch sip main --no-tags
INITIAL_SIP_COMMIT=$(git rev-list --first-parent sip/main | tail -1)
git replace $INITIAL_SIP_COMMIT --graft $INITIAL_COMMIT
# Rewrite history to "rebase" all branches onto the $INITIAL_COMMIT
git filter-repo --path-rename ":" --force
git merge sim/main
git merge simlib/main
git merge sip/main
git checkout sim/develop
git rebase main
git branch develop
git checkout main
git remote remove sim
git remote remove simlib
git remote remove sip
rm -rf sim-git/
rm -rf simlib-git/
rm -rf sip-git/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment