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

我见过类似的问题(123) ,但我没有得到一个适当的解决方案从他们。

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

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

# Ignore everything
*


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


# Don't ignore .gitignore
!.gitignore


# Now exclude our type
!*.foo

这个解决方案的问题是 它停止跟踪新添加的文件(因为 *忽略所有文件)。我不想一直排除每个文件类型。我想正常的行为,如果有任何新的文件添加,git status显示它。

我终于得到了一个解决方案 给你。解决方案是添加另一个 .gitignore文件在 Resources文件夹。这正常工作。

我可以用一个忽略文件实现同样的功能吗?我发现在不同的目录中有许多忽略文件有点笨拙。

这就是我想要达到的目标:

# Ignore everything under Resources folder, not elsewhere
Resources


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


# Now exclude our type
!*.foo

但是这样会得到相反的输出,它忽略 *.foo类型并跟踪其他文件。

133672 次浏览

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

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

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

@SimonBuchan is correct.

Since git 1.8.2, Resources/** !Resources/**/*.foo works.

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.

Either I'm doing it wrongly, or the accepted answer does not work anymore with the current 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 committed anyway if there is no committed file inside)
!/Resources/**/
# And except for *.foo files
!*.foo