Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save oneandoneis2/e9ab35ebef0ad036ae1f8469f0358637 to your computer and use it in GitHub Desktop.
Save oneandoneis2/e9ab35ebef0ad036ae1f8469f0358637 to your computer and use it in GitHub Desktop.
Import git history
To merge repo history in, define the following shell function:
function git-add-repo
{
repo="$1"
dir="$(echo "$2" | sed 's/\/$//')"
pwd="$(pwd)"
tmp="$(mktemp -d)"
remote="$(echo "$tmp" | sed 's/\///g'| sed 's/\./_/g')"
git clone "$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 "$pwd"
git remote add -f "$remote" "file://$tmp/.git"
git pull "$remote/master"
git merge --allow-unrelated-histories -m "Merge repo $repo into master" --edit "$remote/master"
git remote remove "$remote"
rm -rf "$tmp"
}
Then simply run the command git-add-repo <repo> <dir>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment