Skip to main content

🧩 Multi-Folder Git Subtree Sync

Multi-Folder Git Subtree Sync​

Git Subtrees let you share multiple folders (like src/components, docs, and scripts) across different repositories.
Resources can be organized inside folders (recommended for clarity) or placed at the root of the shared repo.
Both approaches work β€” the key is to use the correct --prefix path when adding the subtree.


🧩 Shared Repository Structure Options​

shared-resources/
β”œβ”€β”€ src/components/
β”‚ β”œβ”€β”€ Button.tsx
β”‚ β”œβ”€β”€ Navbar.tsx
β”œβ”€β”€ docs/
β”‚ β”œβ”€β”€ getting-started.md
β”‚ β”œβ”€β”€ api-reference.md
└── scripts/
β”œβ”€β”€ build.sh
β”œβ”€β”€ deploy.sh

πŸͺœ Setup Steps​

git remote add shared https://github.com/ruseleredu/docusaurus-shared.git

πŸ“Š Workflow Overview​

SetupProsCons
FoldersClear separation of components, docs, scriptsSlightly more commands to manage
RootSimpler if only a few filesCan get messy with many mixed resources

βœ… Best Practices​

  • Prefer folders for clarity and scalability.
  • Use --squash to keep history clean.
  • Document the workflow in your site’s README so teammates know how to sync.
  • Always pull before pushing to avoid conflicts.

Sharing a Single File with Git Subtree​

Git Subtree works at the directory level, but you can still share a single file by either:

  • Placing it inside a dedicated folder (recommended for scalability).
  • Keeping it at the root of the shared repo.

🧩 Shared Repository Structure Options​

shared-resources/
└── scripts/
└── deploy.sh

πŸͺœ Setup Steps​

git remote add shared https://github.com/your-org/shared-resources.git

πŸ“Š Comparison​

SetupCommandBest Use Case
Foldergit subtree add --prefix=scripts shared main --squashWhen you may add more files later
Rootgit subtree add --prefix=deploy.sh shared main --squashWhen you only need one file

βœ… Best Practices​

  • Use a folder if you expect to add more shared files later.
  • Root setup is fine for truly single‑file sharing.
  • Always pull before pushing to avoid conflicts.
  • Use --squash to keep history clean.