Using git subtree to export a directory (with its history) from one git repository to another:
git subtree add --prefix=local_directory https://github.com/account_name/other_project_to_commits master
git subtree pull --prefix=local_directory https://github.com/account_name/other_project_to_commits develop
git subtree push --prefix=local_directory https://github.com/account_name/other_project_to_commits develop
If you prefer to avoid repeating the repository URL, then you can add it as a remote:
git remote add -f -t master --no-tags remote_name https://github.com/account_name/other_project_to_commits.git
Examples of remote name and local .git
directory:
git subtree add --squash --prefix=local_directory file:///D:/other_project_to_commits/.git master
git subtree add --squash --prefix=local_directory remote_name/master
In future, you can merge/get new commits with:
git subtree pull --squash --prefix=local_directory remote_name/master
And you can push changes back upstream as follows:
git subtree push --prefix=local_directory remote_name/master
git subtree push --prefix=local_directory https://github.com/account_name/other_project_to_commits.git master
References:
- https://stackoverflow.com/questions/811251/how-can-i-move-a-single-directory-from-a-git-repository-to-a-new-repository-whil
- https://stackoverflow.com/questions/46524819/how-extract-2-files-as-a-git-submodule
- https://stackoverflow.com/questions/23937436/add-subdirectory-of-remote-repo-with-git-subtree
- https://stackoverflow.com/questions/20102594/git-subtree-push-squash-does-not-squash
- https://stackoverflow.com/questions/9614255/git-subtree-push-squash-needs-a-long-time-and-it-is-getting-longer-over-time
- https://stackoverflow.com/questions/10081681/git-subtree-push-changes-back-to-subtree-project
- https://stackoverflow.com/questions/26928299/why-does-git-subtree-push-always-list-hundreds-of-commits