How can binary files be ignored in git
using the .gitignore
file?
Example:
$ g++ hello.c -o hello
The
Add something like
*.o
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.
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.
import subprocess, os
git_root = subprocess.check_output(['git', 'root']).decode("UTF-8").strip()
exes = []
cut = len(git_root)
for root, dirnames, filenames in os.walk(git_root+"/src/"):
for fname in filenames:
f = os.path.join(root,fname)
if not os.access(f,os.X_OK):
continue
ft = subprocess.check_output(['file', f]).decode("UTF-8")
if 'ELF' in ft and 'executable' in ft:
exes.append(f[cut:])
gifiles = [ str.strip(a) for a in open(git_root + "/.gitignore").readlines() ]
gitignore=frozenset(exes+gifiles)
with open(git_root+"/.gitignore", "w") as g:
for a in sorted(gitignore):
print(a, file=g)
A way to also ignore in some subdir, not only in a root:
# Ignore everything in a root
/*
# But not files with extension located in a root
!/*.*
# And not my subdir (by name)
!/subdir/
# Ignore everything inside my subdir on any level below
/subdir/**/*
# A bit of magic, removing last slash or changing combination with previous line
# fails everything. Though very possibly it just says not to ignore sub-sub-dirs.
!/subdir/**/
# ...Also excluding (grand-)children files having extension on any level
# below subdir
!/subdir/**/*.*
Or, if you want to include only some specific types of files:
/*
!/*.c
!/*.h
!/subdir/
/subdir/**/*
!/subdir/**/
!/subdir/**/*.c
!/subdir/**/*.h
Seems it may even also work like for every new subdirectory if you want!:
/*
!/*.c
!/*.h
!/*/
/*/**/*
!/*/**/
!/*/**/*.c
!/*/**/*.h
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.
Your best bet with binaries is to either give them an extension that you can easily filter out with a standard pattern, or put them into directories that you can filter out at the directory level.
The extension suggestion is more applicable in Windows, because extensions are standard and basically required, but in Unix, you may or may not use extensions on your executable binaries. In this case, you can put them in a bin/ folder, and add bin/
to your .gitignore.
In your very specific, small-scope example, you can just put hello
in your .gitignore.
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.
Add the following to your .gitignore file:
[^\.]*
Explanation:
[] encloses a character class, e.g. [a-zA-Z] means "any letter".
^ means "not"
\. means a literal dot - without the backslash . means "any character"
* means "any number of these characters"