How to Add Linux Executable Files to .gitignore?

37,000

Solution 1

Can you ignore all, but source code files?

For example:

*
!*.c
!Makefile

Solution 2

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.

Solution 3

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.

Solution 4

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

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 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.

Share:
37,000

Related videos on Youtube

haziz
Author by

haziz

Updated on July 05, 2022

Comments

  • haziz
    haziz almost 2 years

    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.

    • Sam Watkins
      Sam Watkins over 9 years
      This is pretty much a duplicate of stackoverflow.com/questions/5711120/… Could merge them.
    • Alexander
      Alexander over 8 years
      No, there is a difference between executable files and binary files. I see the need for ignoring both executable scripts and binary files. I don't think this question is a duplicate.
  • Sam Watkins
    Sam Watkins over 9 years
    there is a similar answer using python and file(1), over here: stackoverflow.com/a/19749478/218294
  • René Nyffenegger
    René Nyffenegger over 6 years
    The reason I stumbled upon this question is exactly because it has that many of them.
  • G. Sliepen
    G. Sliepen about 3 years
    This throws the baby out with the bathwater. The point of git warning about untracked files is that you don't accidentily forget to git add new files. By just whitelisting .c files and Makefile, it now becomes easy to forget .h files, READMEs, and other files that might be important. I would keep the .gitignore file more conservative, and only add what you know for sure should be ignored.
  • leftaroundabout
    leftaroundabout almost 3 years
    I supposed it does work well. It is nonstandard though, and doesn't work as well as a convention to put all such files in a dedicated directory.
  • Mahi
    Mahi over 2 years
    although for me this answer works, Everyone should understand well what @G.Sliepen is pointing. The use of .gitignore is to blacklist not whitelist files.