Visual Studio Gitignore: .gitignore for C# and .NET

November 11, 2026 · 9 min read

Visual Studio Gitignore: The .gitignore for C# and .NET Projects

A Visual Studio gitignore tells Git to skip what Visual Studio and MSBuild regenerate on each machine: bin/ and obj/ output, the .vs/ cache and per-user *.user and *.suo files. Create one from Visual Studio's Git menu, with dotnet new gitignore, or from GitHub's template, then untrack anything already committed.

What a Visual Studio Gitignore Keeps Out of Your Repo

Every build and every Visual Studio session writes files that belong to your machine, not to the project. Commit them and each teammate's build overwrites yours, history fills with binaries, and pull requests drown in noise. These are the folders and files a gitignore for Visual Studio exists to hide:

  • bin/ holds compiled output for each configuration and target framework, such as bin/Debug/net8.0/MyApp.dll, plus copied dependencies.
  • obj/ holds intermediate build files, including the project.assets.json that NuGet restore writes.
  • .vs/ is the hidden solution folder Visual Studio has used since 2015. It stores IDE state such as the .suo user options file and local caches.
  • *.user files, like MyApp.csproj.user, keep your personal debug and startup settings.
  • packages/ is where older packages.config projects restore NuGet packages. Projects using PackageReference restore to a global folder instead.
  • TestResults/ collects test run output from Visual Studio's test tools.

All of it comes back after a build, a restore or reopening the solution, so nothing is lost by ignoring it. For the general pattern syntax, the official gitignore documentation is the reference; this post sticks to the lines that matter for C# and .NET. A Unity project needs different rules, which our gitignore for Unity guide covers.

How Do You Add a .gitignore in Visual Studio?

Visual Studio can create the file for you, and there are two paths depending on whether the repo exists yet.

New repo. Open your solution, then choose Git > Create Git Repository. Under Initialize a local Git repository, the .gitignore template option picks the .gitignore Visual Studio adds to the new repo. Choose GitHub, Azure DevOps or a local repo under Other, then select Create and Push.

Existing repo. Choose Git > Settings. In Visual Studio 2022 the Options dialog opens; expand Git Repository Settings > General and select Add next to the ignore file. The button then changes to Edit, which opens the file. In the newer Options pane, the same group lives under Git Repository Config, and the button reads Add /.gitignore. Microsoft documents both layouts on its Git settings page.

Either way, the file lands at the repository root, which is where you want it.

Create a .NET Gitignore with dotnet new gitignore

If you live in a terminal, or use VS Code or Rider, the .NET SDK ships the file as a template. Run this from your solution folder:

dotnet new gitignore

It writes a .gitignore into the current directory. The gitignore short name has been a preinstalled item template since .NET Core 3.0, according to Microsoft's list of default dotnet new templates. Add -o path to write it somewhere else.

If a .gitignore already exists, dotnet new won't silently replace it. You need --force to overwrite existing files, so merge any custom lines first. The related dotnet new editorconfig template is worth running at the same time if you want shared code-style rules.

The dotnet gitignore template tracks the Visual Studio list GitHub maintains, down to its header comments. It then appends Rider's .idea/ folder and macOS and Windows clutter such as .DS_Store and Thumbs.db. That makes it a good fit for mixed teams where not everyone opens the solution in Visual Studio.

The C# Gitignore Lines That Matter Most

GitHub's VisualStudio.gitignore runs to over 400 lines, mostly for add-ins you may never install. These are the lines doing the real work in a typical C# gitignore. The patterns are copied exactly from that file; the comments are shortened:

# User-specific files
*.rsuser
*.suo
*.user
*.env

# Build results
[Dd]ebug/
[Rr]elease/
[Oo]bj/
[Ll]og/
**/[Bb]in/*

# Visual Studio cache and options
.vs/

# Tests and publishing
[Tt]est[Rr]esult*/
*.pubxml
PublishScripts/

# NuGet
*.nupkg
**/[Pp]ackages/*
!**/[Pp]ackages/build/

The bracket pairs are character classes, so [Bb]in/ matches both bin and Bin. Windows treats those as the same folder, but Git on a Linux build server doesn't, so the template covers both spellings.

PatternWhat it ignoresWhere it comes from
**/[Bb]in/*Everything inside any bin folderBuild
[Oo]bj/Intermediate files in every projectBuild and NuGet restore
.vs/Visual Studio's per-machine cacheVisual Studio, on opening the solution
*.suo, *.userYour personal IDE and debug settingsVisual Studio, per user
**/[Pp]ackages/*Restored NuGet packages (old format)NuGet restore
[Tt]est[Rr]esult*/Test run outputRunning tests
*.pubxmlPublish profilesVisual Studio's publish wizard

One detail is easy to miss. GitHub's file ignores **/[Bb]in/*, the contents of bin, rather than the folder itself. That lets you re-include a file with a ! rule, which the template shows in a commented-out line for .refresh files. The SDK template uses the plainer [Bb]in/.

Where Should the .gitignore Go in a Visual Studio Solution?

Put it at the repository root, which is normally the folder holding your .sln file (or .slnx, which dotnet new sln creates by default from .NET 10):

MySolution/
├── .git/
├── .gitignore
├── MySolution.sln
├── src/
│   └── MyApp/
│       ├── MyApp.csproj
│       ├── bin/
│       └── obj/
└── tests/
    └── MyApp.Tests/
        ├── bin/
        └── obj/

Patterns in a .gitignore are relative to the folder that contains it. A pattern with no slash at its start or middle, such as [Oo]bj/, matches at any depth, so one root file covers obj in every project.

A second .gitignore inside src/MyApp/ only affects files below that folder. That's handy for one odd project, but a single root file is easier to keep right. If your .sln sits one level below the repo root, keep the .gitignore at the root anyway; the unanchored patterns still reach every project.

Why Isn't .gitignore Ignoring bin, obj or .vs?

This is the most common complaint, and the cause is nearly always the same. A .gitignore only affects files Git isn't tracking yet. If bin/, obj/ or .vs/ went into an earlier commit, Git keeps tracking every file in them, whatever your rules say.

The fix is to remove them from Git's index while leaving them on disk. Close Visual Studio first, so it isn't rewriting files in .vs/ while you work, then run this from the repo root:

git rm -r --cached .
git add .
git status
git commit -m "Stop tracking build output and .vs"

The --cached flag removes paths from the index only; your working files stay where they are. -r lets it recurse into folders. The git add . step stages everything again, and this time your ignore rules apply. Read the git status output before committing, because any unrelated edits you had are staged too.

To confirm a rule is matching, ask Git directly:

git check-ignore -v .vs src/MyApp/obj

Each matching path comes back with the ignore file, line number and pattern responsible. A path that prints nothing either matches no rule or is still tracked, since check-ignore skips tracked files by default.

One honest limitation: this only cleans up future commits. The old commits still contain those binaries, so the repo's history doesn't shrink. Rewriting history is a separate job that every collaborator has to coordinate on.

A Shorter C# Gitignore from Our Generator

Sometimes 400-plus lines is more than a small project needs. Our .gitignore generator runs in your browser and has a C# / .NET template in its Languages group. It holds 19 patterns, covering [Bb]in/, [Oo]bj/, .vs/, *.user, *.suo, TestResults/, publish/, NuGet package files and appsettings.*.local.json.

There's no separate Visual Studio template. Tick C# / .NET, add Windows or macOS, add the VS Code or JetBrains editor templates if your team uses them, and type extra rules into the custom patterns box. Then copy the result or download it as a .gitignore.

Be clear about the trade-off, though. It's a short curated list, not GitHub's full file, so it skips entries like *.pubxml and the old packages/ folder. We prefer dotnet new gitignore or GitHub's full file for a team repo, and the short template for a small personal project you understand line by line.

Keep Secrets and Publish Profiles Out of Git

Ignoring build output keeps the repo tidy. Missing a secret is the mistake that actually costs you.

  • User secrets. .NET's Secret Manager stores development secrets in your user profile, under %APPDATA%\Microsoft\UserSecrets on Windows. They're outside the project tree, so there's nothing to ignore. Microsoft's app secrets guide for ASP.NET Core covers setup.
  • Environment files. GitHub's template ignores *.env, and the SDK template ignores .env, so a local .env stays out either way.
  • Publish profiles. GitHub's template ignores *.pubxml because a web deploy profile can hold a connection string with a password in plain text. Remove that line only if your profiles are clean.

Neither template ignores appsettings.Development.json or launchSettings.json, and both are normally committed. Keep real credentials out of them, and remember that ignoring a file never removes a secret already pushed. Rotate it.

A good Visual Studio gitignore is easy to read once you know why each block is there. Build output, the .vs/ cache, user settings and secrets stay out, while source and project files go in. Generate it with Visual Studio or dotnet new gitignore, or build a lighter one with the .gitignore generator. Then document the solution in a README or a docs folder, written in our free Markdown editor.