Skip to content

Instantly share code, notes, and snippets.

@ryu1kn
Created November 1, 2017 10:29
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 ryu1kn/6088d752f012bacbbc506bd7ba49778b to your computer and use it in GitHub Desktop.
Save ryu1kn/6088d752f012bacbbc506bd7ba49778b to your computer and use it in GitHub Desktop.
Script to merge one repository into another
# Merge SRC_REPO into a DEST_REPO under its subdirectory NEW_DIR
# cf. https://stackoverflow.com/questions/277029/combining-multiple-git-repositories
#
# Place 2 repository and this Makefile as follows:
#
# ./
# DEST_REPO/
# Makefile
#
# Then execute
#
# $ make prepare # Get ready SRC_DIR for merge
# $ make merge
DEST_REPO := kata
SRC_REPO := kata--potter
NEW_DIR := potter--scala
prepare: clean get-source-repo move-to-sub-dir rewrite-history print
get-source-repo:
git clone git@github.com:ryu1kn/$(SRC_REPO).git
move-to-sub-dir:
(cd $(SRC_REPO) && \
mkdir $(NEW_DIR) && \
ls -1a | grep -v '^\(\.\+\|\.git\|$(NEW_DIR)\)$$' | xargs -I % git mv % $(NEW_DIR) && \
git commit -m "Prepare for moving repository")
rewrite-history:
(cd $(SRC_REPO) && git filter-branch --index-filter \
'git ls-files -s | sed "s# #&$(NEW_DIR)/#" | GIT_INDEX_FILE=$GIT_INDEX_FILE.new git update-index --index-info && mv "$GIT_INDEX_FILE.new" "$GIT_INDEX_FILE"' HEAD )
clean:
rm -rf $(SRC_REPO)
REVS = $(shell cd $(SRC_REPO) && git rev-list --all)
print:
@for REV in $(REVS) ; do \
(cd $(SRC_REPO) && echo $$REV ; git diff-tree --no-commit-id --name-only -r $$REV ) ; \
done
merge:
cd $(DEST_REPO) && \
git pull ../$(SRC_REPO) --allow-unrelated-histories
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment