How to Add Linux Executable Files to .gitignore?

C++CLinuxGitGitignore

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

Can you ignore all, but source code files?

For example:

*
!*.c
!Makefile

Solution 2 - C++

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 - C++

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 - C++

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 - C++

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.

Solution 6 - C++

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

Solution 7 - C++

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.

Solution 8 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhazizView Question on Stackoverflow
Solution 1 - C++HavokView Answer on Stackoverflow
Solution 2 - C++ZaxterView Answer on Stackoverflow
Solution 3 - C++BenView Answer on Stackoverflow
Solution 4 - C++Sam WatkinsView Answer on Stackoverflow
Solution 5 - C++Mark FisherView Answer on Stackoverflow
Solution 6 - C++nad2000View Answer on Stackoverflow
Solution 7 - C++latitovView Answer on Stackoverflow
Solution 8 - C++bazView Answer on Stackoverflow