π§© 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β
- Organized in Folders
- Files at Root
shared-resources/
βββ src/components/
β βββ Button.tsx
β βββ Navbar.tsx
βββ docs/
β βββ getting-started.md
β βββ api-reference.md
βββ scripts/
βββ build.sh
βββ deploy.sh
shared-resources/
βββ Button.tsx
βββ Navbar.tsx
βββ getting-started.md
βββ api-reference.md
βββ build.sh
βββ deploy.sh
πͺ Setup Stepsβ
- Add Remote
- Add Subtrees
- Pull Updates
- Push Changes
git remote add shared https://github.com/ruseleredu/docusaurus-shared.git
If resources are in folders:β
git subtree add --prefix=src/components shared main --squash
git subtree add --prefix=docs shared main --squash
git subtree add --prefix=scripts shared main --squash
If resources are at root:β
git subtree add --prefix=. shared main --squash
git subtree pull --prefix=src/components shared main --squash
git subtree pull --prefix=docs shared main --squash
git subtree pull --prefix=scripts shared main --squash
Or, if root:
git subtree pull --prefix=. shared main --squash
git subtree push --prefix=src/components shared main
git subtree push --prefix=docs shared main
git subtree push --prefix=scripts shared main
Or, if root:
git subtree push --prefix=. shared main
π Workflow Overviewβ
| Setup | Pros | Cons |
|---|---|---|
| Folders | Clear separation of components, docs, scripts | Slightly more commands to manage |
| Root | Simpler if only a few files | Can get messy with many mixed resources |
β Best Practicesβ
- Prefer folders for clarity and scalability.
- Use
--squashto 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β
- File Inside Folder
- File at Root
shared-resources/
βββ scripts/
βββ deploy.sh
shared-resources/
βββ deploy.sh
πͺ Setup Stepsβ
- Add Remote
- Add Subtree
- Pull Updates
- Push Changes
git remote add shared https://github.com/your-org/shared-resources.git
If file is inside a folder:β
git subtree add --prefix=scripts shared main --squash
If file is at root:β
git subtree add --prefix=deploy.sh shared main --squash
π Comparisonβ
| Setup | Command | Best Use Case |
|---|---|---|
| Folder | git subtree add --prefix=scripts shared main --squash | When you may add more files later |
| Root | git subtree add --prefix=deploy.sh shared main --squash | When 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
--squashto keep history clean.