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

发布于 2024-08-02 03:13:28 字数 311 浏览 11 评论 0原文

我的程序通常会生成巨大的输出文件(~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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

二货你真萌 2024-08-09 03:13:29

如果您只想包含点文件,这对我有用......

!.*

If you're only trying to include dot files, this worked for me...

!.*
棒棒糖 2024-08-09 03:13:29

迟到了,但我的解决方案是为源文件提供一个目录,为可执行文件和程序输出提供一个不同的目录,如下所示:

+ .git
|    (...)
+ bin
|    my_exe.exe
|    my_output.txt
+ src
     some_file.c
     some_file.h 

...然后只在 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:

+ .git
|    (...)
+ bin
|    my_exe.exe
|    my_output.txt
+ src
     some_file.c
     some_file.h 

... and then only add the stuff in src/ to my repository and ignore bin/ entirely.

暗喜 2024-08-09 03:13:28

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

*.c
!frob_*.c
!custom.c

让它忽略除 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:

*.c
!frob_*.c
!custom.c

To have it ignore all .c files except custom.c and anything starting with "frob_"

小兔几 2024-08-09 03:13:28

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

*
!*.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....

*
!*.c

'*' will ignore all files

and ! will negate files be to ignored....so here we are asking git not to ignore c files....

一场春暖 2024-08-09 03:13:28

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

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

*.*
!*.c

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

使用

*
!*.c

不适用于所有版本的 git。

测试于

git版本2.12.2.windows.2

The best solution to achieve this

create .gitignore file in repository root, and if you want to include only .c file then you need to add below lines to .gitignore file

*.*
!*.c

this will include all .c file from directory and subdirectory recursively.

using

*
!*.c

will not work on all version of git.

Tested on

git version 2.12.2.windows.2

帅气尐潴 2024-08-09 03:13:28

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

# 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

If you need to ignore files but not a specific file inside a directory, here is how I did it:

# 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
稀香 2024-08-09 03:13:28

我在 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

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

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

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

这并不完全是一个巨大差异,但它足以使文件更易于阅读和维护,尤其是当尝试“取消忽略”嵌套约 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 hosted git-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:

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

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:

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

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文