Coder Perfect

How do I add executable files from Linux to.gitignore?


Without giving them an explicit extension or placing them in a dedicated or /bin directory, how do you add linux executable files to.gitignore? Without the “.c” ending, most are named after the C file from which they were compiled.

Asked by haziz

Solution #1

Is it possible to ignore all but source code files?

For example:


Answered by Havok

Solution #2

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.

Answered by Ben

Solution #3

The actual build process is normally run in a build directory in most developers’ projects. So, all executables,.o,.so,.a, and other files are present, and this build directory has been added to the.gitignore file.

Answered by Zaxter

Solution #4

I built a method to add ELF executables to the.gitignore file automatically.


set -eu
cd "$(git rev-parse --show-toplevel)"
if [ -e "$file" ]; then
    cat "$file"
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: is the single-script version.

Here’s a more modular version that relies on other scripts from the same repository (git-root, find-elf, uniqo):

Answered by Sam Watkins

Solution #5

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 implies that your.gitignore file has already been sorted. The sed portion just removes the leading./ that find produces.

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

Answered by Mark Fisher

Post is based on