.gitignore 一切,除了 .c 和 .h 递归

.gitignore everything but .c and .h recursively

我想忽略某个文件夹及其子文件夹中的所有内容,.c.h 文件除外。

然而在本地,我也需要其他文件。在添加 .gitignore 之前或之后,我是否必须在 git-repo 中包含这些不应被跟踪的文件?

我该怎么做?:

#ignore all
*
#but:
!source/**/*.c
!source/**/*.h

这是我目前的解决方案,但它不起作用。但我认为这也与时间点有关,我必须在其中添加文件,这些文件应该被忽略,但需要在本地?

编辑: 问题是,我得到了一个项目的副本,它执行各种 makefile 魔术和其他事情,我什至不知道有什么样的文件类型和子文件夹(我只会在大型项目的一个文件夹中工作,所以我不认为 gitignore 需要如此排他)...而且我不能只提交所有内容,因为我认为必须安装 "lib",所以每个人需要自己做...

问题是模式

*

也排除所有目录。根据the gitignore documentation,

It is not possible to re-include a file if a parent directory of that file is excluded.

然后,要使这项工作正常进行,您需要确保目录不被忽略。 gitignore 模式格式不提供区分目录和常规文件的方法,因此您需要手动执行此操作。 一种可能是放置一个 .gitignore 文件在每个重新包含其所有子目录的目录中,但重新包含所有目录会更容易。这些可以(唯一地)与以 '/' 结尾的模式匹配:

!source/**/

还有,你说得对

But i think this also relates to the point in time, where i have to add the files, that should be ignored

从某种意义上说,gitignore 不适用于已跟踪的文件。

Ignoring * 表示忽略包括顶级目录在内的所有内容。在那之后 git 甚至不查看子目录。修复那个忽略目录。您的整个 .gitignore 应该如下所示:

# Ignore all
*

# Unignore directories
!*/

# Unignore source code files
!source/**/*.c
!source/**/*.h

另一种方法是忽略所有内容,但使用 git add -f 强制添加必要的文件。