Last active
January 28, 2020 14:25
-
-
Save mnemnion/3f039e5831e9bd7022e68ac429a91046 to your computer and use it in GitHub Desktop.
Move files to a different repo with history
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# Usage: | |
# ./git-move.sh path1/ path2/... path/to/destination/repo | |
args=("$@") | |
# All but last argument: | |
paths=("${args[@]::${#args[@]}-1}") | |
# Last argument: | |
dest="${args[${#args[@]}-1]}" | |
echo "creating patch for paths: ${paths[@]}" | |
echo "moving to destination: $dest" | |
# Iterate path arguments and append to tmpfile: | |
for p in "${paths[@]}" | |
do | |
git log --name-only --pretty="format:" --follow -- "$p" >> __LOGNAMES_TMP1290 | |
done | |
cat __LOGNAMES_TMP1290 | sort -u | \ | |
xargs git log --pretty=email --patch-with-stat --reverse --full-index --binary -m --first-parent -- > "$dest/_patch_" | |
trash __LOGNAMES_TMP1290 | |
echo "moving to destination repo at $dest" | |
cd "$dest" | |
echo "applying patch" | |
git am -s --committer-date-is-author-date < _patch_ | |
trash _patch_ | |
echo "OK" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Based on this answer on Stack.
Notes:
git log
will show commits in the order they are made, not date orderlog
is monstrously complex...git mv
, commit, run script, hope for the bestgit init
repo onlytrash
can be replaced withrm
, or better yet, install it. Nondestructive removal is a nice thing to have.