Skip to content

Instantly share code, notes, and snippets.

@evandrocoan
Last active January 18, 2022 12:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save evandrocoan/01106a3c8681570c715560e166c49cd8 to your computer and use it in GitHub Desktop.
Save evandrocoan/01106a3c8681570c715560e166c49cd8 to your computer and use it in GitHub Desktop.
Using git subtree to export a directory (with its history) from one git repository to another

Using git subtree to export a directory (with its history) from one git repository to another:

  1. git subtree add --prefix=local_directory https://github.com/account_name/other_project_to_commits master
  2. git subtree pull --prefix=local_directory https://github.com/account_name/other_project_to_commits develop
  3. 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:

  1. 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:

  1. git subtree add --squash --prefix=local_directory file:///D:/other_project_to_commits/.git master
  2. git subtree add --squash --prefix=local_directory remote_name/master

In future, you can merge/get new commits with:

  1. git subtree pull --squash --prefix=local_directory remote_name/master

And you can push changes back upstream as follows:

  1. git subtree push --prefix=local_directory remote_name/master
  2. git subtree push --prefix=local_directory https://github.com/account_name/other_project_to_commits.git master

References:

  1. https://stackoverflow.com/questions/811251/how-can-i-move-a-single-directory-from-a-git-repository-to-a-new-repository-whil
  2. https://stackoverflow.com/questions/46524819/how-extract-2-files-as-a-git-submodule
  3. https://stackoverflow.com/questions/23937436/add-subdirectory-of-remote-repo-with-git-subtree
  4. https://stackoverflow.com/questions/20102594/git-subtree-push-squash-does-not-squash
  5. https://stackoverflow.com/questions/9614255/git-subtree-push-squash-needs-a-long-time-and-it-is-getting-longer-over-time
  6. https://stackoverflow.com/questions/10081681/git-subtree-push-changes-back-to-subtree-project
  7. https://stackoverflow.com/questions/26928299/why-does-git-subtree-push-always-list-hundreds-of-commits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment