Bird
Raised Fist0
Gitdevops~10 mins

Sparse checkout for partial repos in Git - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Sparse checkout for partial repos
Initialize git repo
↓
Enable sparse checkout
↓
Define sparse-checkout patterns
↓
Git checkout triggers partial files
↓
Work with partial repo files
↓
Modify sparse patterns to add/remove files
↓
Git checkout updates working directory
This flow shows how to set up and use sparse checkout to work with only parts of a git repository.
Execution Sample
Git
git clone --no-checkout <repo>
git sparse-checkout init --cone
git sparse-checkout set docs/
git checkout main
Clone repo without checkout, enable sparse checkout, set to include only 'docs/' folder, then checkout branch.
Process Table
StepCommandActionResult
1git clone --no-checkout <repo>Clone repo without checking out filesWorking directory empty (.git with fetched data)
2git sparse-checkout init --coneEnable sparse checkout modeSparse checkout enabled with cone mode
3git sparse-checkout set docs/Set sparse patterns to include 'docs/' folderSparse config updated to include only 'docs/'
4git checkout mainCheckout branch with sparse patterns appliedOnly 'docs/' folder files checked out in working directory
5git sparse-checkout set docs/ src/Add 'src/' folder to sparse patternsWorking directory updated to include 'docs/' and 'src/' folders
6git sparse-checkout disableDisable sparse checkoutFull repo files restored in working directory
💡 Sparse checkout stops when patterns are set and checkout completes with only specified files present.
Status Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6
Sparse Patternsnoneenabled (empty)['docs/']['docs/', 'src/']disabled (full repo)
Working Directory Filesemptyempty'docs/' files'docs/' and 'src/' filesall repo files
Key Moments - 3 Insights
Why do I see only some files after checkout?
Because sparse checkout patterns limit files checked out. See execution_table step 4 where only 'docs/' files appear.
What happens if I add more folders to sparse patterns?
Git updates working directory to include those folders, as shown in step 5 where 'src/' is added.
How to get back all files after sparse checkout?
Disable sparse checkout with 'git sparse-checkout disable' as in step 6 to restore full repo files.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, after which step do only 'docs/' files appear in the working directory?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Check the 'Result' column for step 4 in execution_table.
According to variable_tracker, what is the state of 'Sparse Patterns' after step 5?
A['docs/']
B['docs/', 'src/']
Cdisabled
Denabled (empty)
💡 Hint
Look at the 'Sparse Patterns' row under 'After Step 5' in variable_tracker.
If you want to see all files again, which command corresponds to that in the execution_table?
Agit sparse-checkout disable
Bgit sparse-checkout init --cone
Cgit sparse-checkout set docs/
Dgit checkout main
💡 Hint
Refer to step 6 in execution_table where sparse checkout is disabled.
Concept Snapshot
Sparse checkout lets you work with only parts of a git repo.
Enable it with 'git sparse-checkout init'.
Set folders/files with 'git sparse-checkout set <paths>'.
Checkout updates working directory to match patterns.
Disable with 'git sparse-checkout disable' to restore full repo.
Full Transcript
Sparse checkout is a git feature to check out only parts of a repository. First, clone the repo without files using 'git clone --no-checkout <repo>'. Then enable sparse checkout using 'git sparse-checkout init --cone'. Next, define which folders or files you want by using 'git sparse-checkout set' followed by the paths, for example 'docs/'. When you run 'git checkout' after setting patterns, git updates your working directory to include only those files. You can add more folders later by updating the sparse patterns. To return to the full repository, disable sparse checkout with 'git sparse-checkout disable'. This process helps save space and focus on only the parts of the repo you need.

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