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 ?
Building on VenomVendors answer
You may try in your
.gitignore
:This approach has many disadvantages, but it's acceptable for small projects.
The
.gitignore
mechanism works only based on file names, not on file contents. Being a binary file is a property of the content, hence you can't ask git ignore binary files directly, but only to ignore them by name (and as other suggested, you can either add all binary file names to your.gitignore
or use an appropriate naming convention).The fact that
.gitignore
works on file names is an important property performance-wise: Git only needs to list files, but not to open and read them to know which files to ignore. In other words, Git would be terribly slow if you could ask it to ignore files based on their contents.To append all executables to your
.gitignore
(which you probably mean by "binary file" judging from your question), you can useIf you don't care about ordering of lines in your
.gitignore
, you could also update your.gitignore
with the following command which also removes duplicates and keeps alphabetic ordering intact.Note, that you cannot pipe output directly to
.gitignore
, because that would truncate the file beforecat
opens it for reading. Also, you might want to add\! -regex '.*/.*/.*'
as an option to find if you do not want to include executable files in subdirectories.Add something like
in the .gitignore file and place it at the root of your repo ( or you can place in any sub directory you want - it will apply from that level on ) and check it in.
Edit:
For binaries with no extension, you are better off placing them in
bin/
or some other folder. Afterall there is no ignore based on content-type.You can try
but that is not foolproof.