gitignore without binary files

2019-01-07 04:27发布

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 ?

标签: git gitignore
17条回答
唯我独甜
2楼-- · 2019-01-07 05:06

Building on VenomVendors answer

# Ignore all
*

# Unignore all files with extensions recursively
!**/*.*

# Unignore Makefiles recursively
!**/Makefile

# other .gitignore rules...
查看更多
男人必须洒脱
3楼-- · 2019-01-07 05:07

You may try in your .gitignore:

*
!*.c

This approach has many disadvantages, but it's acceptable for small projects.

查看更多
够拽才男人
4楼-- · 2019-01-07 05:07

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.

查看更多
我只想做你的唯一
5楼-- · 2019-01-07 05:08

To append all executables to your .gitignore (which you probably mean by "binary file" judging from your question), you can use

find . -executable -type f >>.gitignore

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

T=$(mktemp); (cat .gitignore; find . -executable -type f | sed -e 's%^\./%%') | sort | uniq >$T; mv $T .gitignore

Note, that you cannot pipe output directly to .gitignore, because that would truncate the file before cat 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.

查看更多
爷的心禁止访问
6楼-- · 2019-01-07 05:10

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.

查看更多
登录 后发表回答