>_ IzzyLab

Hands-on task

This task is step 20 of the course “Git: rewriting and rescuing history”

Keep only one directory in the working tree

medium~5 min

In /root/repo the root contains README.md and three subdirectories with files: src/, docs/, tests/.

Configure sparse-checkout in cone mode so that only the src/ subdirectory remains in the working tree (the files at the repo root stay as well). The docs/ and tests/ directories should disappear from the working tree, but remain in history — that is, you can't delete them with a plain rm; the filtering must be done by sparse-checkout itself.

What you'll practise:
  • git sparse-checkout
  • cone mode
  • partial checkout
  • working tree

Topic breakdown

Sparse-checkout lets you keep only part of the tracked files in the working directory without affecting either the index or the history — handy for monorepos where you don't need the whole volume at once. The modern cone mode works at the directory level: files at the repo root are always kept, and from subdirectories — only those explicitly listed. Git removes the excess directories from the filesystem itself, but they remain in the commit tree, which is fundamentally different from a plain rm. Whether the mode is active is reflected in core.sparseCheckout.

What's checked

  1. src directory remained
  2. docs directory removed from the working tree
  3. tests directory removed from the working tree
  4. sparse-checkout enabled

What it looks like