github/gitignore · Dotnet.gitignore

.NET .gitignore 模板详解

说明 Dotnet.gitignore 忽略 bin 与 obj、Debug 与 Release、NuGet 包、MSBuild 二进制日志、测试结果和 .env 的原因,以及与 VisualStudio 模板的区别。

在生成器中打开 与 VisualStudio, VisualStudioCode, JetBrains, Windows 一起打开

Dotnet.gitignore 是面向 .NET SDK 风格项目(用 dotnet new 创建的 C#、F# 项目)的简洁模板。正如模板开头注释所说,如果使用 Visual Studio,最好同时使用包含更多 IDE 相关规则的 VisualStudio 模板。

如果在 VS Code、Rider 或命令行中开发,仅用此模板就能拦截大部分构建结果和工具副产物。

逐条规则详解

模式忽略什么,为什么忽略
[Bb]in/[Oo]bj/[Dd]ebug/[Rr]elease/x64/x86/bld/构建输出dotnet build 生成的程序集和中间文件(obj/project.assets.json 等)。用方括号同时匹配 Binbin 这样的大小写差异。
artifacts/project.lock.jsonproject.fragment.lock.json.NET 产物artifacts/ 是从 .NET 8 起可用的统一输出布局目录,project.lock.json 是旧 project.json 时代的还原结果。
*.nupkg*.snupkgNuGet 包dotnet pack 生成的包和符号包,是发布到 NuGet 源的产物。
*.binlog[Tt]est[Rr]esult*/[Bb]uild[Ll]og.*TestResult.xmlnunit-*.xmlCodeCoverage/日志与测试结果MSBuild 二进制日志(-bl 选项)以及测试与覆盖率结果。二进制日志中可能包含环境变量等敏感值,共享时要注意。
.env~$**~环境与临时文件环境变量文件、打开 Office 文档时生成的锁文件(~$文档.docx)以及编辑器备份文件。

实践中的注意事项

原始模板

Dotnet.gitignore规则 30 行
## A streamlined .gitignore for modern .NET projects## including temporary files, build results, and## files generated by popular .NET tools. If you are## developing with Visual Studio, the VS .gitignore## https://github.com/github/gitignore/blob/main/VisualStudio.gitignore## has more thorough IDE-specific entries.#### Get latest from https://github.com/github/gitignore/blob/main/Dotnet.gitignore# Build results[Dd]ebug/[Dd]ebugPublic/[Rr]elease/[Rr]eleases/x64/x86/[Ww][Ii][Nn]32/[Aa][Rr][Mm]/[Aa][Rr][Mm]64/bld/[Bb]in/[Oo]bj/[Ll]og/[Ll]ogs/# .NET Coreproject.lock.jsonproject.fragment.lock.jsonartifacts/# ASP.NET ScaffoldingScaffoldingReadMe.txt# NuGet Packages*.nupkg# NuGet Symbol Packages*.snupkg# dotenv environment variables file.env# Others~$**~CodeCoverage/# MSBuild Binary and Structured Log*.binlog# MSTest test Results[Tt]est[Rr]esult*/[Bb]uild[Ll]og.*# NUnit*.VisualState.xmlTestResult.xmlnunit-*.xml

模板来源: github/gitignore/Dotnet.gitignore @356fd7b (2026-09-11) · CC0-1.0

常搭配使用的模板

其他模板详解