A Git repository should contain the files developers need to build, test, maintain, and understand an application. However, many repositories gradually become filled with files that are not part of the actual source code. Build outputs, test reports, temporary files, logs, generated documentation, and deployment packages often find their way into Git.
At first, these files may not appear to cause any problems. A developer commits a build folder to share an output, another adds test results for debugging, and someone else commits a deployment package to make it available to the team. Over time, these small decisions can make the repository significantly larger.
The problem is not limited to disk space. Git maintains historical versions of committed files, so generated content can continue consuming storage even after developers delete it from the current project. Large repositories also take longer to clone, fetch, branch, and check out.
For .NET applications, this problem is particularly common because the development process generates many files automatically. Visual Studio, MSBuild, NuGet, test frameworks, and publishing tools all create output that usually does not belong in source control.
This article explains how to identify generated files, configure Git to ignore them, remove accidentally tracked files, and organize build and test outputs so that repository growth remains manageable.
Understand Which Files Should Be in Git
The first step is to distinguish between project files and generated files.
Project files represent the information developers maintain. These include C# source files, project files, solution files, configuration templates, database scripts, and application documentation.
Generated files are created automatically during development, testing, or deployment. They can usually be recreated from the source code and build configuration.
For example, when a developer builds an ASP.NET Core application, MSBuild creates output directories containing compiled assemblies and supporting files. A typical project might look like this:
MyApplication/
│
├── Controllers/
├── Models/
├── Services/
├── Properties/
├── appsettings.json
├── Program.cs
├── MyApplication.csproj
│
├── bin/
│ └── Debug/
│ └── net10.0/
│ ├── MyApplication.dll
│ └── MyApplication.deps.json
│
└── obj/
└── Debug/
└── net10.0/
├── MyApplication.AssemblyInfo.cs
└── project.assets.json
The bin directory contains compiled output, while obj contains intermediate build files. Both are recreated during the build process.
Committing these directories provides little value because other developers can generate them by restoring dependencies and building the project.
A useful rule is to commit the information required to reproduce an output, rather than committing the output itself.
For example, a project file and package references belong in Git. The compiled DLL files and restored package contents generally do not.
Configure .gitignore for .NET Projects
Git provides a .gitignore file to define patterns for files and directories that should remain untracked.
For a .NET application, the repository should include a .gitignore file at the root. This ensures that all developers receive the same exclusion rules when they clone the project.
A practical starting point is:
# Build output
[Bb]in/
[Oo]bj/
# Visual Studio
.vs/
*.user
*.suo
*.userosscache
*.sln.docstates
# Test results and coverage
TestResults/
coverage/
*.trx
*.coverage
*.coveragexml
# Logs
*.log
logs/
# Temporary files
*.tmp
*.temp
*.bak
# Publish output
publish/
artifacts/
# NuGet packages
*.nupkg
*.snupkg
# Operating system files
.DS_Store
Thumbs.db
The patterns use directory names and file extensions to identify generated content. For example, [Bb]in/ ignores directories named bin or Bin, regardless of capitalization.
The TestResults/ rule prevents test result folders from being added when developers run automated tests locally.
The coverage/ rule excludes generated code coverage reports. These reports can be produced again by running the test suite with the appropriate coverage tool.
The .vs/ directory contains Visual Studio-specific workspace data. It is generally local to each developer's environment and should not be shared through Git.
This configuration is a starting point, not a universal rule. Some projects intentionally track files that match these patterns. For example, a repository may contain a directory named artifacts with manually maintained source assets. In that case, adjust the ignore rules rather than excluding the entire directory blindly.
Understand Why .gitignore Does Not Remove Tracked Files
One common Git misunderstanding is that adding a directory to .gitignore automatically removes it from the repository.
It does not.
Git ignore rules apply to untracked files. If a developer has already committed the bin directory, adding bin/ to .gitignore will not stop Git from tracking changes to those existing files.
Consider this situation:
git status
The output may still show changes under the bin directory even after the ignore rule has been added.
To stop tracking the directory while keeping the local files, use:
git rm -r --cached bin obj
The --cached option removes the files from Git's index without deleting them from the working directory.
Then commit the changes:
git add .gitignore
git commit -m "Stop tracking generated build files"
After this commit, Git will no longer track those directories in subsequent changes.
However, their previous versions remain in repository history. This distinction matters because removing generated files from the current version of a project does not automatically reduce the size of historical Git objects.
If those files have already been committed repeatedly and are significantly increasing repository size, a separate history cleanup may be required.
Keep Build and Test Output Outside Source Control
A healthy repository should allow developers and CI pipelines to generate build and test outputs without committing them.
For a .NET project, developers can use the standard CLI commands:
dotnet restore
dotnet build --configuration Release
dotnet test --configuration Release
The build and test processes generate their own output. There is no need to store those generated files in the repository.
When a team needs test results for reporting or troubleshooting, it should publish them through the CI pipeline rather than committing them to Git.
For example, an Azure Pipelines job can publish test results using the PublishTestResults task:
steps:
- script: dotnet test --configuration Release --logger "trx"
displayName: Run tests
- task: PublishTestResults@2
displayName: Publish test results
condition: succeededOrFailed()
inputs:
testResultsFormat: VSTest
testResultsFiles: '**/*.trx'
searchFolder: '$(System.DefaultWorkingDirectory)'
failTaskOnFailedTests: true
This allows the team to access test reports from the pipeline run without storing them in the source repository.
The same principle applies to build packages. If a team needs to share compiled libraries, NuGet packages, or other reusable dependencies, a package feed such as Azure Artifacts is generally more suitable than committing the files to Git.
Keeping source code and generated outputs separate makes the repository easier to maintain and allows build pipelines to manage artifacts independently.
Handle Generated Files That Must Be Shared
Not every generated file should automatically be excluded from Git.
Some applications rely on generated files that are required to build or run the project. The important question is whether the file is reproducible and whether the team needs to review or maintain it.
For example, generated API clients may be produced from an OpenAPI specification. A team might choose to commit the generated client code so that downstream projects can consume it without running a generation step.
Similarly, a database migration file generated by an Entity Framework Core command is part of the application's development history and should normally be committed.
Consider:
dotnet ef migrations add AddCustomerTable
This command creates migration source files that describe database schema changes. These files are not temporary build output. They are part of the application's source-controlled database evolution.
The distinction is important:
File type | Recommended approach |
|---|
bin and obj directories
| Ignore |
Temporary logs and local diagnostics | Ignore |
Test result reports | Publish through CI |
Compiled application packages | Store as build artifacts |
NuGet dependencies | Restore through package management |
EF Core migration files | Commit |
OpenAPI specifications maintained by the team | Commit |
Generated client code | Decide based on the build and distribution workflow |
A generated file can still be an important project artifact. The goal is not to ignore everything generated, but to avoid storing disposable outputs that Git does not need to version.
Avoid Reordering Large Generated Files
Another source of repository growth is generated text that changes unnecessarily between commits.
Suppose an application generates a JSON file containing thousands of records. A small update adds one record, but the generator changes the ordering of all existing records.
The resulting Git diff may show thousands of modified lines even though only one record was added.
This makes history larger and code reviews more difficult.
A better approach is to generate the content in a consistent order.
For example, in C#:
var orderedRecords = records
.OrderBy(record => record.Name)
.ThenBy(record => record.Id)
.ToList();
The ordering makes the output more predictable. When a new record is added, the resulting diff is more likely to contain only the relevant change.
This is particularly useful for generated JSON, XML, configuration files, API metadata, and other structured text files.
Stable output also makes automated reviews and merge operations easier because unrelated records are less likely to move between commits.
Use Git Hooks and Repository Policies to Prevent Accidental Commits
A .gitignore file is useful, but it cannot prevent every unwanted file from entering a repository. Developers can explicitly force-add ignored files, and some files may not match the configured patterns.
Teams working with large repositories can add additional checks.
A pre-commit hook can inspect staged files and reject files that exceed an agreed size.
For example, a simple Bash hook can check staged file sizes:
#!/bin/sh
MAX_SIZE=10485760
git diff --cached --name-only --diff-filter=ACMR |
while IFS= read -r file
do
if [ -f "$file" ]; then
size=$(wc -c < "$file")
if [ "$size" -gt "$MAX_SIZE" ]; then
echo "File exceeds the allowed size: $file"
exit 1
fi
fi
done
This example sets a 10 MB threshold. Teams should adjust the limit based on their project requirements and use a maintained hook framework or equivalent validation where appropriate.
A local hook alone is not a complete enforcement mechanism because developers can bypass it or may not have installed it. Important restrictions should also be enforced centrally.
Azure Repos provides a Maximum file size repository policy. Administrators can enable it under Project Settings, Repositories, and Repository Policies. When a push contains a new or updated file larger than the configured limit, Azure Repos blocks the push.
This provides a shared safeguard rather than relying only on individual developer configurations.
Monitor Repository Growth Over Time
Repository cleanup should not be a one-time activity. A project that is healthy today can become large again if generated files, binaries, and temporary outputs are repeatedly committed.
Developers can inspect the local repository size using:
git count-objects -vH
The size-pack field provides the size of packed Git objects. Comparing this value periodically helps identify unusual growth.
For a deeper analysis, tools such as git-sizer and git survey can identify large objects, directories, and other repository characteristics.
In Azure Repos, the Health and usage panel also provides information about repository size, object counts, and references. These measurements help teams identify repositories that are approaching recommended limits.
It is useful to include repository health in regular engineering reviews, particularly for projects with many contributors or automated build processes.
A sudden increase in repository size should lead to an investigation of recent commits, rather than waiting until cloning and fetching become noticeably slow.
Conclusion
Generated files can make a Git repository grow much faster than expected, particularly in .NET projects where build tools, test frameworks, and IDEs create large amounts of temporary output.
A well-maintained .gitignore file, clean build and test workflows, and proper artifact storage can prevent much of this growth. Teams should also distinguish between disposable generated output and generated source files that are important to the application, such as database migrations or maintained API clients.
For larger teams, pre-commit checks and Azure Repos file-size policies provide additional protection against accidental commits.
The objective is simple: keep Git focused on source code and the files needed to maintain the project, while using build pipelines and package management to handle generated outputs. This keeps repositories easier to clone, review, maintain, and work with as applications grow.
Join the conversation! Your thoughts help the community grow.