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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1279533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 06:53:39  来源:igfitidea点击:

Is there a way to tell git to only include certain files instead of ignoring certain files?

gitgitignore

提问by Daisy Sophia Hollman

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

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

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?

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

回答by T.E.D.

I haven't had need to try this myself, but from my reading of TFMit 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:

我没有必要自己尝试这个,但是从我对TFM 的阅读来看,否定模式似乎可以满足您的需求。您可以使用稍后的否定条目覆盖 .gitignore 中的条目。因此,您可以执行以下操作:

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

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

让它忽略除 custom.c 和任何以“frob_”开头的所有 .c 文件

回答by Vineel Kumar Reddy

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

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

*
!*.c

'*' will ignore all files

'*' 将忽略所有文件

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

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

回答by Smaranjit

The best solution to achieve this

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

create .gitignorefile in repository root, and if you want to include only .cfile then you need to add below lines to .gitignorefile

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

*.*
!*.c

this will include all .cfile from directory and subdirectory recursively.

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

using

使用

*
!*.c

will not work on all version of git.

不适用于所有版本的 git。

Tested on

git version 2.12.2.windows.2

经过测试

git 版本 2.12.2.windows.2

回答by gzfrancisco

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

回答by Christian Severin

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.

...然后只将这些东西添加src/到我的存储库中并bin/完全忽略。