Jae Wilson @DataCrew

Use Git Subtree Filtering to pull a subfolder out of a GitHub repository (and still receive updates)

May 2, 2024

Hook (1-2 sentences): You have a large monorepo and someone needs access to exactly one subfolder — not the whole thing. Giving them the entire repository isn't an option, but you also don't want to manually sync changes into a separate copy forever.

Why It Matters (2-4 sentences): Without a structured approach, you're stuck choosing between granting overly broad repository access or maintaining a manually-synced copy that inevitably drifts. Git subtree filtering lets you carve out a subfolder into its own standalone repository while preserving a clean path to pull updates from the source. This pattern is particularly useful for security reviews, open-sourcing a subset of private code, or sharing utilities with external teams — without coupling them to your entire codebase.

What You'll Learn

  • Understand the three-branch strategy that keeps your filtered repo in sync with the source
  • Clone a source repository and rewrite its history down to a single subfolder using git filter-branch
  • Wire the filtered content as a remote into a new standalone repository
  • Pull upstream changes into the filtered branch and merge selectively into your own work
  • Recognize when this approach beats alternatives like git submodules or manual copying