git Git忽略子文件夹

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2545602/
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-09-10 08:10:07  来源:igfitidea点击:

Git ignore sub folders

gitdirectoryignoregitignore

提问by Marcel

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's contained therein.

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

.gitignore with "bin/" ignores "Debug" and "Release" folders, but also any dll's contained in the "bin" folder.

带有“bin/”的 .gitignore 会忽略“Debug”和“Release”文件夹,还会忽略“bin”文件夹中包含的任何 dll。

"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.

.gitignore 文件中的“bin/Debug”或“bin/Release”不排除目录,除非我将忽略模式完全限定为“解决方案/项目/bin/Debug” - 我不想像我一样做需要在我的解决方案中为每个项目包含此完整模式,并为添加的任何新项目添加它。

Any suggestions?

有什么建议?

回答by Josh Lee

Have you tried 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:

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

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

回答by Andreas

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

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

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.

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

回答by mgold

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

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

回答by ajacian81

All the above answers are valid, but something that I don't think is mentioned is that once you add a file from that directory into the repo, you can't ignore that directory/subdirectory that contains that file (git will ignore that directive).

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

To ignore already added files run

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

$ git rm --cached

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

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

回答by MidnightJava

Besides putting the correct entries in your .gitignore file, if you're trying to ignore something already added to the repo, you have to do git rm -r /path/to/dirand commit that before you add the dir to your .gitignore file. Otherwise the only thing git will ignore is your ignore directive.

除了将正确的条目放入 .gitignore 文件之外,如果您试图忽略已添加到存储库中的内容,则必须git rm -r /path/to/dir在将目录添加到 .gitignore 文件之前执行并提交该内容。否则 git 唯一会忽略的是你的 ignore 指令。

回答by Katie

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 5 deep under themes, I still need to spell that out.

请注意您必须如何明确允许您想要包含的每个级别的内容。所以如果我在主题下面有 5 个子目录,我仍然需要把它拼出来。

This is from @Yarin's comment here: https://stackoverflow.com/a/5250314/1696153

这是来自@Yarin 的评论:https: //stackoverflow.com/a/5250314/1696153

These were useful topics:

这些是有用的主题:

I also tried

我也试过

*
*/*
**/**

and **/wp-content/themes/**

**/wp-content/themes/**

or /wp-content/themes/**/*

或者 /wp-content/themes/**/*

None of that worked for me, either. Lots of trail and error!

这些都不适合我。很多线索和错误!

回答by A. Morel

To exclude content and subdirectories:

要排除内容和子目录:

**/bin/*

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

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

**/bin/*/

回答by frederickjh

To ignore all subdirectories you can simply use:

要忽略所有子目录,您只需使用:

**/

This works as of version 1.8.2 of git.

这适用于 git 1.8.2 版。

回答by Nick F

The generic way to ignore all subfolders, while continuing to track the files that are in the /bindirectory would be to add the following line to your project's .gitignore file:

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

bin/*/*

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

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

bin/Debug/*
bin/Release/*

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

备注 如果该bin目录不在项目的根目录中(与 .gitignore 文件一起),则代替例如。bin/*/*你可能需要path/to/bin/*/*