如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
当前回答
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
### Above combination will ignore all files without extension ###
# Ignore files with extension `.class` & `.sm`
*.class
*.sm
# Ignore `bin` dir
bin/
# or
*/bin/*
# Unignore all `.jar` in `bin` dir
!*/bin/*.jar
# Ignore all `library.jar` in `bin` dir
*/bin/library.jar
# Ignore a file with extension
relative/path/to/dir/filename.extension
# Ignore a file without extension
relative/path/to/dir/anotherfile
其他回答
要将所有可执行文件追加到.gitignore(从您的问题判断,您可能指的是“二进制文件”),可以使用
find . -executable -type f >>.gitignore
如果您不关心.gitignore中的行顺序,您还可以使用以下命令更新.gitignore,该命令还可以删除重复项并保持字母顺序不变。
T=$(mktemp); (cat .gitignore; find . -executable -type f | sed -e 's%^\./%%') | sort | uniq >$T; mv $T .gitignore
注意,不能将输出直接输送到.gitignore,因为这会在cat打开文件以供读取之前截断该文件。此外,您可能还想添加\!正则表达式”。* / * /。如果您不希望在子目录中包含可执行文件,则可以将*'作为查找选项。
添加如下内容
*.o
在.gitignore文件中,把它放在你的repo的根目录下(或者你可以把它放在你想要的任何子目录中——它将从那个级别应用),然后签入。
编辑:
对于没有扩展名的二进制文件,最好将它们放在bin/或其他文件夹中。毕竟没有基于内容类型的忽略。
你可以试试
*
!*.*
但这并非万无一失。
# Ignore all
*
# Unignore all with extensions
!*.*
# Unignore all dirs
!*/
### Above combination will ignore all files without extension ###
# Ignore files with extension `.class` & `.sm`
*.class
*.sm
# Ignore `bin` dir
bin/
# or
*/bin/*
# Unignore all `.jar` in `bin` dir
!*/bin/*.jar
# Ignore all `library.jar` in `bin` dir
*/bin/library.jar
# Ignore a file with extension
relative/path/to/dir/filename.extension
# Ignore a file without extension
relative/path/to/dir/anotherfile
.gitignore使用glob编程来过滤文件,至少在Linux上是这样。
我准备在一个Meetup上做一个编码演讲,在准备过程中,我创建了一个包含几个子目录的目录,这些子目录根据我想要呈现它们的顺序命名:01_subject1, 02_subject2, 03_subject3。每个子目录都包含一个源文件,其扩展名与语言相关,可编译为一个可执行文件,根据惯例,该文件的名称与不带扩展名的源文件名匹配。
我排除了以下.gitignore行以数字为前缀的目录中的编译文件:
[0-9] [0-9] / [! \] * _ *
According to my understanding of the documentation, it shouldn't work. Having the trailing asterisk should fail because it should match any number of unspecified characters, including the '.' + extension. Omitting the trailing asterisk should fail (and does) because [!\.] matches only a single non-period character. However, I added the trailing asterisk, as I would for a regular expression, and it works. By work, I mean that git notices changes to the source file, but not the existence or changes to the compiled files.
如果你在你的.gitignore文件上执行这些命令,文件仍然出现,你可能想尝试:
git rm --cached FILENAME
之后,添加你的。gitignore,提交并推送。 我花了40分钟才明白,希望这对像我这样的新手有帮助