没有二进制文件的gitignore

Jua*_*blo 131 git gitignore

如何在git使用该.gitignore文件时忽略二进制文件?

例:

$ g++ hello.c -o hello
Run Code Online (Sandbox Code Playgroud)

"hello"文件是二进制文件.可以git忽略这个文件吗?

Ven*_*dor 119

# 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
Run Code Online (Sandbox Code Playgroud)

  • 看来,当我发现有多层目录时,这种方法无法正常工作...... (4认同)
  • 这将忽略 makefile (4认同)

man*_*lds 42

添加类似的东西

*.o
Run Code Online (Sandbox Code Playgroud)

在.gitignore文件中并将其放在您的仓库的根目录(或者您可以放置​​在您想要的任何子目录中 - 它将从该级别应用)并将其签入.

编辑:

对于没有扩展名的二进制文件,最好将它们放在bin/其他文件夹中.毕竟没有基于内容类型的忽略.

你可以试试

*
!*.*
Run Code Online (Sandbox Code Playgroud)

但这不是万无一失的.

  • 可执行文件通常没有扩展名.对于`gcc`传递`-o $ @`创建的文件,我正在尝试做同样的事情. (8认同)

ick*_*cks 27

要将所有可执行文件附加到您的.gitignore(您可能通过"二进制文件"来判断您的问题),您可以使用

find . -executable -type f >>.gitignore
Run Code Online (Sandbox Code Playgroud)

如果您不关心您的行的排序.gitignore,您也可以.gitignore使用以下命令更新您的命令,该命令也会删除重复项并保持字母顺序不变.

T=$(mktemp); (cat .gitignore; find . -executable -type f | sed -e 's%^\./%%') | sort | uniq >$T; mv $T .gitignore
Run Code Online (Sandbox Code Playgroud)

请注意,您无法直接管道输出.gitignore,因为这会在cat打开文件进行读取之前截断文件.此外,您可能希望添加\! -regex '.*/.*/.*'一个选项,以查找是否要在子目录中包含可执行文件.


And*_*ite 23

使用二进制文件的最佳选择是为它们提供一个扩展,您可以使用标准模式轻松过滤掉,或者将它们放入可以在目录级别过滤掉的目录中.

扩展建议更适用于Windows,因为扩展是标准的,基本上是必需的,但在Unix中,您可能会也可能不会在可执行二进制文件上使用扩展.在这种情况下,您可以将它们放在bin /文件夹中,然后添加bin/到.gitignore中.

在您非常具体的小范围示例中,您可以放入hello.gitignore.


And*_*kou 15

你可以试试.gitignore:

*
!*.c
Run Code Online (Sandbox Code Playgroud)

这种方法有许多缺点,但对小型项目来说是可以接受的.

  • 如果你最不列出主要的缺点,那将是很好的 (3认同)

ved*_*ntk 13

如果您正在使用makefile,则可以尝试修改make规则,以将新二进制文件的名称附加到.gitignore文件中.

这是一个小型Haskell项目的Makefile示例;

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

%: %.hs
    ghc $^
    grep -xq "$@" .gitignore || echo $@ >> .gitignore
Run Code Online (Sandbox Code Playgroud)

此makefile定义了一个用于从Haskell代码创建可执行文件的规则.在调用ghc之后,我们检查.gitignore以查看二进制文件是否已经在其中.如果不是,我们将二进制文件的名称附加到文件中.


mat*_*yer 7

Binary files are often without extensions. If this is your case try this:

*
!/**/
!*.*
Run Code Online (Sandbox Code Playgroud)

REF: /sf/answers/1331678981/


Tam*_*inn 6

最具体的 gitignore 行通常是最好的,这样您就不会意外地从 git 中隐藏文件,这会在其他人检查您的提交时导致难以诊断的问题。因此,我特别建议只 gitignoring 目录根目录中名为 hello 的文件。为此,请添加:

/hello
Run Code Online (Sandbox Code Playgroud)

.gitignore存储库根目录中的文件。(也可以在存储库的其他位置拥有 .gitignore 文件,当您的 git 存储库包含多个项目时这是有意义的,您可能希望稍后移动这些项目,或者有一天拆分到他们自己的存储库中。)

但是,如果您真的想忽略所有没有扩展名的文件,您可以使用:

/[^\.]*
Run Code Online (Sandbox Code Playgroud)

甚至不那么具体:

[^\.]*
Run Code Online (Sandbox Code Playgroud)

解释:

/  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"
Run Code Online (Sandbox Code Playgroud)


Sha*_*etz 6

如果您在 .gitignore 文件上执行了这些命令,并且文件似乎仍然出现,您可能需要尝试:

git rm --cached FILENAME
Run Code Online (Sandbox Code Playgroud)

之后,添加您的 .gitignore,提交并推送。我花了40分钟才明白这一点,希望这对像我这样的新手有帮助


小智 5

这是另一个使用文件的解决方案。这样,可执行脚本将不会以gitignore结尾。您可能需要更改文件输出的解释方式以匹配您的系统。然后,您可以设置一个预提交钩子,以在每次提交时调用此脚本。

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)
Run Code Online (Sandbox Code Playgroud)


sha*_*sir 5

一种在某些子目录中也忽略的方法,而不仅仅是在根目录中:

# 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/**/*.*
Run Code Online (Sandbox Code Playgroud)

或者,如果您只想包含某些特定类型的文件:

/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
Run Code Online (Sandbox Code Playgroud)

如果你愿意的话,它甚至可以像每个新子目录一样工作!:

/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
Run Code Online (Sandbox Code Playgroud)

前导斜杠仅在前两行中很重要,在其他行中是可选的。!/*/and中的尾部斜杠!/subdir/也是可选的,但仅限于这一行。