如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
当前回答
在某些子目录中也可以忽略,而不仅仅是在根目录中:
# Ignore everything in a root
/*
# But not files with extension located in a root
!/*.*
# And not my subdir (by name)
!/subdir/
# Ignore everything inside my subdir on any level below
/subdir/**/*
# A bit of magic, removing last slash or changing combination with previous line
# fails everything. Though very possibly it just says not to ignore sub-sub-dirs.
!/subdir/**/
# ...Also excluding (grand-)children files having extension on any level
# below subdir
!/subdir/**/*.*
或者,如果你只想包含一些特定类型的文件:
/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
如果你想的话,它甚至可以像每个新子目录一样工作!:
/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
前导斜杠只在前两行中重要,在其他行中是可选的。在!/*/和!/subdir/中的尾斜杠也是可选的,但仅在这一行中。
其他回答
添加如下内容
*.o
在.gitignore文件中,把它放在你的repo的根目录下(或者你可以把它放在你想要的任何子目录中——它将从那个级别应用),然后签入。
编辑:
对于没有扩展名的二进制文件,最好将它们放在bin/或其他文件夹中。毕竟没有基于内容类型的忽略。
你可以试试
*
!*.*
但这并非万无一失。
对于二进制文件,最好的办法是为它们提供一个可以用标准模式轻松过滤掉的扩展名,或者将它们放入可以在目录级别过滤掉的目录中。
扩展建议在Windows中更适用,因为扩展是标准的,并且基本上是必需的,但在Unix中,您可以对可执行二进制文件使用扩展,也可以不使用扩展。在这种情况下,您可以将它们放在bin/文件夹中,并将bin/添加到.gitignore中。
在您非常具体的小范围示例中,您可以将hello放在.gitignore中。
老帖子,但仍然相关。 我修改了makefile,这样链接后产生的二进制文件的名称为[filname].bin,而不是只有[filname]。然后我在gitignore中添加了*.bin文件。 这个例行程序满足了我的需要。
我不知道还有什么别的办法,只能把它们一个一个地加到。gitignore中。
一个粗略的测试方法是grep文件命令的输出:
find . \( ! -regex '.*/\..*' \) -type f | xargs -n 1 file | egrep "ASCII|text"
EDIT
为什么不直接将可执行文件命名为hello.bin呢?
在某些子目录中也可以忽略,而不仅仅是在根目录中:
# Ignore everything in a root
/*
# But not files with extension located in a root
!/*.*
# And not my subdir (by name)
!/subdir/
# Ignore everything inside my subdir on any level below
/subdir/**/*
# A bit of magic, removing last slash or changing combination with previous line
# fails everything. Though very possibly it just says not to ignore sub-sub-dirs.
!/subdir/**/
# ...Also excluding (grand-)children files having extension on any level
# below subdir
!/subdir/**/*.*
或者,如果你只想包含一些特定类型的文件:
/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
如果你想的话,它甚至可以像每个新子目录一样工作!:
/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
前导斜杠只在前两行中重要,在其他行中是可选的。在!/*/和!/subdir/中的尾斜杠也是可选的,但仅在这一行中。