gitignore没有二进制文件

问题描述 投票:123回答:18

如何二进制文件git使用.gitignore文件被忽略?

例:

$ g++ hello.c -o hello

在“你好”文件是一个二进制文件。 git可以忽略这个文件?

git gitignore
18个回答
111
投票
# 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

1
投票

我不知道任何其他解决方案,但将逐一到!/*/

测试粗的办法是到grep file命令的输出:

!/subdir/

编辑

你为什么不只是名字,你可执行.gitignore


1
投票

只需添加find . \( ! -regex '.*/\..*' \) -type f | xargs -n 1 file | egrep "ASCII|text" hello.bin您的.gitignore。无论是工作。


1
投票

旧线,但仍然具有现实意义。我改变了这样的makefile联后产生的二进制文件的名称为[FILNAME] .bin文件,而不是仅仅[FILNAME。然后我说在gitignore * .bin文件。 这个程序符合我的需求。


1
投票

如果您按照您的.gitignore文件和文件这些命令似乎仍然出现在你可能也想尝试:

hello

在此之后,添加你的.gitignore,提交和推动。我花了40分钟理解,希望这有助于新手和我一样


0
投票

我创建了一个.gitignore文件以在GOPATH目录的两个条目。

/hello

它忽略所有的编译的发展,目前。


0
投票

的.gitignore使用git rm --cached FILENAME 过滤的文件,至少在Linux上。

我现在要给在聚会编码谈话,并在准备,我做了一个目录与根据我想提出的顺序命名的几个子目录:01_subject1,02_subject2,03_subject3。每个子目录包含与语言相关的扩展,它编译成可执行文件的名字,而不根据惯例扩展名匹配的源文件名源文件。

我排除在下面的.gitignore线数字前缀目录编译后的文件:

/bin /pkg

根据我的文档的理解,它不应该工作。具有尾随星号应该会失败,因为它应该匹配任何数目的未指定字符,包括“” +扩展名。省略尾随星号应该失败(并执行),因为glob programming只有一个非周期字符匹配。不过,我加了星号结尾,我会为一个正则表达式,它的工作原理。通过工作,我的意思是git的通知更改源文件,但不存在或改变编译的文件。


0
投票

建立在[0-9][0-9]_*/[!\.]*

[!\.]

0
投票

以下添加到您的.gitignore文件:

VenomVendors answer

说明:

# Ignore all
*

# Unignore all files with extensions recursively
!**/*.*

# Unignore Makefiles recursively
!**/Makefile

# other .gitignore rules...

0
投票

[^\.]* 机制只基于文件名,而不是文件内容。作为一个二进制文件内容的属性,因此,你不能要求混帐忽略直接二进制文件,但只能通过名字忽视他们(和其他的建议,您可以所有二进制文件名添加到您的[] 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" 或使用适当的命名约定)。

.gitignore适用于文件名,这是一重要的性能表现明智:混帐只需要列出文件,但不能打开并阅读他们知道哪些文件被忽视。换句话说,Git的将是非常缓慢的,如果你可以问它忽略基于其内容的文件。


40
投票

添加类似

*.o

在的.gitignore文件,并把它放在你的回购的根(或者你可以在任何你想要的子目录的地方 - 这将适用于该级别),并检入。

编辑:

对于没有扩展名的二进制文件,你是关闭将它们放置在bin/或者其他文件夹好。毕竟没有忽略基于内容的类型。

你可以试试

*
!*.*

但事实并非万无一失。


26
投票

要追加的所有可执行文件到您的.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打开它进行阅读之前,将截断该文件。此外,您可能要\! -regex '.*/.*/.*'添加为一个选项找到,如果你不想包括子目录可执行文件。


23
投票

与二进制您最好的选择是,要么给他们的扩展,你可以很容易地筛选出具有标准模式,或把它们放到你可以在目录级过滤掉的目录。

扩展的建议是更适用于Windows系统,因为扩展的标准和基本要求,但在Unix中,你可能会或可能不会使用您的可执行的二进制文件的扩展。在这种情况下,你可以把它们放在一个斌/文件夹,并添加bin/到你的.gitignore。

在你非常具体的,小范围的例子,你可以把hello在你的.gitignore。


14
投票

您可以尝试在你的.gitignore

*
!*.c

这种方法有很多缺点,但它是小项目可以接受的。


12
投票

如果您使用的是生成文件,你可以尝试修改你的化妆规则的新的二进制文件的名称添加到您的.gitignore文件。

下面是一个小的Haskell项目的例子Makefile文件;

all: $(patsubst %.hs, %, $(wildcard *.hs))

%: %.hs
    ghc $^
    grep -xq "$@" .gitignore || echo $@ >> .gitignore

这个Makefile定义了用于创建可执行文件出来Haskell代码的规则。 GHC被调用后,我们检查的.gitignore,看是否二进制已在它。如果不是,我们追加二进制文件的名称。


4
投票

下面是使用文件中的另一个解决方案。这样,可执行脚本不会gitignore结束。您可能需要改变如何从文件的输出解释为匹配您的系统。然后,可以设置一个pre-commit钩子在每次提交的时候调用这个脚本。

import subprocess, os

git_root = subprocess.check_output(['git', 'root']).decode("UTF-8").strip()
exes = []
cut = len(git_root)

for root, dirnames, filenames in os.walk(git_root+"/src/"):
  for fname in filenames:
    f = os.path.join(root,fname)
    if not os.access(f,os.X_OK):
      continue

    ft = subprocess.check_output(['file', f]).decode("UTF-8")

    if 'ELF' in ft and 'executable' in ft:
      exes.append(f[cut:])

gifiles = [ str.strip(a) for a in open(git_root + "/.gitignore").readlines() ]
gitignore=frozenset(exes+gifiles)

with open(git_root+"/.gitignore", "w") as g:
  for a in sorted(gitignore):
    print(a, file=g)

4
投票

二进制文件通常不带扩展名。如果这是你的情况下,试试这个:

*
!/**/
!*.*

REF:https://stackoverflow.com/a/19023985/1060487


3
投票

一种方法也忽略了一些子目录,不仅在根:

# 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

领先的斜杠是在其他只在头两行重要的,可选的。在qazxsw POI和POI qazxsw尾随斜杠也是可选的,但只有在这行。

© www.soinside.com 2019 - 2024. All rights reserved.