Skip to content

Instantly share code, notes, and snippets.

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: URL BRANCH PATH" >&2
exit 1
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 update-index --index-info && \
mv "$" "$GIT_INDEX_FILE" \
|| test ! -e "$"' "$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