Gitignore不会忽略.vs文件夹。

107
我们已经尝试了各种在.gitignore中显式和通配符条目,但是Visual Studio中隐藏的.vs文件夹中的项目仍然被提交。
由于这些是开发人员的个别设置,它们显然总是不同,并显示在git diff中。
我该如何解决它以忽略存储库中顶级.vs文件夹中的所有内容?

1
你能展示一下你项目中的.gitignore文件吗? - nwinkler
不要使用 git add ...? - bobobobo
11个回答

144

按照以下步骤操作,问题将会得到解决。

步骤1:将以下内容添加到文件.gitignore中。

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

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

# User-specific files (MonoDevelop/Xamarin Studio)
*.userprefs

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
bld/
[Bb]in/
[Oo]bj/
[Ll]og/

# Visual Studio 2015 cache/options directory
.vs/
# Uncomment if you have tasks that create the project's static files in wwwroot
#wwwroot/

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

# NUNIT
*.VisualState.xml
TestResult.xml

# Build Results of an ATL Project
[Dd]ebugPS/
[Rr]eleasePS/
dlldata.c

# DNX
project.lock.json
artifacts/

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

# Chutzpah Test files
_Chutzpah*

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

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

# TFS 2012 Local Workspace
$tf/

# Guidance Automation Toolkit
*.gpState

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

# JustCode is a .NET coding add-in
.JustCode

# TeamCity is a build add-in
_TeamCity*

# DotCover is a Code Coverage Tool
*.dotCover

# NCrunch
_NCrunch_*
.*crunch*.local.xml
nCrunchTemp_*

# MightyMoose
*.mm.*
AutoTest.Net/

# Web workbench (sass)
.sass-cache/

# 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
*.[Pp]ublish.xml
*.azurePubxml
# TODO: Comment the next line if you want to checkin your web deploy settings 
# but database connection strings (with potential passwords) will be unencrypted
*.pubxml
*.publishproj

# NuGet Packages
*.nupkg
# The packages folder can be ignored because of Package Restore
**/packages/*
# except build/, which is used as an MSBuild target.
!**/packages/build/
# Uncomment if necessary however generally it will be regenerated when needed
#!**/packages/repositories.config
# NuGet v3's project.json files produces more ignoreable files
*.nuget.props
*.nuget.targets

# Microsoft Azure Build Output
csx/
*.build.csdef

# Microsoft Azure Emulator
ecf/
rcf/

# Windows Store app package directories and files
AppPackages/
BundleArtifacts/
Package.StoreAssociation.xml
_pkginfo.txt

# Visual Studio cache files
# files ending in .cache can be ignored
*.[Cc]ache
# but keep track of directories ending in .cache
!*.[Cc]ache/

# Others
ClientBin/
~$*
*~
*.dbmdl
*.dbproj.schemaview
*.pfx
*.publishsettings
node_modules/
orleans.codegen.cs

# Since there are multiple workflows, uncomment next line to ignore bower_components 
# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622)
#bower_components/

# 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
*.mdf
*.ldf

# Business Intelligence projects
*.rdl.data
*.bim.layout
*.bim_*.settings

# Microsoft Fakes
FakesAssemblies/

# GhostDoc plugin setting file
*.GhostDoc.xml

# Node.js Tools for Visual Studio
.ntvs_analysis.dat

# Visual Studio 6 build log
*.plg

# Visual Studio 6 workspace options file
*.opt

# Visual Studio LightSwitch build output
**/*.HTMLClient/GeneratedArtifacts
**/*.DesktopClient/GeneratedArtifacts
**/*.DesktopClient/ModelManifest.xml
**/*.Server/GeneratedArtifacts
**/*.Server/ModelManifest.xml
_Pvt_Extensions

# Paket dependency manager
.paket/paket.exe
paket-files/

# FAKE - F# Make
.fake/

# JetBrains Rider
.idea/
*.sln.iml

步骤二:确保上述操作生效

如果问题仍然存在,那是因为.gitignore文件中的设置只能忽略最初未被跟踪的文件。如果某些文件已经包含在版本控制系统中,则修改.gitignore是无效的。 要完全解决此问题,您需要在存储库根文件夹中打开Git Bash并运行以下命令。

$ git rm -r --cached .
$ git add .
$ git commit -m 'Update .gitignore'

当然,您也可以使用 Visual Studio 的包管理器控制台来完成第2步中的操作。 使用 PM 控制台 这样问题就完全解决了。


7
我觉得你不想把 . 作为 git rm 的参数,因为那样会删除所有东西!我猜你只想删除现在要排除的部分,比如 .vs 文件夹。 - ChrisW

37
如果它们出现在 git diff 中,那么这些文件已经被跟踪了,而 .gitignore 只影响未跟踪的文件。你需要使用 git rm --cached 将这些文件从源代码控制中移除,然后 .gitignore 才会生效。
请注意,当您执行此操作时,其他开发人员在进行下一次 git pull 时,它们的本地文件将被删除。所以在这样做之前,他们可能需要备份这些文件。

32

在我们的情况下,当我初始化仓库时,.vs目录已经被添加到源代码控制中。因此,在.gitignore文件中的这一行:

.vs/
/.vs

直到我删除了目录并提交更改后 (类似于TPoschel上面所说的,但不同之处在于检查已删除的目录才是解决问题的关键,因为我已经检查过.gitignore文件了) 才做了任何事情。


2
这是正确的方法。@Bravo Yeung给出的答案从我的本地存储库中删除了所有内容。我需要重新克隆整个项目。因为这个我差点心脏病发作。:P - Sandeep Ingale

5
这是我的操作方法: 打开你的git ignore文件,将以下内容添加到列表中:

.vs/


注意:一些人在使用此答案时遇到了问题。如果有人有时间检查一下,请添加评论。:-) - Nick Westgate
1
这个可行,已经检查过了。如果你不确定,Github桌面版可以很好地预览将要提交到仓库的内容。@NickWestgate - bobobobo
2
请注意,此解决方案忽略任何名为.vs的文件夹(您不需要尾随斜杠),即使在子目录中出现一个也是如此。@ChaimEliyah的答案仅忽略根目录中的.vs文件夹。 - bobobobo
@bobobobo 感谢您的查看。我使用了Chaim Eliyah的答案(并对其进行了评论)。 - Nick Westgate

5

在放置有效的.gitignore文件后推送后,解决步骤

这个答案与上面的答案类似,但是当一个有效的.gitignore文件被放置时,如果项目已经被检入分支并且它们出现在源存储库中,则详细说明了实际步骤。

  1. 如果已经检入.vs目录,请验证当前分支中没有从该目录中暂存的内容。如果有,请取消暂存。
  2. 进入具有.vs的目录级别。
  3. 运行此命令:git rm --cached -r .vs
  4. 此时,您应该看到将要删除的.vs目录下的文件。
  5. 暂存并创建提交。
  6. 将提交推送到源。

3
我发现有些帖子讨论需要删除或更改提交历史,以便将 .gitignore 文件放入分支,并且更重要的是让 Git 尊重其内容,不包含应被跟踪的匹配项。
当使用 Visual Studio 2022 时,只需在 Git-Changes 菜单中右键单击 .vs 文件夹,然后选择选项 Ignore these local items 即可。

enter image description here

之后,.gitignore 文件被创建并添加到下一次提交中,您选择的任何项目现在都将从随后的提交中删除。

2
我遇到了这个问题,我发现解决“git已经跟踪了文件”问题的一个简单方法。
  1. 备份PC上git文件夹中的所有相关文件到另一个位置(通常是c:/user/source/repos,如果你没有指定其他地方)。

  2. 删除git文件夹中的所有相关文件。

  3. 打开Visual Studio并将此删除推送到服务器。

  4. 粘贴回所有文件。

  5. 将其推送到服务器。

这应该是一种简单的方法,可以摆脱所有那些临时文件和你在.gitignore中为在线git文件夹指定的任何其他内容,它已经被跟踪。

1

在向仓库提交任何内容之前,我遇到了这个问题。我错误地认为只要在目录中有.gitignore文件,VS就不会将文件识别为“更改”。您必须先提交.gitignore文件,然后git才会开始忽略其中指定的文件。


1

如果在项目创建过程中错过了.gitignore文件。

解决方法:

  1. 创建一个新的类似项目

  2. 复制.gitignore文件和.gitattributes文件,然后粘贴到旧解决方案中。

  3. 清除git缓存。

    git rm -r --cached .

    git add .

    git commit -m 'Remove ignored files'


0
通常我会在干净的仓库中添加 .vs,但我可以确认 git 遵循以下规则:
.vs
在 .gitignore 中。
正如其他人所说,先在本地删除,然后再提交/推送。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接