Skip to content

Instantly share code, notes, and snippets.

@chaconcarlos
Last active April 30, 2020 22:06
Show Gist options
  • Save chaconcarlos/fcf448ec90c9b0ced308b459701b0fc9 to your computer and use it in GitHub Desktop.
Save chaconcarlos/fcf448ec90c9b0ced308b459701b0fc9 to your computer and use it in GitHub Desktop.
Script to import a subdirectory from a git repository to another git repository.
#!/bin/bash
#Script based on http://gbayer.com/development/moving-files-from-one-git-repository-to-another-preserving-history/
function show_help
{
echo "Usage: git-import-to-repo SOURCE_REPO_PATH IMPORT_PATH DESTINY_REPO_URL"
}
echo "git-import-to-repo script v0.1"
source_repo_path=$1
import_path=$2
destiny_repo_url=$3
if [ ! -d "$source_repo_path" ]; then
echo "'$source_repo_path' is not a valid path."
show_help
exit 1
fi
cd $source_repo_path
if [ ! -d "$import_path" ]; then
echo "'$import_path' is not a valid path."
show_help
exit 1
fi
echo "Preparing to import files from '$import_path'..."
git remote rm origin
git filter-branch --subdirectory-filter $import_path -- --all
cd ..
echo "Preparing destiny $destiny..."
git clone $destiny_repo_url destiny_repo
cd destiny_repo
git remote add repo_source $source_repo_path
git pull repo_source master --allow-unrelated-histories
git remote rm repo_source
git push origin master
echo "Done!"
cd ..
rm -rf destiny_repo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment