如何将 Linux 可执行文件添加到 .gitignore?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8237645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:31:22  来源:igfitidea点击:

How to Add Linux Executable Files to .gitignore?

c++clinuxgitgitignore

提问by haziz

How do you add linux executable files to .gitignore without giving them an explicit extension and without placing them in a specific or /bin directory? Most are named the same as the C file from which they were compiled without the ".c" extension.

如何将 linux 可执行文件添加到 .gitignore 而不给它们显式扩展名,也不将它们放在特定或 /bin 目录中?大多数名称与编译它们的 C 文件相同,没有“.c”扩展名。

回答by Ben

I would explicitly put them in the project .gitignore. It's not elegant, but I imagine your project doesn't have thatmany of them.

我会明确地将它们放在项目 .gitignore 中。这并不优雅,但我想你的项目没有那么多。

回答by Mark Fisher

A way of generating differences against your .gitignorein one go from all the executable files from current dir:

一种.gitignore从当前目录中的所有可执行文件一次性生成差异的方法:

find . -perm /111 -type f | sed 's#^./##' | sort | diff -u .gitignore -

this generates a diff meaning you don't lose any manual changes to the file. This assumes your .gitignorefile is already sorted. The sed part just strips the leading ./that find generates.

这会生成一个差异,这意味着您不会丢失对文件的任何手动更改。这假设您的.gitignore文件已经排序。sed 部分只是剥离了./find 生成的前导。

There's no automatic way to ignore only executable files, so you're always going to have to man-manage the file.

没有自动的方法可以只忽略可执行文件,因此您总是必须对文件进行人工管理。

回答by Havok

Can you ignore all, but source code files?

除了源代码文件之外,您可以忽略所有文件吗?

For example:

例如:

*
!*.c
!Makefile

回答by Sam Watkins

I wrote a script to automatically add ELF executables to .gitignore.

我编写了一个脚本来自动将 ELF 可执行文件添加到.gitignore.

git-ignore-elf:

git-ignore-elf

#!/bin/sh
set -eu
cd "$(git rev-parse --show-toplevel)"
file=.gitignore
new=$file.new.$$
(
if [ -e "$file" ]; then
    cat "$file"
fi
find . -name .git -prune -o -type f ! -name '*.o' ! -name '*.so' \
    -print0 | xargs -0 file | grep ': *ELF ' | sed 's/:.*//' |
sed 's,^./,,'
) | perl -ne 'print if !$already{$_}++' >"$new"
mv "$new" "$file"

Features:

特征:

  • starts looking from the top-level folder (might be a misfeature!)
  • ignores ELF files, excluding .o and .so files which can be ignored with a generic rule
  • preserves existing entries in .gitignore without duplicating them
  • 从顶级文件夹开始查看(可能是功能错误!)
  • 忽略 ELF 文件,不包括可以使用通用规则忽略的 .o 和 .so 文件
  • 保留 .gitignore 中的现有条目而不复制它们

This single-script version is here: http://sam.nipl.net/b/git-ignore-elf-1

这个单脚本版本在这里:http: //sam.nipl.net/b/git-ignore-elf-1

Here is a more modular version, which depends on other scripts (git-root, find-elf, uniqo) from the same place: http://sam.nipl.net/b/git-ignore-elf

这是一个更模块化的版本,它依赖于来自同一地方的其他脚本(git-root、find-elf、uniqo):http://sam.nipl.net/b/git-ignore-elf

回答by Zaxter

Most developers usually have a builddirectory in their project where the actual build process in run. So, all executables, .o, .so, .a, etc. are there and this build directory is added into the .gitignore.

大多数开发人员通常build在他们的项目中有一个目录,其中运行实际的构建过程。因此,所有可执行文件、.o.so.a等都在那里,并且这个构建目录被添加到.gitignore.