gitignore 没有扩展名的二进制文件

发布于 2024-11-01 21:01:46 字数 144 浏览 5 评论 0原文

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

示例:

$ g++ hello.c -o hello

“hello”文件是一个二进制文件。 git 可以忽略这个文件吗?

How can binary files be ignored in git using the .gitignore file?

Example:

$ g++ hello.c -o hello

The "hello" file is a binary file. Can git ignore this file ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

故事↓在人 2024-11-08 21:01:46
# 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
# 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
执手闯天涯 2024-11-08 21:01:46

添加类似

*.o

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

编辑:

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

您可以尝试

*
!*.*

,但这并非万无一失。

Add something like

*.o

in the .gitignore file and place it at the root of your repo ( or you can place in any sub directory you want - it will apply from that level on ) and check it in.

Edit:

For binaries with no extension, you are better off placing them in bin/ or some other folder. Afterall there is no ignore based on content-type.

You can try

*
!*.*

but that is not foolproof.

久而酒知 2024-11-08 21:01:46

要将所有可执行文件附加到您的 .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 '.*/.*/.*' 作为一个选项来查找是否不想在子目录中包含可执行文件。

To append all executables to your .gitignore (which you probably mean by "binary file" judging from your question), you can use

find . -executable -type f >>.gitignore

If you don't care about ordering of lines in your .gitignore, you could also update your .gitignore with the following command which also removes duplicates and keeps alphabetic ordering intact.

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

Note, that you cannot pipe output directly to .gitignore, because that would truncate the file before cat opens it for reading. Also, you might want to add \! -regex '.*/.*/.*' as an option to find if you do not want to include executable files in subdirectories.

非要怀念 2024-11-08 21:01:46

二进制文件通常没有扩展名。如果这是您的情况,请尝试以下操作:

*
!/**/
!*.*

参考:https://stackoverflow.com/a/19023985/1060487

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

*
!/**/
!*.*

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

墨小墨 2024-11-08 21:01:46

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

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

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

Your best bet with binaries is to either give them an extension that you can easily filter out with a standard pattern, or put them into directories that you can filter out at the directory level.

The extension suggestion is more applicable in Windows, because extensions are standard and basically required, but in Unix, you may or may not use extensions on your executable binaries. In this case, you can put them in a bin/ folder, and add bin/ to your .gitignore.

In your very specific, small-scope example, you can just put hello in your .gitignore.

逐鹿 2024-11-08 21:01:46

您可以在 .gitignore 中尝试:

*
!*.c

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

You may try in your .gitignore:

*
!*.c

This approach has many disadvantages, but it's acceptable for small projects.

走野 2024-11-08 21:01:46

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

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

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

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

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

If you're using a makefile, you could try modifying your make rules to append the names of new binaries to your .gitignore file.

Here's an example Makefile for a small Haskell project;

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

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

This makefile defines a rule for creating executables out of Haskell code. After ghc is invoked, we check the .gitignore to see if the binary is already in it. If it isn't, we append the name of the binary to the file.

温柔戏命师 2024-11-08 21:01:46

最具体的 gitignore 行通常是最好的,这样您就不会意外地从 git 中隐藏文件,这会在其他人检查您的提交时导致难以诊断的问题。因此,我特别建议 ​​gitignoring 只忽略目录根目录中名为 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"

The most specific gitignore line is normally the best, so that you don't accidentally hide files from git, which causes hard-to-diagnose problems when someone else checks out your commit. Hence, I recommend specifically gitignoring only the file called hello in the root of your directory. To do that, add:

/hello

to the .gitignore file in the root of your repository. (It is also possible to have .gitignore files elsewhere in the repository, and this makes sense when your git repository contains multiple projects, which you might want to move around later, or split off into their own repository one day.)

However, if you really want to ignore all files that have no extension, you could use:

/[^\.]*

or even less specific:

[^\.]*

Explanation:

/  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"
萌︼了一个春 2024-11-08 21:01:46

这是使用文件的另一种解决方案。这样可执行脚本就不会出现在 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)

Here's another solution using file. This way executable scripts will not end up in gitignore. You may need to change how the output from file is interpreted to match your system. One could then set up a pre-commit hook to call this script each time you 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)
热鲨 2024-11-08 21:01:46

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

git rm --cached FILENAME

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

If you follow these commands on your .gitignore file and files still seems to appear you may want to try:

git rm --cached FILENAME

After that, add your .gitignore, commit and push.
Took me 40 minutes to understand that, hope this helps to newbies like me

你的笑 2024-11-08 21:01:46

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

# 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/ 中的尾部斜杠也是可选的,但仅限于这一行。

A way to also ignore in some subdir, not only in a root:

# 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/**/*.*

Or, if you want to include only some specific types of files:

/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h

Seems it may even also work like for every new subdirectory if you want!:

/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h

Leading slashes are important only in first two lines and optional in other. Tailing slash in !/*/ and !/subdir/ is also optional, but only in this line.

无远思近则忧 2024-11-08 21:01:46

基于 VenomVendors 答案

# Ignore all
*

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

# Unignore Makefiles recursively
!**/Makefile

# other .gitignore rules...

Building on VenomVendors answer

# Ignore all
*

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

# Unignore Makefiles recursively
!**/Makefile

# other .gitignore rules...
幽蝶幻影 2024-11-08 21:01:46

老线程,但仍然相关。
我更改了 makefile,因此链接后生成的二进制文件的名称为 [filname].bin,而不仅仅是 [filname]。然后我在 gitignore 中添加了 *.bin 文件。
这个例程满足了我的需求。

Old thread, but still relevant.
I changed the makefile so the resulting binary file after linking has the name [filname].bin instead of only [filname]. Then I added *.bin files in the gitignore.
This routine fulfill my needs.

静谧 2024-11-08 21:01:46

我不知道任何其他解决方案,只能将它们一一添加到 .gitignore 中。

一种粗略的测试方法是 grep 文件命令的输出:

find . \( ! -regex '.*/\..*' \) -type f | xargs -n 1 file | egrep "ASCII|text"

编辑

为什么不简单地将可执行文件命名为hello.bin

I don't know any other solution but adding them one by one to .gitignore.

A crude way to test is to grep the file command's output:

find . \( ! -regex '.*/\..*' \) -type f | xargs -n 1 file | egrep "ASCII|text"

EDIT

Why don't you simply name you executable hello.bin?

寄离 2024-11-08 21:01:46

只需将 hello/hello 添加到您的 .gitignore 中即可。要么有效。

Just add hello or /hello to your .gitignore. Either works.

Spring初心 2024-11-08 21:01:46

.gitignore 机制仅基于文件名称,而不是文件内容。作为二进制文件是内容的属性,因此您不能要求 git 直接忽略二进制文件,而只能按名称忽略它们(正如其他建议的那样,您可以将所有二进制文件名添加到您的 .git 中)。 gitignore 或使用适当的命名约定)。

.gitignore 作用于文件名这一事实在性能方面是一个重要的属性:Git 只需列出文件,而不需要打开和读取它们来知道要忽略哪些文件。换句话说,如果你可以要求 Git 根据文件内容忽略文件,它会非常慢。

The .gitignore mechanism works only based on file names, not on file contents. Being a binary file is a property of the content, hence you can't ask git ignore binary files directly, but only to ignore them by name (and as other suggested, you can either add all binary file names to your .gitignore or use an appropriate naming convention).

The fact that .gitignore works on file names is an important property performance-wise: Git only needs to list files, but not to open and read them to know which files to ignore. In other words, Git would be terribly slow if you could ask it to ignore files based on their contents.

草莓酥 2024-11-08 21:01:46

我创建了一个 .gitignore 文件,其中 GOPATH 目录中有两个条目。

/bin
/pkg

目前它忽略了所有已编译的开发。

I created a .gitignore file with two entries in GOPATH directory.

/bin
/pkg

It ignore all the compiled developments, currently.

放肆 2024-11-08 21:01:46

.gitignore 使用 glob 编程 来过滤文件,至少在 Linux 上是这样。

我即将在聚会上进行编码演讲,在准备过程中,我创建了一个目录,其中包含几个子目录,这些子目录根据我想要呈现的顺序命名:01_subject1、02_subject2、03_subject3。每个子目录都包含一个具有与语言相关的扩展名的源文件,根据惯例,该文件会编译为名称与不带扩展名的源文件名匹配的可执行文件。

我使用以下 .gitignore 行排除数字前缀目录中的已编译文件:

[0-9][0-9]_*/[!\.]*

根据我对文档,它不应该工作。拥有尾随星号应该会失败,因为它应该匹配任意数量的未指定字符,包括“.”。 + 扩展名。省略尾随星号应该会失败(并且确实如此),因为 [!\.] 仅匹配单个非句点字符。但是,我添加了尾随星号,就像我为正则表达式所做的那样,它起作用了。通过工作,我的意思是 git 注意到源文件的更改,但不注意到编译文件的存在或更改。

.gitignore uses glob programming to filter files, at least on Linux.

I am about to give a coding talk at a Meetup and, in preparation, I made a directory with several subdirectories that are named according to the order I want to present them: 01_subject1, 02_subject2, 03_subject3. Each subdirectory contains a source file with a language-dependent extension that compiles to an executable file whose name matches the source file name without the extension according to common practice.

I exclude the compiled files in the numeral-prefixed directories with the following .gitignore line:

[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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文