HOWTO · Git

How to Clone a Subdirectory of a Git Repository

Use Git sparse-checkout to select repository directories in your working tree.

On this page

To work with one directory from a Git project, use sparse-checkout to select paths for the working tree. This keeps the work inside the original repository; it does not create a separate repository containing only that directory.

What Sparse Checkout Does (and Does Not Do)

A normal clone checks out the repository’s files into a working tree. Git sparse-checkout lets you choose which tracked paths are populated there, while the checkout remains attached to the same repository and its history. It is useful when you need to work mainly in one module, such as frontend in a project that also has backend and documentation directories.

Selecting paths does not guarantee a smaller download. Sparse checkout controls the working tree; the amount of repository data fetched depends on the clone options and Git setup. If you need an independent project with its own history, the source project must provide a separate repository for that module.

Clone a Repository with Sparse Checkout

First confirm that the installed Git provides the sparse-checkout command, for example by checking git help -a. Use the commands below only when that interface is available. They clone without populating the working tree, then select frontend. Replace the example repository URL and directory names with your own values; the URL and paths below are placeholders.

This self-contained Bash example creates a small local repository and selects its frontend directory:

set -eu
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT

git init --quiet "$work/source"
git -C "$work/source" config user.name "Example User"
git -C "$work/source" config user.email "example@example.com"
mkdir -p "$work/source/frontend" "$work/source/backend"
printf 'app source\n' > "$work/source/frontend/app.txt"
printf 'service source\n' > "$work/source/backend/service.txt"
git -C "$work/source" add frontend backend
git -C "$work/source" commit -m "Add project modules" >/dev/null
branch=$(git -C "$work/source" branch --show-current)

git clone --no-checkout --branch "$branch" "$work/source" "$work/project" >/dev/null 2>&1
git -C "$work/project" sparse-checkout init --cone
git -C "$work/project" sparse-checkout set frontend
git -C "$work/project" sparse-checkout list
frontend

For a remote project, the corresponding steps are git clone --no-checkout <repository-url> my_project, cd my_project, git sparse-checkout init --cone, and git sparse-checkout set frontend. Run them in order. The selected paths are relative to the repository root. If your Git installation does not provide sparse-checkout, use git clone <repository-url> my_project to get a full working tree instead.

Change the Selected Directories

To replace the selected paths with backend and documentation, run git sparse-checkout set backend documentation from the repository. To keep frontend selected and add another directory, use git sparse-checkout add backend. Cone mode is intended for selecting directories, so list directory paths rather than individual files.

Use paths relative to the repository root, with the same spelling and capitalization as the tracked paths. If a selection does not show the files you expect, check the path with git ls-tree --name-only HEAD for top-level entries, then set the correct directory. A nested path can be selected by including its path from the root, such as frontend/components.

Verify the Checkout and Troubleshoot Common Issues

Run git sparse-checkout list to see the selected paths and git status --short to inspect the working tree. Confirm that you are in the intended repository with git rev-parse --show-toplevel; selected directory names must be interpreted relative to that root.

If the command is unavailable, the sparse-checkout interface is not supported by that Git installation, or the repository workflow does not permit it, use a full clone. If the directory is selected but expected files are absent, check that the path exists in the checked-out commit and that it is spelled correctly. Untracked files already present in the working tree can remain visible even when their paths are outside the sparse selection.

For more information, see the Git sparse-checkout documentation, git clone documentation, and git help documentation.