有没有办法告诉 git 只包含某些文件而不是忽略某些文件?
我的程序通常会生成巨大的输出文件(~1 GB),我不想将其备份到 git 存储库。 因此,我不能这样做,而是
git add .
必须做
git add *.c *.cc *.f *.F *.C *.h *.cu
一些有点麻烦的事情...
我相当有信心我可以编写一个快速的 perl 脚本 ls 将目录内容放入 .gitignore 中,然后根据 .gitinclude 删除文件(或一些类似的名称)文件,但这似乎有点太黑客了。 有没有更好的办法?
My programs generally generate huge output files (~1 GB) which I do not want to be backing up to the git repository. So instead of being able to do
git add .
I have to do something like
git add *.c *.cc *.f *.F *.C *.h *.cu
which is a little bit cumbersome...
I feel fairly confident I could write a quicky perl script ls the directory contents into .gitignore and then remove files based on a .gitinclude (or some similar name) file, but that seems a little too hackish. Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您只想包含点文件,这对我有用......
If you're only trying to include dot files, this worked for me...
迟到了,但我的解决方案是为源文件提供一个目录,为可执行文件和程序输出提供一个不同的目录,如下所示:
...然后只在 src/ 中添加内容到我的存储库并完全忽略
bin/
。Late to the party, but my solution would be to have a directory for source files and a different directory for executables and program output, something like this:
... and then only add the stuff in
src/
to my repository and ignorebin/
entirely.我自己不需要尝试这个,但是从我对 TFM 的阅读来看,它看起来像一个否定模式会做你想做的事。 您可以使用后来的否定条目覆盖 .gitignore 中的条目。 因此你可以这样做:
让它忽略除 custom.c 和以“frob_”开头的任何内容之外的所有 .c 文件
I haven't had need to try this myself, but from my reading of TFM it looks like a negated pattern would do what you want. You can override entries in .gitignore with later negated entries. Thus you could do something like:
To have it ignore all .c files except custom.c and anything starting with "frob_"
在存储库中创建 .gitignore 文件,并且您只想跟踪 c 文件并忽略所有其他文件,然后向其中添加以下行...
“*”将忽略所有文件
并且! 将否定文件被忽略......所以在这里我们要求 git 不要忽略 c 文件......
create .gitignore file in your repository and you want to track only c files and ignore all other files then add the following lines to it....
'*' will ignore all files
and ! will negate files be to ignored....so here we are asking git not to ignore c files....
在存储库
root
中创建.gitignore
文件,并且如果您只想包含.c
文件,那么您需要将以下行添加到.gitignore
文件中,这将递归地包含目录和子目录中的所有
.c
文件。使用
不适用于所有版本的 git。
create
.gitignore
file in repositoryroot
, and if you want to include only.c
file then you need to add below lines to.gitignore
filethis will include all
.c
file from directory and subdirectory recursively.using
will not work on all version of git.
如果您需要忽略文件而不是目录中的特定文件,我是这样做的:
If you need to ignore files but not a specific file inside a directory, here is how I did it:
我在 SO 和其他网站上看到了许多关于最初的“忽略一切”规则的建议,但我发现其中大多数都有自己烦人的使用问题。 这催生了诸如 可分发的
.gitinclude.NET
和托管的 GH 页面git-do-not-ignore
,其中每一个都有助于减少维护的繁琐。这些文章(以及许多其他博客文章)都建议从简单的
*
开始,从字面上看,忽略当前根目录中的所有文件和文件夹。此后,包含文件就像在路径前添加
!
一样简单,例如!.gitignore
以确保我们的存储库不会忽略它自己的 .gitignore 规则文件。这样做的缺点是,当 Git 遇到被忽略的文件夹时,出于性能原因,它不会检查其内容。 尝试不忽略嵌套路径中的文件变得非常麻烦:
因此为了提供替代想法,我刚刚在 Windows 根目录中配置了一个
.gitignore
文件用户配置文件文件夹,以**/*
开头,而不是常见的*
或*.*
。此后,我想要显式包含的每条路径在每个树级别仅需要一个条目。 稍微将前面的示例简化为以下内容:
这并不完全是一个巨大差异,但它足以使文件更易于阅读和维护,尤其是当尝试“取消忽略”嵌套约 5 层深度的文件时...
I've seen a number of suggestions for the initial "ignore everything" rule, both here on SO and on other sites, but I found most of them to have thir own annoying usage issues. this has given rise to projects like the distributable
.gitinclude.NET
and the GH Pages hostedgit-do-not-ignore
, each of which simply help make this less tedious to manutain.each of these (and many other blog posts) recommend starting out with a simply
*
to, quite literally, ignore all files and folders in the current root.thereafter, including a file is "as simple" as prefixing the path with
!
, such as!.gitignore
to ensure our repo doesn't ignore it's own.gitignore
rules file.the down side of this, is that when Git encounters an ignored folder, for performance reasons it does not check it's contents. trying to not ignore a file in a nested path gets very cumbersome:
so to offer an alternative idea, I've just configured a
.gitignore
file in the root of my Windows user profile folder, starting with**/*
instead of the commonly seen*
or*.*
.thereafter, each path that I want to explicitly include requires only one entry per tree level. slightly simplifying the previous example to the following:
this is not exactly a massive difference, but it is enough of a difference to make the file much easier to read and maintain, especially when trying to "un-ignore" a file nested about 5 levels deep...