Git works well for most .NET projects, but its performance can gradually decline as a repository grows. Developers may notice that commands such as git status, git checkout, git fetch, and git commit take longer than they used to. Visual Studio may also appear unresponsive while refreshing source control information or switching branches.
These problems are not always caused by a slow internet connection or an underpowered development machine. A repository with thousands of files, many branches, large binary assets, or a complex working directory can make routine Git operations expensive.
The right solution depends on which operation is slow. A delayed git status command requires a different investigation from a slow clone or a branch switch that takes several minutes. Applying random Git configuration changes may provide temporary improvement, but it does not address the actual cause.
This article explores practical ways to diagnose and improve Git performance in large .NET repositories, including Git maintenance, file system monitoring, sparse checkout, partial clone, and Azure Repos considerations.
Identify Which Git Operation Is Slow
Start by identifying the command that takes the most time. Different Git operations examine different parts of the repository, so the symptoms provide useful clues.
Git operation | Common reasons for slow performance |
|---|
git status
| Many untracked files, large working directories, file system scanning |
git checkout
| Large working trees, many changed files, expensive file operations |
git fetch
| Many remote branches, large history, large incoming objects |
git clone
| Large repository history, binary files, many branches |
git commit
| Large staged changes, hooks, expensive index updates |
git log
| Large history, expensive graph traversal, missing commit-graph optimization |
Run the affected command from a terminal and observe how long it takes. Compare the result with another repository on the same machine. If other repositories respond quickly, the issue may be related to the size or structure of the affected repository.
You should also check the repository's working directory. .NET projects often contain build output, test results, package folders, and temporary files. Even when these files are ignored by Git, a large number of files can still affect file system operations and developer workflows.
For an initial repository check, run:
git count-objects -vH
This command reports information about loose objects and packed Git objects. The size-pack value is particularly useful when investigating local repository storage.
For a deeper analysis, use git-sizer:
git-sizer --verbose
It reports repository size and structural metrics that can help identify unusually large objects, trees, and histories.
Improve Slow git status Operations
The git status command checks the working directory and index to identify modified, deleted, and untracked files. In a large .NET solution, this can become expensive when the repository contains many directories and generated files.
For example, a developer may have several projects in one solution:
CompanyPlatform/
├── CompanyPlatform.sln
├── src/
│ ├── WebApi/
│ ├── IdentityService/
│ ├── BillingService/
│ └── Shared/
├── tests/
│ ├── WebApi.Tests/
│ └── BillingService.Tests/
├── tools/
└── documentation/
If every project generates build output and test reports, the working directory can contain thousands of additional files.
Start by checking whether generated directories are correctly ignored. Common .NET directories include bin, obj, .vs, and TestResults.
You can inspect ignored files with:
git status --ignored
This helps identify generated content that is present locally but excluded from normal Git status output.
Enable Git file system monitoring
On supported platforms, Git's built-in file system monitor can reduce the work required to detect changes in a working directory.
Enable it with:
git config core.fsmonitor true
Git can use file system notifications to identify which files have changed instead of repeatedly scanning the entire working directory. This is particularly useful for repositories containing many files.
You can also enable the untracked cache:
git config core.untrackedCache true
The untracked cache helps Git avoid repeatedly checking directories that have not changed when looking for untracked files.
These settings are not guaranteed to improve every repository. Their effectiveness depends on the operating system, file system, Git version, and repository structure. Test them in the affected environment and compare command execution times before and after enabling them.
For teams using Visual Studio, also check whether extensions, background tools, or antivirus software are repeatedly scanning the repository. Source control performance can be affected by file system activity outside Git itself.
Reduce the Cost of Branch Switching
Developers frequently switch branches to review pull requests, fix bugs, and work on multiple features. In a large repository, switching branches can take longer when many files need to be updated.
Consider a solution with multiple services and test projects. If a branch changes thousands of files, Git must update the working directory to match the target commit. The time required depends on the number and size of files, disk performance, and file system behavior.
One improvement is to avoid storing unnecessary generated output inside the repository. Build folders should normally be ignored so that developers do not carry large sets of temporary files between branches.
You should also avoid running builds or other tools that modify tracked files while switching branches. Generated source files, modified configuration files, and local changes can make branch switching more complicated.
Before switching, inspect the working tree:
git status
If you have unfinished changes, either commit them or temporarily save them:
git stash push -m "Temporary work before branch switch"
Switch to the required branch:
git switch feature/customer-api
Restore the saved changes when appropriate:
git stash pop
Stashing does not make Git itself faster, but it helps avoid conflicts and failed branch switches caused by local modifications.
Speed Up Cloning and Fetching
Cloning and fetching are often the most noticeable performance problems in large repositories. A normal Git clone downloads the repository's reachable history and checks out the working tree. For a repository with many branches and large historical files, this can require substantial network transfer and disk space.
There are several ways to reduce the amount of data developers and build agents need.
Clone only the required branch
If a developer needs only one branch, use a single-branch clone:
git clone --single-branch --branch main <repository-url>
This avoids downloading the remote-tracking history for every branch during the initial clone.
It is useful for build agents, deployment environments, and developers who work primarily on one branch. However, it is not suitable when the workflow frequently requires comparing or switching between many remote branches.
Use a shallow clone for CI pipelines
Continuous integration jobs often need only the latest source code to build and test an application. Downloading years of history for every pipeline run may be unnecessary.
A shallow clone limits the amount of commit history retrieved:
git clone --depth 1 <repository-url>
This retrieves the latest commit and its working tree without downloading the complete history.
For Azure Pipelines, the checkout step can be configured with a shallow fetch:
steps:
- checkout: self
fetchDepth: 1
A shallow clone is useful for ordinary builds, but some tasks require complete history. Version calculation tools, changelog generators, release automation, and commands that compare commits may need additional history.
For those workflows, configure the pipeline to fetch the required depth or disable shallow fetching.
Use partial clone when full history is required
A shallow clone limits commit history. A partial clone takes a different approach: it can preserve the commit history while downloading selected object types only when needed.
For example:
git clone --filter=blob:none <repository-url>
The blob:none filter initially omits file contents, while retaining the commit and tree information needed to navigate history. Git retrieves missing file contents from the remote when an operation needs them.
This is useful when developers need access to repository history but do not immediately need every historical file version.
Partial clone requires a compatible server and network access to retrieve missing objects. Operations that touch many missing files may trigger additional downloads, so it is important to test the workflow rather than assume every command will become faster.
Use Sparse Checkout in Large .NET Monorepos
Some .NET repositories contain multiple applications, services, libraries, and tools in one repository. This arrangement is often called a monorepo.
For example:
EnterprisePlatform/
├── src/
│ ├── CustomerApi/
│ ├── OrderApi/
│ ├── PaymentService/
│ └── NotificationService/
├── tests/
│ ├── CustomerApi.Tests/
│ ├── OrderApi.Tests/
│ └── PaymentService.Tests/
├── infrastructure/
└── tools/
A developer working only on CustomerApi may not need every other application in the working directory.
Git sparse checkout allows a developer to check out only selected directories while keeping the repository's broader history available.
Start by cloning the repository with blob filtering and sparse checkout:
git clone --filter=blob:none --sparse <repository-url>
Move into the repository:
cd EnterprisePlatform
Select the required directories:
git sparse-checkout set src/CustomerApi tests/CustomerApi.Tests
The working directory now contains the selected paths rather than the complete project tree.
If the developer later needs another service, the sparse checkout can be expanded:
git sparse-checkout add src/Shared
This approach is especially useful when teams maintain many applications in one repository but developers work on only a small portion of the codebase.
There is an important distinction between sparse checkout and partial clone. Sparse checkout controls which files appear in the working directory. Partial clone controls which Git objects are downloaded from the remote. Using them together can reduce both local working-tree size and initial object transfer.
Run Git Maintenance for Local Repository Health
Git stores objects in a way that supports efficient versioning and retrieval. As a repository changes, objects can accumulate in loose-object storage and pack files.
Git maintenance performs background or scheduled optimization tasks that help improve repository performance.
For a manual maintenance run, use:
git maintenance run
Git also supports registering a repository for scheduled maintenance:
git maintenance start
The available maintenance tasks depend on the Git version and configuration. They can include loose-object cleanup, incremental repacking, commit-graph updates, and other optimizations.
Another useful command is:
git gc
Git garbage collection performs repository housekeeping, including packing loose objects and pruning objects that are no longer needed and have passed the relevant expiration conditions.
Do not run aggressive garbage collection automatically as a routine performance fix. Aggressive operations can take significantly longer and may consume additional resources. Start with normal maintenance and measure the results.
Also remember that local Git maintenance optimizes the local clone. It does not directly control the storage or maintenance performed by Azure Repos on its servers.
Apply the Right Settings in Azure Repos Pipelines
A large repository can affect build agents even when developers have reasonably fast local machines. Every pipeline run may download source code, check out branches, and create a fresh working directory.
For build pipelines that need only the latest version of the application, shallow checkout is a practical starting point:
steps:
- checkout: self
clean: true
fetchDepth: 1
The clean option helps ensure that the agent starts with a clean working directory. It does not reduce the repository's underlying history or replace a shallow fetch.
For a pipeline that needs full Git history, use a suitable fetch depth instead of blindly limiting it to one commit.
You should also review whether multiple pipeline jobs repeatedly clone the same large repository. Depending on the agent type and pipeline architecture, workspace reuse, caching, and carefully scoped checkout can reduce repeated work.
Do not cache the entire .git directory without understanding the consequences. A stale or incorrectly restored Git directory can cause reference and object inconsistencies. Prefer supported pipeline caching strategies and validate the checkout behavior.
For Azure Repos, the Health and usage panel can help identify repository size, object counts, and reference counts. Microsoft's repository performance guidance also recommends keeping large, frequently updated binaries out of regular Git storage and using Git LFS or Azure Artifacts where appropriate.
Measure the Improvement Instead of Guessing
After making changes, compare the same Git operations under similar conditions.
For example, record the time required for:
git status
git fetch
git checkout main
git count-objects -vH
Run the tests before and after the changes using the same machine, repository state, and network conditions.
For a simple timing check in PowerShell:
Measure-Command {
git status
}
You can use the same approach for other Git commands:
Measure-Command {
git fetch
}
Keep in mind that the first fetch after changing configuration may behave differently from later fetches because of caching and object availability.
If the repository remains slow, investigate the specific operation again. A slow git status may require file system monitoring or a cleaner working directory, while a slow fetch may be related to remote references or transferred objects. A slow branch switch may be caused by a large working tree or extensive file changes.
Conclusion
Slow Git operations in large .NET repositories usually have identifiable causes. A large working directory can affect git status, a large history can slow down cloning and fetching, and a monorepo with many applications can make developers download and maintain files they rarely use.
Start by measuring the slow operation and inspecting repository health. Then apply the appropriate improvement: file system monitoring for status checks, sparse checkout for focused development, partial clone for reducing downloaded objects, shallow clones for suitable CI workflows, and Git maintenance for local repository optimization.
For Azure Repos pipelines, review checkout depth and avoid transferring unnecessary history on every build. For development teams, keep generated files and large binaries out of ordinary Git storage whenever possible.
The objective is not to apply every Git optimization. It is to make the repository efficient for the actual development workflow while preserving the history and files that the team genuinely needs.
Join the conversation! Your thoughts help the community grow.