gitignore binary files that have no extension

前端 未结 18 1122
没有蜡笔的小新
没有蜡笔的小新 2020-11-28 02:14

How can binary files be ignored in git using the .gitignore file?

Example:

$ g++ hello.c -o hello

The

相关标签:
18条回答
  • 2020-11-28 02:28

    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.

    0 讨论(0)
  • 2020-11-28 02:30

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

    0 讨论(0)
  • 2020-11-28 02:32

    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.

    0 讨论(0)
  • 2020-11-28 02:32

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

    0 讨论(0)
  • Building on VenomVendors answer

    # Ignore all
    *
    
    # Unignore all files with extensions recursively
    !**/*.*
    
    # Unignore Makefiles recursively
    !**/Makefile
    
    # other .gitignore rules...
    
    0 讨论(0)
  • 2020-11-28 02:33

    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.

    0 讨论(0)
提交回复
热议问题