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

发布于 2024-12-17 08:36:09 字数 153 浏览 5 评论 0原文

如何将 Linux 可执行文件添加到 .gitignore 而不给它们明确的扩展名,也不将它们放在特定的或 /bin 目录中?大多数的名称与编译它们的 C 文件相同,但不带 .c 扩展名。

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.

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

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

发布评论

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

评论(11

溇涏 2024-12-24 08:36:09

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

例如:

*
!*.c
!Makefile

Can you ignore all, but source code files?

For example:

*
!*.c
!Makefile
指尖上得阳光 2024-12-24 08:36:09

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

Most developers usually have a build directory 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.

十雾 2024-12-24 08:36:09

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

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

说不完的你爱 2024-12-24 08:36:09

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

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"

功能:

  • 从顶级文件夹开始查找(可能是一个错误功能!)
  • 忽略 ELF 文件,不包括可以使用通用规则
  • 保留 忽略的 .o 和 .so 文件.gitignore 中的现有条目而不重复它们

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

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

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

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

This single-script version is here: 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

天涯离梦残月幽梦 2024-12-24 08:36:09

忽略所有c、c++可执行文件将其保存在.gitignore文件中这将忽略所有没有文件扩展名的文件,因为Linux中通常没有c/c++可执行文件的文件扩展名

*
!*.*
!*/

ignore all c, c++ executable files save this in .gitignore file this will ignore all files without file extension cause generally there is no file extension of c/c++ executables in linux

*
!*.*
!*/
我是有多爱你 2024-12-24 08:36:09

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

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

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

没有自动方法可以仅忽略可执行文件,因此您始终必须手动管理该文件。

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

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 .gitignore file is already sorted. The sed part just strips the leading ./ that find generates.

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

柏拉图鍀咏恒 2024-12-24 08:36:09

如果你碰巧有一个包含大量可执行文件的“项目”,例如,一个研究项目,其中有很多编译后的小练习,你可以使用这个单行代码来更新你的 .gitignore:

for f in $(find . -perm /111 -type f | grep -v '.git' | sed 's#^./##' | sort -u); do grep -q "$f" .gitignore || echo "$f" >> .gitignore ; done

If you happen to have a "project" with quite a lot of executables, for instance, a study project where have a lot of small exercises that result get compiled, you can use this single-liner to update your .gitignore:

for f in $(find . -perm /111 -type f | grep -v '.git' | sed 's#^./##' | sort -u); do grep -q "$f" .gitignore || echo "$f" >> .gitignore ; done
み格子的夏天 2024-12-24 08:36:09

我不知道,你可以在 Makefile 中添加一两条规则;类似于:

$(TARGET): $(TARGET).o
    $(CC) -ggdb -o $@ $^
    @grep $@ .gitignore > tmp || true
    @[ -s tmp ] || echo $@ >> .gitignore; rm tmp

如果可执行文件尚不存在,它将把可执行文件附加到 .gitignore 中。

I don't know, you can add a rule or two, in the Makefile; something like:

$(TARGET): $(TARGET).o
    $(CC) -ggdb -o $@ $^
    @grep $@ .gitignore > tmp || true
    @[ -s tmp ] || echo $@ >> .gitignore; rm tmp

which will append the executable to the .gitignore if it's not already there.

如日中天 2024-12-24 08:36:09

所有未跟踪和未忽略的文件或存储库:

git ls-files --others --exclude-standard

可执行文件:

find -type f -executable 

将任何也可执行的未跟踪的未忽略文件添加到 .gitignore:

git ls-files -z --exclude-standard --others \
| find -files0-from /dev/stdin -type f -executable > new.$
[ -s new.$ ] && ( cat new.$ >>.gitignore; rm new.$; git add .gitignore )

或者实际上,只是尽量不要造成如此荒谬的混乱。将可执行文件放入 bin/ 中并完成操作。

All untracked and unignored files or repositories:

git ls-files --others --exclude-standard

Executable files:

find -type f -executable 

add any untracked unignored files that are also executable to .gitignore:

git ls-files -z --exclude-standard --others \
| find -files0-from /dev/stdin -type f -executable > new.$
[ -s new.$ ] && ( cat new.$ >>.gitignore; rm new.$; git add .gitignore )

or really, just try not to make such a ridiculous mess. Put executables in a bin/ and be done with it.

一紙繁鸢 2024-12-24 08:36:09

我也多次试图弄清楚这个问题。想分享一个长期存在的解决方案。虽然我是从 Go 的角度写这篇文章的,但除此之外我相信这是普遍适用的。

首先,观察结果是,典型项目中的可执行(和/或二进制)文件要少得多,然后是其他所有文件。另外,值得注意的是,显式地将源文件标记为“不忽略”并忽略其他所有内容的方法效果不佳,因为我们希望我们的注释和文本文件也被 git 编辑。

因此,解决方案是制定一个约定,即可执行文件具有 .e 后缀,并且 .gitignore 中具有 *.e。

它很简单,而且效果很好。

I too was trying to figure out this very question several times. Wanna share a solution that stuck for long. Though I am writing this from the Go perspective, but otherwise I believe this is generally applicable.

First, the observation is that there are much fewer executables (and/or binary) files in typical project, then everything else. Also, worth noting, that the approach to instead explicitly mark source files "to not ignore", and ignore everything else, doesn't work well, because we want our comments and text files, e.g., be git'ed too.

So the solution was to make a convention that executables have .e suffix, and .gitignore has *.e in it.

It is simple, and works well.

稍尽春風 2024-12-24 08:36:09

这可能会有所帮助:

file * | sed -n '/text/d;/exec/s/^\(.*\):.*/\1/p'

基本上它会搜索所有非文本文件(即二进制文件)的可执行文件。
如果需要 ELF 二进制文件,可能是这样的:

file * | sed -n '/text/d;/ELF.*exec/s/^\(.*\):.*/\1/p'

并将结果放入 .gitignore

This could help:

file * | sed -n '/text/d;/exec/s/^\(.*\):.*/\1/p'

Basically it search all executable that are not text file, hence binaries.
If ELF binaries are wanted, it could be something like:

file * | sed -n '/text/d;/ELF.*exec/s/^\(.*\):.*/\1/p'

And put the result in .gitignore

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