Lesson 2 of 6 Repository Management

Scaling Repositories with Scalar and Sharing Code

This lesson explains Git's scaling features, and how to share code across repositories.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish shallow clone, partial clone and sparse-checkout.
  • Describe what Scalar configures.
  • Compare packages, submodules and subtrees.
  • Choose a scaling feature for a group of users.

Why large repositories become slow

Slowness has several causes, and each has a different remedy. A clone is slow if it downloads too many objects. A checkout is slow if it writes too many files. Commands such as git status are slow if Git must examine every file in a huge working directory. Knowing the cause tells you which feature to use.

Shallow clone

A shallow clone uses --depth 1 to fetch only recent commits. It greatly reduces download time, and suits build agents that need only the latest code. It limits commands that need history, such as git log and git blame, so it suits developers less well.

Partial clone

A partial clone uses a filter such as --filter=blob:none. It downloads all commits and trees, but file contents only when they are needed. Developers keep a full history, while the initial download stays small.

Sparse-checkout

Sparse-checkout limits the working directory to selected folders, so a developer who works on one service sees only that service. It reduces disk use and speeds up commands, and is often combined with partial clone in monorepos.

Maintenance and caches

Commit-graph files speed up history operations, and git maintenance runs such tasks in the background. Build systems can also reduce clone time through a reference repository or by reusing a workspace on self-hosted agents.

Match the pairs

Matching the remedy to the cause

Exam skills 2.2.2

Match each scaling feature with the problem it solves.

Log in to try this activity Interactive activities are free with an account.

Scalar

Enabling all those features by hand is error-prone. Scalar is a tool included with Git, originally developed by Microsoft, that applies recommended settings for large repositories in one step. scalar clone differs from a plain clone in several ways.

  • Only commit and tree objects are cloned by default, and file contents arrive on demand.
  • Sparse-checkout is enabled, and only the files in the top-level directory are present at first. git sparse-checkout set expands the set of folders, and --full-clone turns this off.
  • Background maintenance is scheduled, with tasks for the commit-graph, prefetching, loose objects and incremental repacking.
  • Recommended configuration values are set for the index, and for fetch and pack behaviour.
# Clone with recommended large-repository settings
scalar clone https://dev.azure.com/contoso/monorepo/_git/monorepo
cd monorepo/src

# Work only on two folders
git sparse-checkout set services/payments libraries/shared

# Apply Scalar's configuration to an existing clone
scalar register

# List the repositories that Scalar manages
scalar list

scalar clone places the working tree in a src folder beneath the target directory, so that build outputs can stay outside the repository. scalar register adds an existing clone to the list and schedules maintenance, so there is no need to clone again.

The following diagram matches two groups that work with a 400-service monorepo, build agents and developers, to a scaling feature for each.

Two groups, two ways to work with a 400-service monorepo. Build agents need only the latest commit, so a shallow clone with fetch depth 1 cuts the 25-minute clone. Developers need history but work in one or two folders, so Scalar gives them partial clone and sparse-checkout.
The scenario is illustrative.
True or false

What Scalar does

Exam skills 2.2.2

Check what scalar clone and its companion commands set up.

Log in to try this activity Interactive activities are free with an account.

Sharing code across repositories

Copying code between repositories creates divergence, so a sharing method is needed. The following options suit different needs.

Packages

Publishing a library to a package feed is usually the best option. Consumers declare a version, and they upgrade when ready, while the owning team releases independently. Package management is covered in its own module.

Git submodules

A submodule records a pointer to a specific commit of another repository in a folder. This is useful when consumers need the source, but it adds steps: developers run git submodule update --init, and pipelines must set submodules: true on checkout. Forgetting to update a pointer is a common mistake.

Git subtrees

A subtree copies another repository's content into a folder, with its history. Consumers need no special commands, but pushing changes back upstream is less convenient.

Pipeline and template sharing

Pipeline logic is shared through YAML templates in a separate repository and through reusable workflows, which are explained in a later module. Azure Pipelines can check out several repositories in one job with a resources: repositories block.

Monorepos are the alternative: all code in one repository, which makes atomic changes across components easy. They need the scaling features described above, and path filters in pipelines. The following table recaps the choices.

NeedRecommended option
Versioned library with independent releasesPackage feed
Consumers need source at a pinned commitGit submodule
Include source without extra commandsGit subtree
Atomic changes across componentsMonorepo
Shared pipeline logicYAML templates or reusable workflows
Scenario questions

Choosing a sharing method

Exam skills 2.2.2

Choose how code should be shared between repositories.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing a scaling feature

A monorepo holds 400 services. Build agents take 25 minutes to clone it, and they need only the latest commit. Developers need full commit history, but each works in one or two service folders. What should you recommend for each group?

Build agents should use a shallow clone, with a fetch depth of 1, because they need no history. Developers should use Scalar, which provides partial clone behaviour and sparse-checkout, so they keep history while downloading and checking out only what they use. Git LFS would not help unless large binaries were the cause, and splitting the repository is a major change that the requirements do not call for.

Check yourself

A developer needs full history, but not all file contents immediately. Which clone type fits?

Show answer

A partial clone, such as --filter=blob:none. A shallow clone would remove history, which the developer needs.

Exam Tips

Remember for the exam

  • Shallow clone reduces history; partial clone defers file contents.
  • Sparse-checkout reduces the files in the working directory.
  • Scalar applies recommended settings for large repositories.
  • Packages are the default for sharing libraries across repositories.
  • Set submodules: true on checkout for pipelines that use submodules.