How to Add Linux Executable Files to .gitignore?

2019-01-22 01:38发布

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.

5条回答
欢心
2楼-- · 2019-01-22 01:44

Can you ignore all, but source code files?

For example:

*
!*.c
!Makefile
查看更多
放我归山
3楼-- · 2019-01-22 01:56

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

查看更多
放荡不羁爱自由
4楼-- · 2019-01-22 01:59

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.

查看更多
姐就是有狂的资本
5楼-- · 2019-01-22 02:05

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.

查看更多
萌系小妹纸
6楼-- · 2019-01-22 02:07

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.

查看更多
登录 后发表回答