Created
August 14, 2019 07:48
-
-
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
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
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