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’d include them in the project explicitly. gitignore. It’s not really elegant, but I’m guessing you don’t have that many of them in your project.

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"


This single-script version is here:

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 method of generating differences in your favor. gitignore from all executable files in the current directory in one go:

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

This creates a diff, ensuring that any manual modifications to the file are preserved. This implies that your.gitignore file has already been sorted. The sed portion just removes the leading./ that find produces.

Because there’s no way to automatically disregard only executable files, you’ll have to manually handle the file.

Answered by Mark Fisher

Post is based on