IT박스

바이너리 파일이없는 gitignore

itboxs 2020. 7. 3. 20:41
반응형

바이너리 파일이없는 gitignore


어떻게 바이너리 파일은 무시 될 수있다 git사용하여 .gitignore파일을?

예:

$ g++ hello.c -o hello

"hello"파일은 이진 파일입니다. git이 파일을 무시?


# 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

같은 것을 추가하십시오

*.o

.gitignore 파일에서 repo의 루트에 배치하십시오 (또는 원하는 하위 디렉토리에 배치 할 수 있습니다-해당 레벨에서 적용됩니다).

편집하다:

확장명이없는 바이너리의 경우 bin/또는 다른 폴더 에 배치하는 것이 좋습니다 . 결국 내용 유형에 따른 무시는 없습니다.

당신은 시도 할 수 있습니다

*
!*.*

그러나 그것은 완전하지 않습니다.


모든 실행 파일을 .gitignore(당신의 질문에서 판단하는 "이진 파일"이라고 의미 할 수 있음)에 추가하려면 다음을 사용할 수 있습니다

find . -executable -type f >>.gitignore

에서 행 순서를 신경 쓰지 않으면 다음 명령 .gitignore으로을 업데이트하여 .gitignore중복을 제거하고 알파벳 순서를 그대로 유지하십시오.

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

읽기 위해 .gitignore파일을 열기 전에 파일을 자르기 때문에 출력을로 직접 파이프 할 수는 없습니다 cat. 또한 \! -regex '.*/.*/.*'하위 디렉토리에 실행 파일을 포함시키지 않으려는 경우 옵션 으로 추가 할 수 있습니다 .


바이너리를 사용하는 가장 좋은 방법은 표준 패턴으로 쉽게 필터링 할 수있는 확장 기능을 제공하거나 디렉토리 수준에서 필터링 할 수있는 디렉토리에 넣는 것입니다.

확장 제안은 기본적으로 기본적으로 필요하기 때문에 확장 제안은 Windows에서 더 적용 가능하지만 Unix에서는 실행 바이너리에서 확장을 사용하거나 사용하지 않을 수 있습니다. 이 경우 bin / 폴더 bin/에 넣고 .gitignore에 추가 할 수 있습니다.

매우 구체적인 소규모 예제에서는 hello.gitignore를 넣을 수 있습니다 .


당신은 당신의 시도 할 수 있습니다 .gitignore:

*
!*.c

이 방법에는 많은 단점이 있지만 소규모 프로젝트에는 적합합니다.


makefile을 사용하는 경우 make 규칙을 수정하여 새 바이너리 이름을 .gitignore 파일에 추가 할 수 있습니다.

다음은 작은 Haskell 프로젝트를위한 Makefile의 예입니다.

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.


Binary files are often without extensions. If this is your case try this:

*
!/**/
!*.*

REF: https://stackoverflow.com/a/19023985/1060487


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.


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.


If you follow these commands on your .gitignore file and files still seems to appear you may want to try:

git rm --cached FILENAME

After that, add your .gitignore, commit and push. Took me 40 minutes to understand that, hope this helps to newbies like me


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?


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


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

/bin
/pkg

It ignore all the compiled developments, currently.


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


Building on VenomVendors answer

# Ignore all
*

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

# Unignore Makefiles recursively
!**/Makefile

# other .gitignore rules...

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"

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.

참고URL : https://stackoverflow.com/questions/5711120/gitignore-without-binary-files

반응형