Git 如何忽略子文件夹/子目录?

发布于 2024-08-26 22:46:47 字数 559 浏览 6 评论 0原文

我的 .NET 解决方案中有很多项目。我想排除所有“bin/Debug”和“bin/Release”文件夹(及其内容),但仍包含“bin”文件夹本身以及其中包含的任何 DLL 文件。

.gitignore 与“bin/”忽略“调试< /strong>”和“Release”文件夹,以及“bin”文件夹中包含的任何 DLL 文件。

.gitignore 文件中的 bin/Debugbin/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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

趁微风不噪 2024-09-02 22:46:47

使用通配符:

Solution/*/bin/Debug
Solution/*/bin/Release

在 Git 1.8.2 版本中,您还可以使用 ** 通配符来匹配任何级别的子目录:

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

Use wildcards:

Solution/*/bin/Debug
Solution/*/bin/Release

With version 1.8.2 of Git, you can also use the ** wildcard to match any level of subdirectories:

**/bin/Debug/
**/bin/Release/
鼻尖触碰 2024-09-02 22:46:47

您可以在顶层使用 .gitignore 来忽略项目中具有相同名称的所有目录。例如:

Debug/
Release/

这应该立即更新,以便在您执行 git status 时可见。确保这些目录尚未添加到 git,因为这将覆盖忽略。

You can use .gitignore in the top level to ignore all directories in the project with the same name. For example:

Debug/
Release/

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.

回忆追雨的时光 2024-09-02 22:46:47

前面的所有答案都是有效的,但我认为没有提到的是,一旦您将文件从该目录添加到存储库中,您就不能忽略包含该文件的目录/子目录(git 将忽略该指令)。

要忽略已添加的文件,请运行

git rm -r --cached .

否则,您必须首先从存储库的目标目录中删除所有文件 - 然后您可以忽略该文件夹。

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

git rm -r --cached .

Otherwise you'll have to remove all files from the repository's target directory first - and then you can ignore that folder.

你在看孤独的风景 2024-09-02 22:46:47

问题不是询问忽略所有子目录,但我在任何地方都找不到答案,所以我将其发布:*/*

The question isn't asking about ignoring all subdirectories, but I couldn't find the answer anywhere, so I'll post it: */*.

甜心 2024-09-02 22:46:47

排除内容和子目录:

**/bin/*

要排除所有子目录但保留内容,请添加“/”:

**/bin/*/

To exclude content and subdirectories:

**/bin/*

To just exclude all subdirectories but take the content, add "/":

**/bin/*/
脱离于你 2024-09-02 22:46:47

除了将正确的条目放入 .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.

向日葵 2024-09-02 22:46:47

我让它在我的机器上工作的唯一方法是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

# Now ignore all files in the current directory
# (This fails to ignore files without a ".", for example
#'file.txt' works, but
#'file' doesn't):
/*.*

# Only include these specific directories and subdirectories:
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/

注意如何必须明确允许您想要包含的每个级别的内容。因此,如果我在主题下有五个子目录,我仍然需要将其拼写出来。

这是来自 @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:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

# Now ignore all files in the current directory
# (This fails to ignore files without a ".", for example
#'file.txt' works, but
#'file' doesn't):
/*.*

# Only include these specific directories and subdirectories:
!wordpress/
!wordpress/*/
!wordpress/*/wp-content/
!wordpress/*/wp-content/themes/

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!

伴梦长久 2024-09-02 22:46:47

忽略存储库根目录的所有子文件夹,但包含此文件夹根目录中的文件的最简单形式是将其添加到您的 .gitignore 文件中:

*/

如果您想忽略另一个子文件夹的所有子文件夹(仍然包括那里的文件):

subfolder/*/

如果您已经提交了需要忽略的项目,则需要在忽略它们之前将它们从存储库中删除。

要删除存储库根目录的所有子文件夹:

git rm -r --cached */*

要删除另一个子文件夹的所有子文件夹:

git rm -r --cached subfolder/*/*

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):

subfolder/*/

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:

git rm -r --cached */*

To remove all subfolders of another subfolder:

git rm -r --cached subfolder/*/*
香橙ぽ 2024-09-02 22:46:47

要忽略所有子目录,您可以简单地使用:

**/

这适用于 Git 1.8.2 版本。

To ignore all subdirectories, you can simply use:

**/

This works as of version 1.8.2 of Git.

他是夢罘是命 2024-09-02 22:46:47

忽略所有子文件夹,同时继续跟踪 /bin 目录中的文件的通用方法是将以下行添加到项目的 .gitignore 文件中:

bin/*/*

如果您只想忽略特定的命名文件子文件夹,您可以执行以下操作:

bin/Debug/*
bin/Release/*

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:

bin/*/*

If you want to ignore only particular named subfolders, you can do:

bin/Debug/*
bin/Release/*

nb. if the bin directory is not in the root of your project (alongside the .gitignore file), then instead of eg. bin/*/* you might need path/to/bin/*/*

妥活 2024-09-02 22:46:47

看起来这个页面在这么多年后仍然出现在 Google 搜索的顶部...

现代版本的 Git 支持在单个存储库中嵌套 .gitignore 文件。只需将 .gitignore 文件放在您想要忽略的子目录中即可。使用单个星号来匹配该目录中的所有内容:

echo "*" > /path/to/bin/Debug/.gitignore
echo "*" > /path/to/bin/Release/.gitignore

如果您之前进行了提交,请记住删除以前跟踪的文件:

git rm -rf /path/to/bin/Debug
git rm -rf /path/to/bin/Release

您可以通过执行 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:

echo "*" > /path/to/bin/Debug/.gitignore
echo "*" > /path/to/bin/Release/.gitignore

If you've made previous commits, remember to remove previously tracked files:

git rm -rf /path/to/bin/Debug
git rm -rf /path/to/bin/Release

You can confirm it by doing git status to show you all the files removed from tracking.

好听的两个字的网名 2024-09-02 22:46:47

要实现忽略子目录中的所有文件,您只需将这些模式添加到 .gitignore 文件中:

*/*
*.*

要了解此处发生的情况,

  1. */*:此模式告诉 Git 忽略任何子目录中的所有文件 (包括当前目录)。斜杠之前的星号与任何文件夹名称匹配,斜杠之后的星号与任何文件名匹配。
  2. *.*:另一方面,此模式将忽略所有以点为前缀的文件,例如在 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,

  1. */*: 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.
  2. *.*: 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.

孤者何惧 2024-09-02 22:46:47

为了忽略子文件夹,但不忽略主文件夹,我只能通过在主文件夹中放置一个虚拟 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文