How can binary files be ignored in git
using the .gitignore
file?
Example:
$ g++ hello.c -o hello
The "hello" file is a binary file. Can git
ignore this file ?
How can binary files be ignored in git
using the .gitignore
file?
Example:
$ g++ hello.c -o hello
The "hello" file is a binary file. Can git
ignore this file ?
.gitignore uses glob programming to filter files, at least on Linux.
I am about to give a coding talk at a Meetup and, in preparation, I made a directory with several subdirectories that are named according to the order I want to present them: 01_subject1, 02_subject2, 03_subject3. Each subdirectory contains a source file with a language-dependent extension that compiles to an executable file whose name matches the source file name without the extension according to common practice.
I exclude the compiled files in the numeral-prefixed directories with the following .gitignore line:
[0-9][0-9]_*/[!\.]*
According to my understanding of the documentation, it shouldn't work. Having the trailing asterisk should fail because it should match any number of unspecified characters, including the '.' + extension. Omitting the trailing asterisk should fail (and does) because
[!\.]
matches only a single non-period character. However, I added the trailing asterisk, as I would for a regular expression, and it works. By work, I mean that git notices changes to the source file, but not the existence or changes to the compiled files.Here's another solution using file. This way executable scripts will not end up in gitignore. You may need to change how the output from file is interpreted to match your system. One could then set up a pre-commit hook to call this script each time you commit.
A way to also ignore in some subdir, not only in a root:
Or, if you want to include only some specific types of files:
Seems it may even also work like for every new subdirectory if you want!:
Leading slashes are important only in first two lines and optional in other. Tailing slash in
!/*/
and!/subdir/
is also optional, but only in this line.Old thread, but still relevant. I changed the makefile so the resulting binary file after linking has the name [filname].bin instead of only [filname]. Then I added *.bin files in the gitignore.
This routine fulfill my needs.
Binary files are often without extensions. If this is your case try this:
REF: https://stackoverflow.com/a/19023985/1060487
Add the following to your .gitignore file:
Explanation: