Make .gitignore ignore everything except a few files

前端 未结 23 2901
无人共我
无人共我 2020-11-22 00:14

I understand that a .gitignore file cloaks specified files from Git\'s version control. I have a project (LaTeX) that generates lots of extra files (.auth, .dvi, .pdf, logs,

23条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-22 01:00

    Gist

    # Ignore everything
    *
    
    # But not these files...
    !script.pl
    !template.latex
    

    And probably include:

    !.gitignore
    

    Reference

    From https://git-scm.com/docs/gitignore:

    An optional prefix "!" which negates the pattern; any matching file excluded by a previous pattern will become included again. It is not possible to re-include a file if a parent directory of that file is excluded. Git doesn’t list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined. Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

    ...

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

    $ cat .gitignore
    # exclude everything except directory foo/bar
    /*
    !/foo
    /foo/*
    !/foo/bar
    

提交回复
热议问题