Last active
June 6, 2025 09:02
-
-
Save hgraca/9406f9a17afa5e996246d1a8d3cbd1b6 to your computer and use it in GitHub Desktop.
Extract repo folder into a new repo
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
#!/usr/bin/env bash | |
# ATTENTION | |
# This script is incomplete, we would still need to add a way to provide the paths where the files we want to keep have been before. | |
# Alternatively, we could change the script to remove all files that are not under the path we specify, | |
# the code is already there, we just need to adjust and test. | |
# Usage: ./git-move.sh [email protected]:My/source-repo.git [email protected]:My/destination-repo.git my/path/to/keep | |
SOURCE_REPO=${1} | |
DEST_REPO=${2} | |
DIR_TO_KEEP=${3} | |
SOURCE_REPO_DIR='source_repo' | |
DEST_REPO_DIR='dest_repo' | |
SOURCE_REPO_BRANCH='filtered_code' | |
DEST_REPO_BRANCH='transfered_code' | |
echo | |
echo | |
echo "Cloning source repo..." | |
git clone ${SOURCE_REPO} ${SOURCE_REPO_DIR} | |
echo | |
echo | |
echo "Removing what we don't want to keep..." | |
cd ${SOURCE_REPO_DIR} | |
git remote rm origin | |
git checkout -b ${SOURCE_REPO_BRANCH} | |
echo "Filtering in what we want to keep..." | |
git filter-repo --force \ | |
--path "${DIR_TO_KEEP}" \ | |
--path app/Core/Port/AuditLog \ | |
--path app/Core/Port/CircuitBreaker \ | |
--path app/Core/Port/CommandBus \ | |
--path app/Core/Port/Framework \ | |
--path app/Core/Port/EventBus \ | |
--path app/Core/Port/MessageBus \ | |
--path app/Core/Port/QueryBus \ | |
--path app/Core/Port/RateLimiter \ | |
--path app/Infrastructure\AuditLog\Laravel \ | |
--path app/Infrastructure\CircuitBreaker\Laravel \ | |
--path app/Infrastructure\CommandBus\Laravel \ | |
--path app/Infrastructure\Framework\Laravel \ | |
--path app/Infrastructure\EventBus\Laravel \ | |
--path app/Infrastructure\MessageBus\Laravel \ | |
--path app/Infrastructure\QueryBus\Laravel \ | |
--path app/Infrastructure\RateLimiter\Laravel \ | |
--path tests/Integration/Infrastructure/AuditLog/Laravel \ | |
--path tests/Integration/Infrastructure/CircuitBreaker/Laravel \ | |
--path tests/Integration/Infrastructure/CommandBus/Laravel \ | |
--path tests/Integration/Infrastructure/Framework/Laravel \ | |
--path tests/Integration/Infrastructure/EventBus/Laravel \ | |
--path tests/Integration/Infrastructure/MessageBus/Laravel \ | |
--path tests/Integration/Infrastructure/Monitoring/Prometheus \ | |
--path tests/Integration/Infrastructure/QueryBus/Laravel \ | |
--path tests/Integration/Infrastructure/RateLimiter/Laravel | |
echo "Removing from history leftover files..." | |
#git filter-repo --path file1.txt --path dir/file2.log --invert-paths | |
# Find all files under $PARENT except those under $KEEP | |
find "./" -type f ! -path "./.git/*" ! -path "./${DIR_TO_KEEP}/*" > /tmp/files_to_remove.txt | |
# remove `./` from the beginning of every path | |
sed -i 's|^\./||' /tmp/files_to_remove.txt | |
# Run git filter-repo to remove those files from history | |
git filter-repo $(cat /tmp/files_to_remove.txt | sed 's/^/--path /') --invert-paths --force | |
cd .. # back to root | |
echo | |
echo | |
echo "Finished preparing source!" | |
echo | |
echo | |
echo "Creating destination repo..." | |
git clone ${DEST_REPO} ${DEST_REPO_DIR} | |
cd ${DEST_REPO_DIR} | |
git checkout -b ${DEST_REPO_BRANCH} | |
echo | |
echo | |
echo "Copying what want to keep into the new repository..." | |
git remote add local_source_repo ../${SOURCE_REPO_DIR} | |
git fetch local_source_repo | |
git merge local_source_repo/${SOURCE_REPO_BRANCH} --allow-unrelated-histories | |
echo | |
echo | |
echo "Finished!" | |
echo | |
echo "Now you just need to push to origin." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment