有没有办法告诉git只包含某些文件而不是忽略某些文件?

Dav*_*man 151 git gitignore

我的程序通常生成巨大的输出文件(~1 GB),我不想备份到git存储库.所以不是能够做到的

git add .
Run Code Online (Sandbox Code Playgroud)

我必须做点什么

git add *.c *.cc *.f *.F *.C *.h *.cu
Run Code Online (Sandbox Code Playgroud)

这有点麻烦......

我相信我可以编写一个快速的perl脚本,将目录内容写入.gitignore,然后根据.gitinclude(或类似名称)文件删除文件,但这似乎有点过于苛刻.有没有更好的办法?

T.E*_*.D. 212

我没有必要自己尝试这个,但是从我对TFM的阅读看起来,一个否定的模式会做你想要的.您可以使用以后的否定条目覆盖.gitignore中的条目.因此你可以这样做:

*.c
!frob_*.c
!custom.c
Run Code Online (Sandbox Code Playgroud)

让它忽略除custom.c之外的所有.c文件以及以"frob_"开头的任何文件

  • 谢谢,TED这很有效.我所要做的就是用*启动.gitignore文件,然后列出感叹号所包含的所有包含的文件模式. (19认同)
  • 文件夹怎么样?我无法通过否定规则找到一种方法来包含文件夹和文件. (7认同)
  • 关于文件夹请查看http://stackoverflow.com/questions/12799855/configure-git-to-track-only-one-file-extension (3认同)
  • 非常好.我正在使用它在我的主文件夹中有一个存储库,用于vimrc和bashrc之类的东西 (2认同)

Vin*_*ddy 70

在您的存储库中创建.gitignore文件,您只想跟踪c文件并忽略所有其他文件,然后添加以下行...

*
!*.c
Run Code Online (Sandbox Code Playgroud)

'*'将忽略所有文件

而且!否定文件被忽略....所以这里我们要求git不要忽略c文件....

  • “ *”即使是。也是通配符,因此隐藏文件也将被忽略,但是所有以.c结尾的隐藏文件都将被包括在内。 (2认同)
  • 使用这种方法我认为子文件夹中的文件也会被忽略.有关详细信息,请查看此信息http://stackoverflow.com/a/11853075/739436 (2认同)
  • 您可能需要在 Windows 上使用 `*.*` - 请参阅@Smaranjit Maiti 的答案 (2认同)

Sma*_*jit 10

实现这一目标的最佳解决方案

.gitignore在存储库中创建文件root,如果您只想包含.c文件,则需要在.gitignore文件中添加以下行

*.*
!*.c
Run Code Online (Sandbox Code Playgroud)

这将包括.c递归目录和子目录中的所有文件.

运用

*
!*.c
Run Code Online (Sandbox Code Playgroud)

不适用于所有版本的git.

经过测试

git版本2.12.2.windows.2

  • 是的,我对 `*` 有同样的问题,不得不使用 `*.*`。这是在 Windows 上使用 gitbash 和 git 版本 2.9.0.windows.1 (3认同)

And*_*eff 6

我在 SO 和其他网站上看到了许多关于最初的“忽略一切”规则的建议,但我发现其中大多数都有自己烦人的使用问题。这催生了诸如可分发.gitinclude.NET托管的 GH 页面git-do-not-ignore之类的项目,每个项目都有助于简化维护工作。

这些(以及许多其他博客文章)中的每一篇都建议从简单地开始*,毫不夸张地说,忽略当前根目录中的所有文件和文件夹。

此后,包含文件就像在路径中添加前缀一样简单!,例如!.gitignore确保我们的存储库不会忽略它自己的.gitignore规则文件。

这样做的缺点是,当 Git 遇到被忽略的文件夹时,出于性能原因,它不会检查其内容。尝试忽略嵌套路径中的文件会变得非常麻烦:

# ...when ignoring all files and folders in the current root
*

!custom_path                       # allow Git to look inside this folder
custom_path/*                      # but ignore everything it contains
!custom_path/extras                # allow Git to look inside this folder
custom_path/extras/*               # but ignore everything it contains
!custom_path/extras/path_to_keep   # allow Git to see the file or folder you want to commit
Run Code Online (Sandbox Code Playgroud)

因此,为了提供另一种想法,我刚刚.gitignore在 Windows 用户配置文件文件夹的根目录中配置了一个文件,以而**/*不是常见的*或开头*.*

此后,我想要显式包含的每条路径在每个树级别仅需要一个条目。将前面的示例稍微简化为以下内容:

# ...when ignoring all files recursively from the current root
**/*

!custom_path                       # allow Git to look inside this folder
!custom_path/extras                # allow Git to look inside this folder
!custom_path/extras/path_to_keep   # allow Git to see the file or folder you want to commit
Run Code Online (Sandbox Code Playgroud)

这并不是一个巨大的差异,但它足以使文件更容易阅读和维护,特别是当尝试“取消忽略”嵌套大约 5 层深度的文件时......


小智 5

如果您需要忽略文件而不是目录中的特定文件,我是这样做的:

# Ignore everything under "directory"
directory/*
# But don't ignore "another_directory"
!directory/another_directory
# But ignore everything under "another_directory"
directory/another_directory/*
# But don't ignore "file_to_be_staged.txt"
!directory/another_directory/file_to_be_staged.txt
Run Code Online (Sandbox Code Playgroud)