我的程序通常会生成巨大的输出文件(约1 GB),我不希望将它们备份到 Git 存储库中。因此,我不能使用以下命令:
git add .
我需要做类似这样的事情
git add *.c *.cc *.f *.F *.C *.h *.cu
这有点繁琐...
我相信我可以编写一个快速的perl脚本,将目录内容列入.gitignore文件中,再根据一个名为.gitinclude(或类似名称)的文件删除文件,但这似乎有点hackish。 有更好的方法吗?
我自己没有尝试过这个,但是根据我阅读的TFM,似乎否定模式可以实现你想要的效果。 你可以使用后面的否定项覆盖.gitignore中的条目。 因此,你可以这样做:
*.c
!frob_*.c
!custom.c
忽略所有的.c文件,但是除了custom.c和任何以"frob_"开头的文件。在您的代码库中创建.gitignore文件,并且只想跟踪c文件并忽略所有其他文件,则将以下行添加到该文件中...
*
!*.c
'*' 将忽略所有文件
而!将排除需要被忽略的文件....所以这里我们要求git不要忽略c文件....
*
是一个通配符,即使对于 .
也是如此,因此隐藏文件也被忽略,但是以 .c
结尾的所有隐藏文件都会被包括。 - tim-phillips*
后跟
!.bash*
- user5359531*.*
- 参见@Smaranjit Maiti的答案 - Martin Capodici最好的解决方案是
在仓库根目录
中创建.gitignore
文件,如果你只想包含.c
文件,则需要将以下行添加到.gitignore
文件中。
*.*
!*.c
这将递归地包括目录和子目录中的所有.c
文件。
使用*
!*.c
不是所有版本的git都适用。
已测试通过
git版本为2.12.2.windows.2
*
相关的问题,只能使用*.*
解决。这是在Windows上使用gitbash和git版本2.9.0.windows.1时发生的。 - Martin Capodici# 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
.gitinclude.NET
和托管在GH Pages上的git-do-not-ignore
,它们只是简单地帮助使这个过程更加容易维护。*
开始,以字面意义上忽略当前根目录中的所有文件和文件夹。!
那样简单,例如!.gitignore
,以确保我们的仓库不会忽略自己的.gitignore
规则文件。# ...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
.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
!.*
+ .git
| (...)
+ bin
| my_exe.exe
| my_output.txt
+ src
some_file.c
some_file.h
...然后只将src/
中的内容添加到我的代码库中,完全忽略bin/
。