当Git与Visual Studio解决方案(.sln)和项目一起使用时,我应该在.gitignore中包含哪些文件?


当前回答

我明白这是一个老问题,还在分享一个信息。在Visual Studio 2017中,只需右键单击解决方案文件,并选择将解决方案添加到源代码控制

这将向源文件夹中添加两个文件。

.gitattributes .gitignore

这是最简单的方法。

其他回答

对于那些对微软认为应该包含在gitignore中感兴趣的人,这里是Visual Studio 2013 RTM在创建一个新的Git-Repository时自动生成的默认gitignore:

## Ignore Visual Studio temporary files, build results, and
## files generated by popular Visual Studio add-ons.

# User-specific files
*.suo
*.user
*.sln.docstates

# Build results

[Dd]ebug/
[Rr]elease/
x64/
build/
[Bb]in/
[Oo]bj/

# Enable "build/" folder in the NuGet Packages folder since NuGet packages use it for MSBuild targets
!packages/*/build/

# MSTest test Results
[Tt]est[Rr]esult*/
[Bb]uild[Ll]og.*

*_i.c
*_p.c
*.ilk
*.meta
*.obj
*.pch
*.pdb
*.pgc
*.pgd
*.rsp
*.sbr
*.tlb
*.tli
*.tlh
*.tmp
*.tmp_proj
*.log
*.vspscc
*.vssscc
.builds
*.pidb
*.log
*.scc

# Visual C++ cache files
ipch/
*.aps
*.ncb
*.opensdf
*.sdf
*.cachefile

# Visual Studio profiler
*.psess
*.vsp
*.vspx

# Guidance Automation Toolkit
*.gpState

# ReSharper is a .NET coding add-in
_ReSharper*/
*.[Rr]e[Ss]harper

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
*.ncrunch*
.*crunch*.local.xml

# Installshield output folder
[Ee]xpress/

# DocProject is a documentation generator add-in
DocProject/buildhelp/
DocProject/Help/*.HxT
DocProject/Help/*.HxC
DocProject/Help/*.hhc
DocProject/Help/*.hhk
DocProject/Help/*.hhp
DocProject/Help/Html2
DocProject/Help/html

# Click-Once directory
publish/

# Publish Web Output
*.Publish.xml

# NuGet Packages Directory
## TODO: If you have NuGet Package Restore enabled, uncomment the next line
#packages/

# Windows Azure Build Output
csx
*.build.csdef

# Windows Store app package directory
AppPackages/

# Others
sql/
*.Cache
ClientBin/
[Ss]tyle[Cc]op.*
~$*
*~
*.dbmdl
*.[Pp]ublish.xml
*.pfx
*.publishsettings

# RIA/Silverlight projects
Generated_Code/

# Backup & report files from converting an old project file to a newer
# Visual Studio version. Backup files are not needed, because we have git ;-)
_UpgradeReport_Files/
Backup*/
UpgradeLog*.XML
UpgradeLog*.htm

# SQL Server files
App_Data/*.mdf
App_Data/*.ldf


#LightSwitch generated files
GeneratedArtifacts/
_Pvt_Extensions/
ModelManifest.xml

# =========================
# Windows detritus
# =========================

# Windows image file caches
Thumbs.db
ehthumbs.db

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Mac desktop service store files
.DS_Store

参见:在MSDN上添加默认的.gitignore文件

参见官方GitHub的“有用的。gitignore模板集合”。

Visual Studio的.gitignore可以在这里找到: https://github.com/github/gitignore/blob/main/VisualStudio.gitignore

我更喜欢根据需要排除一些事情。您不希望在名称中以字符串“bin”或“obj”排除所有内容。至少要确保跟在后面有斜杠。

以下是我在VS2010项目中开始的内容:

bin/
obj/
*.suo
*.user

只是因为我使用ReSharper,还有这个:

_ReSharper*

我在c#项目中使用下面的.gitignore。在需要时添加其他模式。

[Oo]bj
[Bb]in
*.user
*.suo
*.[Cc]ache
*.bak
*.ncb
*.log 
*.DS_Store
[Tt]humbs.db 
_ReSharper.*
*.resharper
Ankh.NoLoad

在Visual Studio中有一个快捷方式,因为它在2015年及以上版本支持Git开箱即用。对于新的解决方案(或一些没有.git文件夹的解决方案),使用解决方案资源管理器中的源代码控制功能:

右键单击解决方案并选择将解决方案添加到源代码控制…弹出菜单中的项目。

它会自动初始化.git存储库,将.gitignore和必要的东西添加到您的解决方案,甚至是.gitattributes文件(行结束符等)。

文本将出现在VS控制台:

A new Git repository has been created for you in C:\<path to your solution>
Commit ______ created locally in repository.

完成了!