Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Merges a repo into a subdirectory of another repo (useful when making a submodule part of a parent repo)
#!/bin/bash -e
function merge_repo_to_subdir {
local url=$1
local commit=$2
local module_path=$3
if [[ -z $url || -z $commit || -z $module_path ]]; then
echo "Usage: merge-repo-to-subdir.sh URL BRANCH PATH" >&2
exit 1
fi
local remote_name
local branch_name
remote_name=$(basename "$url")
branch_name=$(basename "$url")
git remote add -f "$remote_name" "$url"
git branch "$branch_name" "$commit"
git filter-branch -f --index-filter \
'git ls-files -s | gsed "s-\t\"*-&'${module_path//-/\\-}'/-" |
GIT_INDEX_FILE=$GIT_INDEX_FILE.new \
git update-index --index-info && \
mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE" \
|| test ! -e "$GIT_INDEX_FILE.new"' "$branch_name"
git merge --allow-unrelated-histories "$branch_name"
git branch -D "$branch_name"
git remote rm "$remote_name"
git for-each-ref --format="%(refname)" refs/original/ | xargs -n 1 git update-ref -d
git reflog expire --verbose --expire=0 --all
git gc --prune=0
}
merge_repo_to_subdir "$1" "$2" "$3"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.