Sub topic 2
What Git stores versus what you see on disk
This chapter assumes you can clone, commit, branch, and read git status, and that you are comfortable in a terminal. It explains the three places Git keeps your files. Sparse checkout changes only one of them.
Three places, not one
A normal clone seems to give you "the project". Git actually keeps three separate things.
- The object database is in
.git/objects. It holds every commit, every directory listing (called a tree), and every version of every file (called a blob). A blob is the raw contents of one file version, with no name attached. This is the repository history. - The index, also called the staging area, is the file
.git/index. It lists the paths Git tracks for your current checkout and which blob each path points to. It also records file metadata, such as timestamps, so Git can detect changes quickly. - The working tree is the ordinary files and directories you edit. Git writes them from the index when you check out a commit.
Only the working tree is something you open in an editor. The other two are internal, but they decide how much work Git does.
Seeing the difference
Each place can be counted separately. Run these in a freshly cloned repository:
git ls-tree -r --name-only HEAD | wc -l
git ls-files | wc -l
find . -path ./.git -prune -o -type f -print | wc -lThe first command counts files recorded in the current commit. The second counts paths in the index. The third counts regular files on disk, ignoring .git. In a normal clone all three numbers match, apart from untracked or ignored files you have created. That match is not a rule of Git. It is what a default checkout does, and sparse checkout breaks it on purpose.
You can also look at the history without touching the working tree. This command lists the top-level entries of the latest commit, whether or not those directories exist on disk:
git ls-tree HEADWhy the working tree is the costly part
In a large monorepo, the same files are paid for in different ways.
| Place | Cost grows with | Paid when |
|---|---|---|
| Object database | Total history and file versions | Clone and fetch |
| Index | Number of tracked paths | Almost every command |
| Working tree | Number of checked-out files | Checkout, switch, and status |
Commands such as git status compare the index with the working tree. They must examine every tracked path, so a million paths cost far more than a thousand, even if you only edit ten files. Switching branches rewrites every file that differs between the two commits. A build tool or editor that scans the directory sees every file too.
Most developers on a monorepo touch one or two directories. The cost they pay depends on the whole repository.
What this means for sparse checkout
The key point is that the working tree can be a subset of what is committed. Git can keep the full history, so branches, tags, and git log all work, while writing only some directories to disk. Nothing is deleted from the repository. Files outside your selection stay in the object database, and a commit you make still contains the complete tree.
That separates two questions:
- Which commits and file versions do I have? This is controlled by clone and fetch options, covered in a later chapter on partial clone.
- Which files are written to disk? This is the job of sparse checkout.
A sparse checkout also does not shrink .git by itself. If you clone fully and then enable sparse checkout, the objects are already downloaded. Saving download time and disk space in .git needs a partial clone as well.
Common misunderstandings
- "Deleting a directory from disk is the same as excluding it." It is not. If you delete a tracked directory by hand, Git reports its files as deleted and
git commit -awould remove them from history going forward. Sparse checkout marks them as intentionally absent instead. - "Sparse checkout makes my clone smaller." It reduces files on disk. The size of
.gitstays the same unless the clone was also partial. - "The excluded files are lost." They are still in the object database and return when you widen the selection.
Exercise
You clone a monorepo normally, then delete the folder services/billing with rm -rf. What does git status show, and why is this different from excluding that folder with sparse checkout?
Show answer
git status lists every file under services/billing as deleted, because the index still records those paths and the working tree no longer has them. Git treats this as an uncommitted change that you could stage and commit. With sparse checkout, Git marks those paths as intentionally absent, so they are not reported as deletions and a commit keeps them unchanged.