Bird
Raised Fist0
Gitdevops~7 mins

Sparse checkout for partial repos in Git - Commands & Configuration

Choose your learning style10 modes available
Introduction
Sometimes repositories are very large, but you only need a small part of the files. Sparse checkout lets you download and work with only the parts you want, saving space and time.
When you want to clone a huge repository but only need one folder or file inside it.
When your internet is slow and downloading the full repo would take too long.
When your computer has limited storage and cannot hold the entire repository.
When you want to work on a specific module or feature without distractions from other files.
When you want to speed up build or test processes by only checking out relevant code.
Commands
Clone the repository without checking out files. This prepares the repo for sparse checkout.
Terminal
git clone --no-checkout https://github.com/git/git.git my-git-repo
Expected OutputExpected
Cloning into 'my-git-repo'... remote: Enumerating objects: 100000, done. remote: Counting objects: 100% (100000/100000), done. remote: Compressing objects: 100% (50000/50000), done. Receiving objects: 100% (100000/100000), 50.00 MiB | 2.00 MiB/s, done. Resolving deltas: 100% (70000/70000), done.
→
--no-checkout - Prevents automatic checkout of files after cloning
Change directory into the cloned repository to run further git commands.
Terminal
cd my-git-repo
Expected OutputExpected
No output (command runs silently)
Enable sparse checkout mode with cone pattern support for easier folder selection.
Terminal
git sparse-checkout init --cone
Expected OutputExpected
Sparse checkout initialized.
→
--cone - Simplifies sparse patterns to folder-based rules
Specify the folder 'Documentation' to be checked out. Only this folder will be downloaded.
Terminal
git sparse-checkout set Documentation
Expected OutputExpected
Updated working directory.
Checkout the main branch to populate the working directory with only the specified sparse files.
Terminal
git checkout main
Expected OutputExpected
Updating files: 100% (500/500), done.
List files and folders in the current directory to verify only the 'Documentation' folder is present.
Terminal
ls
Expected OutputExpected
Documentation
Key Concept

Sparse checkout lets you work with only parts of a repository by downloading just the files or folders you need.

Common Mistakes
Cloning the repo normally without --no-checkout and then trying to enable sparse checkout.
Git will have already checked out all files, so sparse checkout won't reduce files on disk.
Use 'git clone --no-checkout' first, then enable sparse checkout before checking out files.
Not running 'git sparse-checkout init' before setting sparse paths.
Sparse checkout is not enabled, so setting paths has no effect.
Always run 'git sparse-checkout init' to enable sparse checkout mode first.
Using complex sparse patterns without --cone mode when only folders are needed.
Complex patterns are harder to manage and may cause unexpected files to appear.
Use '--cone' mode for simple folder-based sparse checkout.
Summary
Clone the repository with --no-checkout to avoid downloading all files.
Enable sparse checkout mode with 'git sparse-checkout init --cone'.
Specify which folders or files to include using 'git sparse-checkout set'.
Checkout the branch to populate the working directory with only the selected files.
Verify by listing files to confirm only the desired parts are present.

Practice

(1/5)
1. What is the main purpose of using git sparse-checkout?
easy
A. To merge two branches automatically
B. To clone a repository faster by skipping all files
C. To delete files from the repository permanently
D. To download only specific parts of a repository you need

Solution

  1. Step 1: Understand sparse checkout concept

    Sparse checkout allows you to work with only parts of a repo instead of the whole repo.
  2. Step 2: Identify the main purpose

    This helps save space and time by downloading only needed folders or files.
  3. Final Answer:

    To download only specific parts of a repository you need -> Option D
  4. Quick Check:

    Sparse checkout = partial repo download [OK]
Hint: Sparse checkout means partial repo download only [OK]
Common Mistakes:
  • Thinking it deletes files from the repo
  • Confusing with branch merging
  • Assuming it skips all files
2. Which command correctly initializes sparse checkout with cone mode?
easy
A. git sparse-checkout start --cone
B. git sparse-checkout init --cone
C. git sparse-checkout enable --cone
D. git sparse-checkout create --cone

Solution

  1. Step 1: Recall the correct initialization command

    The official command to start sparse checkout in cone mode is git sparse-checkout init --cone.
  2. Step 2: Verify other options

    Other commands like start, enable, or create are not valid git sparse-checkout commands.
  3. Final Answer:

    git sparse-checkout init --cone -> Option B
  4. Quick Check:

    Init with --cone = git sparse-checkout init --cone [OK]
Hint: Use 'init' with '--cone' to start sparse checkout [OK]
Common Mistakes:
  • Using 'start' instead of 'init'
  • Forgetting the '--cone' flag
  • Using invalid verbs like 'enable' or 'create'
3. After running git sparse-checkout set docs src, which folders will be present in your working directory?
medium
A. No folders, only files at root
B. All folders except 'docs' and 'src'
C. Only the 'docs' and 'src' folders
D. All folders in the repository

Solution

  1. Step 1: Understand the effect of 'git sparse-checkout set'

    This command sets which folders to include in the working directory.
  2. Step 2: Analyze the given folders

    Setting 'docs' and 'src' means only these two folders will be checked out and visible.
  3. Final Answer:

    Only the 'docs' and 'src' folders -> Option C
  4. Quick Check:

    Set folders = only those folders appear [OK]
Hint: Set folders to include with 'git sparse-checkout set' [OK]
Common Mistakes:
  • Assuming all folders remain
  • Thinking it excludes specified folders
  • Expecting files outside these folders to appear
4. You ran git sparse-checkout set src but still see all folders. What is the likely mistake?
medium
A. You forgot to run git sparse-checkout init --cone first
B. You used the wrong folder name in the set command
C. You need to delete the .git folder to apply changes
D. Sparse checkout does not work on folders, only files

Solution

  1. Step 1: Check sparse checkout initialization

    Before setting folders, you must initialize sparse checkout with git sparse-checkout init --cone.
  2. Step 2: Understand why all folders appear

    If not initialized, sparse checkout settings are ignored, so all folders remain visible.
  3. Final Answer:

    You forgot to run git sparse-checkout init --cone first -> Option A
  4. Quick Check:

    Init missing = no sparse effect [OK]
Hint: Always init sparse checkout before setting folders [OK]
Common Mistakes:
  • Skipping initialization step
  • Deleting .git folder unnecessarily
  • Believing sparse checkout works only on files
5. You want to work only on the frontend and backend/api folders in a large repo. Which sequence of commands correctly sets this up?
hard
A. git sparse-checkout init --cone git sparse-checkout set frontend backend/api
B. git sparse-checkout set frontend backend/api git sparse-checkout init --cone
C. git sparse-checkout init git sparse-checkout set frontend backend/api
D. git sparse-checkout init --cone git sparse-checkout add frontend backend/api

Solution

  1. Step 1: Initialize sparse checkout with cone mode

    Use git sparse-checkout init --cone to enable sparse checkout in cone mode.
  2. Step 2: Set the folders to include

    Use git sparse-checkout set frontend backend/api to specify the folders you want.
  3. Step 3: Verify command order and syntax

    Initialization must come before setting folders; 'add' is not a valid command.
  4. Final Answer:

    git sparse-checkout init --cone git sparse-checkout set frontend backend/api -> Option A
  5. Quick Check:

    Init then set folders = correct sequence [OK]
Hint: Init sparse checkout first, then set folders [OK]
Common Mistakes:
  • Setting folders before initialization
  • Using 'add' instead of 'set'
  • Omitting the '--cone' flag during init
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard