To transfer a directory from one Git repository to another while preserving its history, follow these steps:
- Clone the SOURCE REPOSITORY to your local machine.
- Remove the origin from the local copy of the SOURCE REPOSITORY.
git remote remove origin
- Use Git filter-repo to filter the repository's history to include only the files you need, specifying the DIRECTORY_PATH.
git filter-repo --path DIRECTORY_PATH --force
- Move the filtered results to the desired folder in the local copy of the SOURCE REPOSITORY.
- Add the new remote origin to the target repository using
git remote add origin [email protected]:userame/repo-name.git.
- Pull the changes from the remote repository using to sync the local and remote target repositories.
git pull --allow-unrelated-histories
- Create a new branch using
git checkout -b BRANCH_NAME.
- Push the changes to the target repository using git push origin HEAD.