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 04:46

If you're using a makefile, you could try modifying your make rules to append the names of new binaries to your .gitignore file.

Here's an example Makefile for a small Haskell project;

all: $(patsubst %.hs, %, $(wildcard *.hs))

%: %.hs
    ghc $^
    grep -xq "$@" .gitignore || echo $@ >> .gitignore

This makefile defines a rule for creating executables out of Haskell code. After ghc is invoked, we check the .gitignore to see if the binary is already in it. If it isn't, we append the name of the binary to the file.

查看更多
爷的心禁止访问
3楼-- · 2019-01-07 04:49

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.

查看更多
\"骚年 ilove
4楼-- · 2019-01-07 04:51

I don't know any other solution but adding them one by one to .gitignore.

A crude way to test is to grep the file command's output:

find . \( ! -regex '.*/\..*' \) -type f | xargs -n 1 file | egrep "ASCII|text"

EDIT

Why don't you simply name you executable hello.bin?

查看更多
Bombasti
5楼-- · 2019-01-07 04:51

Just add hello or /hello to your .gitignore. Either works.

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-07 04:52
# Ignore all
*

# Unignore all with extensions
!*.*

# Unignore all dirs
!*/

### Above combination will ignore all files without extension ###

# Ignore files with extension `.class` & `.sm`
*.class
*.sm

# Ignore `bin` dir
bin/
# or
*/bin/*

# Unignore all `.jar` in `bin` dir
!*/bin/*.jar

# Ignore all `library.jar` in `bin` dir
*/bin/library.jar

# Ignore a file with extension
relative/path/to/dir/filename.extension

# Ignore a file without extension
relative/path/to/dir/anotherfile
查看更多
啃猪蹄的小仙女
7楼-- · 2019-01-07 04:53

I created a .gitignore file with two entries in GOPATH directory.

/bin
/pkg

It ignore all the compiled developments, currently.

查看更多
登录 后发表回答