Skip to content

Git Sparse Checkout Commands

Overview

The git sparse-checkout command allows you to check out only a subset of the files in a repository. This can be useful for working with large repositories where you only need to work on a small portion of the project. This document covers the basic and advanced usage of git sparse-checkout.

Enabling Sparse Checkout

git sparse-checkout init

Initializes sparse checkout in the repository.

git sparse-checkout init

Example

git sparse-checkout init

This command initializes sparse checkout, setting up the repository to allow sparse checkouts.

Defining Sparse Checkout Patterns

git sparse-checkout set <pattern> [<pattern> ...]

Sets the sparse checkout patterns to include the specified paths.

git sparse-checkout set <pattern> [<pattern> ...]

Example

git sparse-checkout set src/ docs/

This command sets the sparse checkout to include only the src/ and docs/ directories.

git sparse-checkout add <pattern> [<pattern> ...]

Adds additional patterns to the sparse checkout list without removing existing ones.

git sparse-checkout add <pattern> [<pattern> ...]

Example

git sparse-checkout add test/

This command adds the test/ directory to the existing sparse checkout patterns.

Removing Sparse Checkout Patterns

git sparse-checkout remove <pattern> [<pattern> ...]

Removes specified patterns from the sparse checkout list.

git sparse-checkout remove <pattern> [<pattern> ...]

Example

git sparse-checkout remove docs/

This command removes the docs/ directory from the sparse checkout patterns.

Checking Sparse Checkout Patterns

git sparse-checkout list

Lists the current sparse checkout patterns.

git sparse-checkout list

Example

git sparse-checkout list

This command lists all the patterns currently included in the sparse checkout.

Using Cone Mode

git sparse-checkout init --cone

Initializes sparse checkout in "cone mode," which uses simplified and more efficient pattern matching.

git sparse-checkout init --cone

Example

git sparse-checkout init --cone

This command initializes sparse checkout in cone mode for more efficient pattern matching.

git sparse-checkout set --cone <directory> [<directory> ...]

Sets sparse checkout patterns in cone mode, including only the specified directories.

git sparse-checkout set --cone <directory> [<directory> ...]

Example

git sparse-checkout set --cone src/ docs/

This command sets the sparse checkout to include only the src/ and docs/ directories using cone mode.

Disabling Sparse Checkout

git sparse-checkout disable

Disables sparse checkout, making all files in the repository available.

git sparse-checkout disable

Example

git sparse-checkout disable

This command disables sparse checkout and makes all files in the repository available.

Summary

The git sparse-checkout command is a powerful tool for managing large repositories by allowing you to check out only the files you need. This can improve performance and reduce clutter in your working directory. For more detailed information on each command, refer to the official Git documentation.