C# .gitignore 和 Visual Studio 项目:忽略 bin/Debug 目录但不忽略 bin/Release 目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11123248/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 16:36:05  来源:igfitidea点击:

.gitignore and Visual Studio project: Ignore bin/Debug directory but not bin/Release directory

c#.netvisual-studiogitgitignore

提问by Dan Stevens

I have a C# Visual Studio project in a git repository. I want to ignore the contents bin/Debugdirectory but not the contents of the bin/Release' directory. I've added bin/Debugto my .gitignorefile, but it doesn't seem to work - it is including the entire contents of the bindirectory. What would the correct entry be to do this?

我在 git 存储库中有一个 C# Visual Studio 项目。我想忽略内容bin/Debug目录而不是bin/Release' 目录的内容。我已经添加bin/Debug到我的.gitignore文件中,但它似乎不起作用 - 它包括bin目录的全部内容。执行此操作的正确条目是什么?

采纳答案by Dan Stevens

I fixed this by replacing bin/Debugwith Debug.

我通过替换bin/DebugDebug.

This would also have the affect of ignoring the obj/Debugdirectory, however I want to ignore the entire contents of the objdirectory, so I have also added objto .gitignore.

这也会有忽略obj/Debug目录的影响,但是我想忽略obj目录的全部内容,所以我也添加obj.gitignore.

回答by Noah

Here's what we've been using lately, it removes all resharper generated stuff and some other important things. Note that we don't commit our release directory, so you shouldn't include Release/in your .gitignore, but to answer your question, you should include Debug/.

这是我们最近一直在使用的东西,它删除了所有重新锐化生成的东西和一些其他重要的东西。请注意,我们不承诺我们的发布目录,所以你不应该包括Release/在你的.gitignore,而是要回答你的问题,你应该包括Debug/

/build/
*.suo
*.user
_ReSharper.*/
*.sdf
bin/
obj/
Debug/
Release/
*.opensdf
*.tlog
*.log
TestResult.xml
*.VisualState.xml
Version.cs
Version.h
Version.cpp

UPDATE

更新

Here's a pretty comprehensive example from github:

这是来自 github 的一个非常全面的示例:

回答by Gary.S

This typically happens because the .gitignore was added after the files were committed. The .gitignore tells git to ignore untracked files that match, once stuff is committed the ignore will no longer work. One way to fix it is to remove the bin/debug folder (manually through explorer/powershell/bash), then commit the removals. Once that is done the ignores should work as you expect.

这通常是因为在提交文件后添加了 .gitignore。.gitignore 告诉 git 忽略匹配的未跟踪文件,一旦提交,忽略将不再起作用。修复它的一种方法是删除 bin/debug 文件夹(通过 explorer/powershell/bash 手动),然后提交删除。完成后,忽略应按您的预期工作。

  1. Remove files/folder
  2. git add -A
  3. git commit
  1. 删除文件/文件夹
  2. git add -A
  3. git commit

回答by orourkedd

You shouldn't have to delete anything. After you added the .gitignore file, run this command to clear the cache, then stage and commit again:

你不应该删除任何东西。添加 .gitignore 文件后,运行此命令以清除缓存,然后暂存并再次提交:

git rm -r . --cached

回答by Evgeny Shmanev

I know it is an old question, but I've decided to share my approach which excludes exactly bin/Debug, bin/Release etc.

我知道这是一个老问题,但我决定分享我的方法,它完全排除了 bin/Debug、bin/Release 等。

*/**/bin/Debug
*/**/bin/Release
*/**/obj/Debug
*/**/obj/Release

回答by Benjamin RAIBAUD

Running the following command worked for me (thanks to "orourkedd"):

运行以下命令对我有用(感谢“orourkedd”):

git rm -r . --cached

I manually added the .gitignorefile but it wasn't taken into consideration until I ran this command.

我手动添加了该.gitignore文件,但直到我运行此命令时才考虑它。

I then had to commit again and all good to go now. /binand /objfolders are properly excluded now.

然后我不得不再次承诺,现在一切都很好。/bin/obj文件夹现在被正确排除。

回答by Willy David Jr

This works for me:

这对我有用:

*.exe
*.pdb
*.manifest
*.cache

If you are using GitHub Desktop as I am using it, you can just right click the file you want to exclude or right click it and exclude by extension:

如果您像我一样使用 GitHub Desktop,只需右键单击要排除的文件或右键单击它并按扩展名排除:

enter image description here

在此处输入图片说明

This will automatically generate a .gitignorefile for you with the code like above. You can download GitHub Desktop here: GitHub Desktop for Windows 64-bit.

这将.gitignore使用上述代码自动为您生成一个文件。您可以在此处下载 GitHub Desktop:GitHub Desktop for Windows 64-bit

回答by Farax

This may be slightly off topic, but whenever starting to create a new project, I usually use GitIgnore.IOfor creating my initial gitignore file and then I tweek it from there according to my needs.

这可能有点偏离主题,但每当开始创建一个新项目时,我通常使用GitIgnore.IO来创建我的初始 gitignore 文件,然后我根据我的需要从那里开始 tweek 。

回答by Kiriti

Right click on those file names you want to ignore from "Git Desktop", then add them to .gitignorefile. Here gitignore file will be automatically created. Check in that file to git.

在“Git 桌面”中右键单击要忽略的文件名,然后将它们添加到.gitignore文件中。这里将自动创建 gitignore 文件。将该文件签入 git。