Git 如何忽略子文件夹/子目录?
我的 .NET 解决方案中有很多项目。我想排除所有“bin/Debug”和“bin/Release”文件夹(及其内容),但仍包含“bin”文件夹本身以及其中包含的任何 DLL 文件。
.gitignore 与“bin/”忽略“调试< /strong>”和“Release”文件夹,以及“bin”文件夹中包含的任何 DLL 文件。
.gitignore 文件中的 bin/Debug 或 bin/Release 不会排除目录,除非我将忽略模式完全限定为解决方案/Project/bin/Debug - 我不想这样做,因为我需要在解决方案中包含每个项目的完整模式,并为添加的任何新项目添加它。
我该怎么做呢?
I have a lot of projects in my .NET solution. I would like to exclude all "bin/Debug" and "bin/Release" folders (and their contents), but still include the "bin" folder itself and any DLL files contained therein.
.gitignore with "bin/" ignores "Debug" and "Release" folders, but also any DLL files contained in the "bin" folder.
bin/Debug or bin/Release in the .gitignore file does not exclude the directories unless I fully qualify the ignore pattern as Solution/Project/bin/Debug—which I don't want to do as I will need to include this full pattern for each project in my solution, as well as add it for any new projects added.
How can I do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
使用通配符:
在 Git 1.8.2 版本中,您还可以使用
**
通配符来匹配任何级别的子目录:Use wildcards:
With version 1.8.2 of Git, you can also use the
**
wildcard to match any level of subdirectories:您可以在顶层使用 .gitignore 来忽略项目中具有相同名称的所有目录。例如:
这应该立即更新,以便在您执行 git status 时可见。确保这些目录尚未添加到 git,因为这将覆盖忽略。
You can use .gitignore in the top level to ignore all directories in the project with the same name. For example:
This should update immediately so it's visible when you do git status. Ensure that these directories are not already added to git, as that will override the ignores.
前面的所有答案都是有效的,但我认为没有提到的是,一旦您将文件从该目录添加到存储库中,您就不能忽略包含该文件的目录/子目录(git 将忽略该指令)。
要忽略已添加的文件,请运行
否则,您必须首先从存储库的目标目录中删除所有文件 - 然后您可以忽略该文件夹。
All the previous answers are valid, but something that I don't think is mentioned is that once you add a file from that directory into the repository, you can't ignore that directory/subdirectory that contains that file (git will ignore that directive).
To ignore already added files run
Otherwise you'll have to remove all files from the repository's target directory first - and then you can ignore that folder.
问题不是询问忽略所有子目录,但我在任何地方都找不到答案,所以我将其发布:
*/*
。The question isn't asking about ignoring all subdirectories, but I couldn't find the answer anywhere, so I'll post it:
*/*
.排除内容和子目录:
要排除所有子目录但保留内容,请添加“/”:
To exclude content and subdirectories:
To just exclude all subdirectories but take the content, add "/":
除了将正确的条目放入 .gitignore 文件中之外,如果您尝试忽略已添加到存储库中的内容,则必须执行
git rm -r /path/to/dir< /code> 并在将目录添加到 .gitignore 文件之前提交该内容。否则,Git 唯一会忽略的是你的忽略指令。
Besides putting the correct entries in your .gitignore file, if you're trying to ignore something already added to the repository, you have to do
git rm -r /path/to/dir
and commit that before you add the directory to your .gitignore file. Otherwise, the only thing Git will ignore is your ignore directive.我让它在我的机器上工作的唯一方法是这样做:
注意如何必须明确允许您想要包含的每个级别的内容。因此,如果我在主题下有五个子目录,我仍然需要将其拼写出来。
这是来自 @Yarin 的评论:使用.gitignore 忽略除特定目录之外的所有内容
这些都是有用的主题:
我也尝试过
**/wp-content/themes/**
或
/wp-content/themes/** /*
这些对我来说都不起作用。大量的尝试和错误!
The only way I got this to work on my machine was to do it this way:
Notice how you have to explicitly allow content for each level you want to include. So if I have subdirectories five deep under themes, I still need to spell that out.
This is from @Yarin's comment here: Using .gitignore to ignore everything but specific directories
These were useful topics:
I also tried
and
**/wp-content/themes/**
or
/wp-content/themes/**/*
None of that worked for me, either. Lots of trial and error!
忽略存储库根目录的所有子文件夹,但包含此文件夹根目录中的文件的最简单形式是将其添加到您的 .gitignore 文件中:
如果您想忽略另一个子文件夹的所有子文件夹(仍然包括那里的文件):
如果您已经提交了需要忽略的项目,则需要在忽略它们之前将它们从存储库中删除。
要删除存储库根目录的所有子文件夹:
要删除另一个子文件夹的所有子文件夹:
The simplest form to ignore all subfolders of the root of your repo, but include the files at the root of this folder is to add this to your .gitignore file:
If you want ignore all subfolders of another subfolder (still including the files there):
If you have already commited items that need to be ignored, you will need to remove them from the repo before they are ignored.
To remove all subfolders of the root of your repo:
To remove all subfolders of another subfolder:
要忽略所有子目录,您可以简单地使用:
这适用于 Git 1.8.2 版本。
To ignore all subdirectories, you can simply use:
This works as of version 1.8.2 of Git.
忽略所有子文件夹,同时继续跟踪
/bin
目录中的文件的通用方法是将以下行添加到项目的 .gitignore 文件中:如果您只想忽略特定的命名文件子文件夹,您可以执行以下操作:
nb.如果
bin
目录不在项目的根目录中(与 .gitignore 文件一起),则不要使用例如。bin/*/*
您可能需要path/to/bin/*/*
The generic way to ignore all subfolders, while continuing to track the files that are in the
/bin
directory would be to add the following line to your project's .gitignore file:If you want to ignore only particular named subfolders, you can do:
nb. if the
bin
directory is not in the root of your project (alongside the .gitignore file), then instead of eg.bin/*/*
you might needpath/to/bin/*/*
看起来这个页面在这么多年后仍然出现在 Google 搜索的顶部...
现代版本的 Git 支持在单个存储库中嵌套 .gitignore 文件。只需将 .gitignore 文件放在您想要忽略的子目录中即可。使用单个星号来匹配该目录中的所有内容:
如果您之前进行了提交,请记住删除以前跟踪的文件:
您可以通过执行
git status
来确认它,以显示从跟踪中删除的所有文件。It seems this page still shows up on the top of Google search after so many years...
Modern versions of Git support nesting .gitignore files within a single repository. Just place a .gitignore file in the subdirectory that you want ignored. Use a single asterisk to match everything in that directory:
If you've made previous commits, remember to remove previously tracked files:
You can confirm it by doing
git status
to show you all the files removed from tracking.要实现忽略子目录中的所有文件,您只需将这些模式添加到 .gitignore 文件中:
要了解此处发生的情况,
*/*
:此模式告诉 Git 忽略任何子目录中的所有文件 (包括当前目录)。斜杠之前的星号与任何文件夹名称匹配,斜杠之后的星号与任何文件名匹配。*.*
:另一方面,此模式将忽略所有以点为前缀的文件,例如在 UNIX 系统上通常被视为隐藏的文件。这应该适用于大多数忽略文件,例如 .gitignore/.dockerignore/.npmignore 等。
To accomplish ignoring all files in subdirectories, you can simply add these patterns to your .gitignore file:
To understand what is happening here,
*/*
: This pattern tells Git to ignore all files in any subdirectory (including the current directory). The asterisk before the slash matches any folder name, and the asterisk after the slash matches any file name.*.*
: This pattern, on the other hand, will ignore all dot-prefixed files, such as ones commonly treated as hidden on UNIX systems.This should work in most ignore files such as .gitignore/.dockerignore/.npmignore etc.
为了忽略子文件夹,但不忽略主文件夹,我只能通过在主文件夹中放置一个虚拟 readme.txt 文件来使其在 Visual Studio Code 中工作。
只有这样 /*/ 才会在主文件夹中检查(并且没有子文件夹)。
For ignoring subfolders, but not the main folder, I could only get this to work in Visual Studio Code by placing a dummy readme.txt file in the main folder.
Only then is /*/ checked in the main folder (and no subfolders).