git 正确递归地忽略特定文件夹下的所有文件,特定文件类型除外

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

Correctly ignore all files recursively under a specific folder except for a specific file type

gitversion-controldirectorygitignoresubdirectory

提问by nawfal

I have seen similar questions (1, 2and 3), but I don't get a proper solution from them.

我见过类似的问题(123),但我没有从他们那里得到正确的解决方案。

I need to ignore all files under a particular folder except for a specific file type. The folder is a subdirectory for the root path. Let me name the folder Resources. Since I don't want to complicate things, let me ignore files under all folders named Resourceswherever it is.

我需要忽略特定文件夹下的所有文件,特定文件类型除外。该文件夹是根路径的子目录。让我命名文件夹Resources。由于我不想让事情复杂化,让我忽略所有命名的文件夹下的文件Resources

This is the most common solution (in all the duplicate questions)

这是最常见的解决方案(在所有重复问题中)

# Ignore everything
*

# Don't ignore directories, so we can recurse into them
!*/

# Don't ignore .gitignore
!.gitignore

# Now exclude our type
!*.foo

The problem with this solution is that it stops tracking newly added files(since *ignores all files). I don't want to keep excluding each and every file type. I want normal behaviour where if any new file is added, git statusshows it.

此解决方案的问题在于它停止跟踪新添加的文件(因为*忽略所有文件)。我不想继续排除每种文件类型。我想要正常的行为,如果添加了任何新文件,则git status显示它。

I finally got a solution here. The solution is to add another .gitignorefile in Resourcesfolder. This works correctly.

我终于在这里找到了解决方案。解决方案是.gitignoreResources文件夹中添加另一个文件。这工作正常。

Can I achieve the same with one ignore file? I find having many ignore files in different directories a bit clunky.

我可以用一个忽略文件来达到同样的效果吗?我发现在不同目录中有许多忽略文件有点笨拙。

This is what I'm trying to achieve:

这就是我想要实现的目标:

# Ignore everything under Resources folder, not elsewhere
Resources

# Don't ignore directories, so we can recurse into them
!*Resources/

# Now exclude our type
!*.foo

But this gives the opposite output. It ignores *.footypes and tracks other files.

但这给出了相反的输出。它忽略*.foo类型并跟踪其他文件。

回答by Jim G.

@SimonBuchan is correct.

@SimonBuchan 是正确的。

Since git 1.8.2, Resources/** !Resources/**/*.fooworks.

从 git 1.8.2 开始,Resources/** !Resources/**/*.foo有效。

回答by Ben Martin

The best answer is to add a Resources/.gitignore file under Resources containing:

最好的答案是在 Resources 下添加一个 Resources/.gitignore 文件,其中包含:

# Ignore any file in this directory except for this file and *.foo files
*
!/.gitignore
!*.foo

If you are unwilling or unable to add that .gitignore file, there is an inelegant solution:

如果您不愿意或无法添加该 .gitignore 文件,则有一个不雅的解决方案:

# Ignore any file but *.foo under Resources. Update this if we add deeper directories
Resources/*
!Resources/*/
!Resources/*.foo
Resources/*/*
!Resources/*/*/
!Resources/*/*.foo
Resources/*/*/*
!Resources/*/*/*/
!Resources/*/*/*.foo
Resources/*/*/*/*
!Resources/*/*/*/*/
!Resources/*/*/*/*.foo

You will need to edit that pattern if you add directories deeper than specified.

如果添加的目录比指定的更深,则需要编辑该模式。

回答by Tiago

This might look stupid, but check if you haven't already added the folder/files you are trying to ignore to the index before. If you did, it does not matter what you put in your .gitignore file, the folders/files will still be staged.

这可能看起来很愚蠢,但请检查您之前是否尚未将您试图忽略的文件夹/文件添加到索引中。如果你这样做了,不管你在 .gitignore 文件中放了什么,文件夹/文件仍然会被暂存。

回答by Aleksander Stelmaczonek

I'm doing it wrong or accepted answer does not work anymore with current git.

我做错了或接受的答案不再适用于当前的 git。

I have actually found the proper solution and posted it under almost the same question here. For more details head there.

其实我已经找到妥善的解决办法,并张贴在几乎同样的问题在这里。有关更多详细信息,请前往那里。

Solution:

解决方案:

# Ignore everything inside Resources/ directory
/Resources/**
# Except for subdirectories(won't be commited anyway if there is no commited file inside)
!/Resources/**/
# And except for *.foo files
!*.foo