Sub topic 14
Clone Patterns for CI Pipelines
This chapter assumes you know the basics of git clone, fetch, and branches. It also assumes you have read the earlier chapters on shallow clones (--depth), partial clones (--filter), and sparse checkout. Here we put those pieces together for continuous integration (CI) jobs, where a fresh machine clones the repository on every run.
Why CI Is a Special Case
A developer clones a repository once and then fetches small updates for months. A CI job often starts from an empty workspace, so it pays the full clone cost every time. Multiply a 2 GB clone by hundreds of runs per day and the cost shows up as slow pipelines, network charges, and load on the Git server.
CI jobs also have a different need from people. Most jobs read only the current files. They build, test, or lint one snapshot. They rarely browse history. That makes them good candidates for the cheapest clone that still works.
The questions to ask for each job are:
- Does it need history, or only the current commit?
- Does it need every branch and tag?
- Does it need every directory?
- Does it need file contents from old commits?
Each answer maps to one tool you already know.
| Job needs | Tool |
|---|---|
| Only the latest commit | `--depth 1` |
| One branch, no tags | `--single-branch --no-tags` |
| History but few file contents | `--filter=blob:none` |
| Only part of the tree | `--sparse` plus `sparse-checkout set` |
Pattern 1: The Minimal Snapshot Clone
Most build and test jobs need only the files at one commit. The smallest common starting point is:
git clone --depth 1 --single-branch --no-tags --branch main https://example.com/org/big-repo.gitThis fetches one commit on one branch and no tags. The download is roughly the size of the current tree, plus a small amount of metadata.
Many CI systems run this for you. GitHub Actions' checkout action fetches with depth 1 by default. GitLab CI uses a GIT_DEPTH variable, and Jenkins has a shallow clone option in its Git plugin. Check your system's default before adding your own commands, because the default may differ from what you assume.
Checking Out a Specific Commit
CI often builds a specific commit, not a branch tip. A pull request job, for example, builds a merge commit or a head commit that the CI system names for you. A plain --branch clone cannot fetch an arbitrary commit. Instead, initialize an empty repository and fetch exactly one commit:
git init build
cd build
git remote add origin https://example.com/org/big-repo.git
git fetch --depth 1 origin "$COMMIT_SHA"
git checkout --detach FETCH_HEADThis pattern avoids downloading any branch you do not need. It requires the server to allow fetching by object ID. Hosted services such as GitHub and GitLab allow it. Some self-hosted servers need uploadpack.allowReachableSHA1InWant or uploadpack.allowAnySHA1InWant enabled.
Always quote the variable. A value from a pipeline is data from outside your script, and unquoted use in a shell command is an injection risk.
Pattern 2: Shallow With Enough History
Some jobs need a bit of history. Common examples:
- A job that finds changed files with
git diff origin/main...HEAD. This needs the merge base, the commit where the branch diverged. - A release job that runs
git describeto build a version string from the nearest tag. - A changelog tool that reads the last N commits.
With --depth 1, these fail or give wrong results. The merge base is missing, so Git reports no merge base. Or git describe cannot reach a tag and prints a bare commit hash or fails.
You have three options.
- Pick a depth that is large enough, such as
--depth 50, and accept that it is a guess. - Start shallow and deepen only if needed.
- Use a blobless clone, which keeps full history without file contents.
Option 2 looks like this:
git clone --depth 1 --single-branch --branch feature/login https://example.com/org/big-repo.git
cd big-repo
git fetch --deepen 50 origin main
git merge-base origin/main HEAD || git fetch --unshallow originThe script tries a modest deepening first. If no merge base appears, it falls back to a full history fetch. This keeps the common case fast and still handles long-lived branches.
A shallow clone created with --single-branch only tracks that one branch, so origin/main may not exist until you fetch it. In the script above, git fetch --deepen 50 origin main is written to name the branch explicitly. If the remote-tracking ref is not created in your Git version, fetch with an explicit refspec such as +refs/heads/main:refs/remotes/origin/main.
Pattern 3: Blobless Clones for History-Aware Jobs
When a job needs real history, a blobless clone is usually better than a deep shallow clone. It downloads every commit and tree but skips file contents:
git clone --filter=blob:none https://example.com/org/big-repo.gitHistory commands such as git log, git describe, and git merge-base work as normal because commits and trees are present. Git fetches blobs lazily, only for the files the checkout needs. A checkout of the default branch downloads blobs for that one snapshot.
The trade-off is that any command that reads old file contents triggers on-demand fetches from the remote. Examples are git blame over a large file, git log -p, and git diff across many commits. Each fetch is a network round trip. In CI this can be slower than a single full clone if the job touches a lot of old content. Measure before choosing.
A treeless clone (--filter=tree:0) goes further and omits trees too. It is a good fit for a build that needs only the current snapshot and a bit of commit metadata:
git clone --filter=tree:0 --depth 1 https://example.com/org/big-repo.gitAvoid treeless clones for jobs that run history commands. Git must fetch trees repeatedly, and this is often slower than a blobless clone.
Pattern 4: Monorepo Jobs With Sparse Checkout
In a monorepo, a job for one service should not download or write files for every other service. Combine a blobless filter with sparse checkout:
git clone --filter=blob:none --sparse https://example.com/org/monorepo.git
cd monorepo
git sparse-checkout set services/api docs
git sparse-checkout listGit downloads blobs only for the paths in the sparse set. The working tree stays small, so file system work such as indexing and virus scanning also drops.
Combine this with a shallow depth when history is not needed:
git clone --filter=blob:none --depth 1 --sparse https://example.com/org/monorepo.gitNote that sparse checkout limits the working tree. It does not limit commits or trees. A build tool that expects a file outside the sparse set will fail with a missing file error. List every directory the build reads, including shared libraries and configuration directories.
Pattern 5: Caching a Reference Clone
If runners are long-lived or share a disk, you can avoid repeated downloads. Keep one full mirror on the runner and clone from it:
git clone --mirror https://example.com/org/big-repo.git /cache/big-repo.git
git --git-dir=/cache/big-repo.git remote update --prune
git clone --reference /cache/big-repo.git --dissociate https://example.com/org/big-repo.git workThe first command runs once. The second updates the mirror before each job and transfers only new objects. The third clone borrows objects from the local mirror. The --dissociate flag copies the borrowed objects into the new clone, so deleting the cache later does not break it.
This pattern costs disk space and cache management, but it can turn a multi-minute clone into seconds. It does not suit ephemeral runners that start empty each time, unless your CI system can restore the cache directory.
Choosing a Pattern
A short summary of the trade-offs:
| Pattern | Download size | History available | Main risk |
|---|---|---|---|
| Depth 1, single branch | Smallest | One commit | Merge base and `describe` fail |
| Shallow plus deepen | Small | Partial | Needs a fallback script |
| Blobless | Medium | Full | Lazy fetches on old content |
| Treeless with depth 1 | Small | Minimal | Slow with history commands |
| Sparse plus filter | Small | Full or shallow | Missing files outside the set |
| Reference mirror | Small per job | Full | Cache upkeep |
Operational Concerns
Server Support
Filters need server support. If a server does not support them, git clone --filter prints a warning such as filtering not recognized by server, ignoring and performs a full clone. A job that silently becomes a full clone is slow but still correct. Watch clone times so you notice the change.
Authentication and Lazy Fetches
A blobless clone keeps a connection to the remote for later object fetches. If your CI token is short-lived, a lazy fetch after the token expires fails. Keep credentials available for the whole job, or force all needed blobs early with git checkout of the target commit.
Fetch Depth and Tags
If you pass --no-tags and later need a version tag, git describe fails. Decide this up front. Fetch only the tag you need with git fetch --depth 1 origin tag v2.4.0.
Detecting What You Got
Add a diagnostic step to the start of a job when troubleshooting:
git rev-parse --is-shallow-repository
git config --get remote.origin.promisor
git config --get remote.origin.partialclonefilter
git sparse-checkout listThese show whether the repository is shallow, whether it is a partial clone, which filter it used, and which paths are checked out.
Common Errors
fatal: no merge base. The clone is too shallow. Deepen or unshallow.git describereturns only a hash. Tags were skipped or are out of reach. Fetch the tag or deepen.- A job is slow despite
--filter. A command reads many old blobs. Look forgit log -p,git blame, or a tool that diffs against old commits. - Missing file during a build. The sparse set is too narrow.
Server does not allow request for unadvertised object. The server does not allow fetch by commit ID. Fetch a branch or tag instead.
Exercise
A pull request job runs git diff --name-only origin/main...HEAD to decide which services to test. It clones with --depth 1 and fails with "no merge base". Name two ways to fix it, and say which you would choose for a repository with 200,000 commits and a few branches that live for months.
Show answer
Option one is to deepen the clone, with git fetch --deepen N or a fallback to git fetch --unshallow when git merge-base fails. Option two is to clone with --filter=blob:none, which keeps all commits and trees so the merge base is always available. For long-lived branches, a fixed depth is unreliable, so the blobless clone is the better choice. It also avoids guessing a depth. The diff of file names needs only trees, so it triggers few or no blob fetches.
Exercise
A monorepo job builds services/api, which imports shared code from libs/common. You run git sparse-checkout set services/api and the build fails with a missing file error. What went wrong and what is the fix?
Show answer
Sparse checkout removed libs/common from the working tree, so the build cannot read it. Add the directory with git sparse-checkout set services/api libs/common. List every directory the build reads.