Skip to content

Instantly share code, notes, and snippets.

@Drambluker
Last active May 11, 2023 14:57
Show Gist options
  • Save Drambluker/46b626b05781eb1838eb568693899939 to your computer and use it in GitHub Desktop.
Save Drambluker/46b626b05781eb1838eb568693899939 to your computer and use it in GitHub Desktop.
#!/bin/bash
set -e
source=""
target=""
function usage() {
echo ""
echo "Usage: "
echo "merge-repo.sh -s <source> -t <target>"
echo ""
echo "-s: Source path."
echo "-t: Target path."
echo "-h: Display this help and exit."
echo ""
}
while getopts "s:t:h" opts; do
case $opts in
s)
source=${OPTARG}
;;
t)
target=${OPTARG}
;;
h)
usage
exit 0
;;
\?)
usage
exit 1
;;
esac
done
if [ ! $source ]; then
echo "Please specify the source path."
echo "Use -h for help."
exit 1
fi
if [ ! $target ]; then
echo "Please specify the target path."
echo "Use -h for help."
exit 1
fi
tmp_uuid=`uuidgen`
tmp_dir=tmp-$tmp_uuid
tmp_repo=tmp-repo-$tmp_uuid
tmp_branch=tmp-branch-$tmp_uuid
git clone $source $tmp_dir
cd $tmp_dir
git filter-repo --path-regex .+ --to-subdirectory-filter $target
cd ..
git remote add $tmp_repo $tmp_dir
git fetch $tmp_repo
git branch $tmp_branch $tmp_repo/master
git merge $tmp_branch --allow-unrelated-histories
git branch -d $tmp_branch
git remote rm $tmp_repo
rm -rf $tmp_dir
@Drambluker
Copy link
Author

Drambluker commented May 11, 2023

sudo apt install git-filter-repo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment