如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
如何在git中使用.gitignore文件忽略二进制文件?
例子:
$ g++ hello.c -o hello
“hello”文件是二进制文件。git可以忽略这个文件吗?
当前回答
最具体的gitignore行通常是最好的,这样您就不会意外地对git隐藏文件,从而在其他人检查您的提交时导致难以诊断的问题。因此,我特别建议只对目录根目录中名为hello的文件进行命名。要做到这一点,添加:
/hello
到存储库根目录下的.gitignore文件。(也可以在存储库的其他地方有.gitignore文件,当你的git存储库包含多个项目时,这是有意义的,你可能想稍后移动这些项目,或者有一天将它们分离到自己的存储库中。)
然而,如果你真的想忽略所有没有扩展名的文件,你可以使用:
/[^\.]*
或者更不具体:
[^\.]*
解释:
/ starting with / means "only in the root of the repository"
[] encloses a character class, e.g. [a-zA-Z] means "any letter".
^ means "not"
\. means a literal dot - without the backslash . means "any character"
* means "any number of these characters"
其他回答
基于venomvendor的答案
# Ignore all
*
# Unignore all files with extensions recursively
!**/*.*
# Unignore Makefiles recursively
!**/Makefile
# other .gitignore rules...
只需添加hello或/hello到你的.gitignore。要么工作。
在某些子目录中也可以忽略,而不仅仅是在根目录中:
# 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/中的尾斜杠也是可选的,但仅在这一行中。
我在GOPATH目录中创建了一个包含两个条目的.gitignore文件。
/bin
/pkg
目前,它忽略所有已编译的开发。
如果您正在使用makefile,可以尝试修改make规则,将新二进制文件的名称附加到.gitignore文件中。
下面是一个小型Haskell项目的Makefile示例;
all: $(patsubst %.hs, %, $(wildcard *.hs))
%: %.hs
ghc $^
grep -xq "$@" .gitignore || echo $@ >> .gitignore
这个makefile定义了一个用Haskell代码创建可执行文件的规则。在ghc被调用之后,我们检查.gitignore,看看二进制文件是否已经在其中。如果不是,我们将二进制文件的名称附加到文件中。