我的程序通常生成巨大的输出文件(~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_"开头的任何文件
Vin*_*ddy 70
在您的存储库中创建.gitignore文件,您只想跟踪c文件并忽略所有其他文件,然后添加以下行...
*
!*.c
Run Code Online (Sandbox Code Playgroud)
'*'将忽略所有文件
而且!否定文件被忽略....所以这里我们要求git不要忽略c文件....
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
我在 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)