C# .gitignore template
The C# template ignores build output, NuGet packages and Visual Studio user state, and writes its directory names in character classes so the rules work on case sensitive filesystems.
At a glance
- Category
- Language
- Patterns in this template
- 23 rules, plus 3 comment lines
- Directories ignored
- 13 of the 23 rules end in a slash, so they match directories only
- Also needs an OS template
- Yes, though the Visual Studio rules already cover a lot of Windows specific tooling noise.
- Typical companions
- .NET if you are on the modern SDK, plus an OS template.
In detail
The first thing you notice is the bracket notation: [Bb]in, [Oo]bj, [Dd]ebug, [Rr]elease. Git patterns are case sensitive, but Windows filesystems are not, so a project created by one tool may produce bin while another produces Bin, and both are the same directory as far as Windows is concerned. The character class matches either, which is why this template is full of them rather than plain lowercase names.
bin and obj are the two output directories every project produces. obj holds intermediate state including the restored package graph and generated assembly info; bin holds the compiled assemblies and everything copied alongside them for a run. Both are rebuilt from the project file, and obj in particular embeds absolute paths from the restore step.
The NuGet block covers *.nupkg and *.snupkg, which are the package and symbol package you produce when packing a library, plus the packages directory used by the older package management style. Modern projects restore into a global cache outside the repository, so that directory may never appear, but the rule is cheap insurance for a solution that has not been migrated.
The Visual Studio rules are about per developer state rather than build output. .vs is the editor's own working directory. *.suo holds solution user options such as which files you had open. *.user and *.userosscache record project settings scoped to one person, and *.sln.docstates records document layout. Committing any of them means every teammate fights over your window arrangement.
[Tt]est[Rr]esult* covers the output directory that test runs produce. What the template does not cover is a local application settings file holding a connection string, which is the usual place a C# project leaks a database password. The convention that avoids it is keeping development secrets in the user secrets store, which lives in your profile directory rather than in the repository, so there is nothing to ignore in the first place.
Examples
# Build results
[Dd]ebug/
[Rr]elease/
x64/
x86/
[Aa][Rr][Mm]/
[Aa][Rr][Mm]64/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ll]ogs/
# NuGet
*.nupkg
*.snupkg
**/[Pp]ackages/*
*.nuget.props
*.nuget.targets
# Visual Studio
.vs/
*.suo
*.user
*.userosscache
*.sln.docstates
*.userprefs
[Tt]est[Rr]esult*/26 lines exactly as the tool writes them, before any common additions you tick.
Things that catch people out
- The bracket classes only cover the first letter. A directory named BIN in full uppercase is not matched by [Bb]in, which occasionally bites projects generated by older tooling.
- **/[Pp]ackages/* ignores the contents of a packages directory but not the directory itself, so an empty folder can still show up in a clone if anything else inside it is tracked.
- *.user is broad enough to catch unrelated files in a mixed repository. If you keep a data file named settings.user, anchor the rule to the project directories instead.
Try it: .gitignore Generator
Pick one or more templates, tick the common additions you want, and copy or download the combined .gitignore.
.gitignore Generator
LOCAL · Browser-OnlyGenerate comprehensive .gitignore files by combining templates for languages, frameworks, IDEs, and operating systems
Language
Framework
IDE
OS
Build Tool
Infrastructure
Common Additions
Preview
Select templates or common additions to generate a .gitignore file.
Related .gitignore templates
All .gitignore templates →.NET .gitignore template
The .NET template covers bin, obj and NuGet output, and ignores launchSettings.json, which holds the launch profiles and environment variables for local runs.
Unity .gitignore template
The Unity template ignores the import cache, temporary build state and the IDE project files the editor regenerates, using bracket classes for case safety.
Windows .gitignore template
What the Windows template covers: the thumbnail caches Explorer writes into image folders, the folder view settings file, and installer package extensions.
Ruby .gitignore template
The Ruby template anchors most rules to the repository root with a leading slash, and ignores Gemfile.lock, which is right for a gem and wrong for an app.
PHP .gitignore template
The PHP template ignores the vendor directory and the .env file, and also ignores composer.lock, which most application projects should commit instead.
Swift .gitignore template
What the Swift template ignores: the package build folder, Xcode user state, derived data and symbol archives, plus the Package.resolved decision it makes.
Tools for this
- .gitignore Generator Generate .gitignore files for any project - 50+ templates for languages, frameworks, IDEs, and OS