Skip to content

Instantly share code, notes, and snippets.

@dirkgroenen
Created August 14, 2019 07:48
Show Gist options
  • Save dirkgroenen/6ceff64a9c9836cab36d351a1244b14f to your computer and use it in GitHub Desktop.
Save dirkgroenen/6ceff64a9c9836cab36d351a1244b14f to your computer and use it in GitHub Desktop.
Merge multiple GIT repositories while keeping their history. Credits: https://stackoverflow.com/a/43345686
function git-add-repo
{
repo="$1"
branch="$3"
dir="$(echo "$2" | sed 's/\/$//')"
path="$(pwd)"
tmp="$(mktemp -d)"
remote="$(echo "$tmp" | sed 's/\///g'| sed 's/\./_/g')"
git clone --branch="$branch" "$repo" "$tmp"
cd "$tmp"
git filter-branch --index-filter '
git ls-files -s |
sed "s,\t,&'"$dir"'/," |
GIT_INDEX_FILE="$GIT_INDEX_FILE.new" git update-index --index-info &&
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"
' HEAD
cd "$path"
git remote add -f "$remote" "file://$tmp/.git"
git pull "$remote/$branch"
git merge --allow-unrelated-histories -m "Merge repo $repo into current branch" --edit "$remote/$branch"
git remote remove "$remote"
rm -rf "$tmp"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment