Skip to content

Instantly share code, notes, and snippets.

@JeffSpies
Last active August 29, 2015 14:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JeffSpies/6cbe696b6eaad930930f to your computer and use it in GitHub Desktop.
Save JeffSpies/6cbe696b6eaad930930f to your computer and use it in GitHub Desktop.
Merge two git repositories--move one into another--while retaining history of both
#!/bin/bash
# License: http://www.apache.org/licenses/LICENSE-2.0
# Description: Merge two git repositories--move one into another--while retaining history of both
# WARNING: Before using, backup both repositories.
# Usage: ./git-mv-remember.sh repo_a repo_b relative/path/to/move/a/to/inside/of/b
# Example:
# Repo A:
# A/.git
# A/foo1.txt
# A/foo2.txt
# Repo B:
# B/.git
# B/bar1.txt
# B/bar2.txt
# ./git-mv-remember.sh A B lib/A
# B/.git
# B/bar1.txt
# B/bar2.txt
# B/lib/A/foo1.txt
# B/lib/A/foo2.txt
if [ ! $# == 3 ]; then
echo "Usage: $0 repo_a repo_b relative/path/to/move/a/to/in/b"
exit
fi
# Get absolute paths
FROM=`cd "$1"; pwd`
TO=`cd "$2"; pwd`
# Remove trailing slash
PREFIX=${3%/}
# Get a proper tab for mac's sed
TAB=`echo -e "\t"`
cd $FROM
git filter-branch --index-filter "git ls-files -s | sed \"s,\(.*\)$TAB\(.*\),\1$TAB$PREFIX/\2,\" | GIT_INDEX_FILE=\$GIT_INDEX_FILE.new git update-index --index-info && mv \$GIT_INDEX_FILE.new \$GIT_INDEX_FILE" HEAD
cd $TO
git pull $FROM
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment