Make .gitignore ignore everything except a few files

前端 未结 23 2590
无人共我
无人共我 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 00:47

    Had the similar issue as OP but none of top 10 upvoted answer actually worked.
    I finally found out the following

    Wrong syntax :

    *
    !bin/script.sh
    

    Correct syntax :

    *
    !bin
    !bin/script.sh
    

    Explanation from gitignore man page :

    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.

    Which means that "Wrong syntax" above is wrong because bin/script.sh cannot be reincluded as bin/ is ignored. That's all.

    Extended example :

    $ tree .

    .
    ├── .gitignore
    └── bin
        ├── ignore.txt
        └── sub
            └── folder
                └── path
                    ├── other.sh
                    └── script.sh
    

    $ cat .gitignore

    *
    !.gitignore
    !bin
    !bin/sub
    !bin/sub/folder
    !bin/sub/folder/path
    !bin/sub/folder/path/script.sh
    

    $ git status --untracked-files --ignored

    On branch master
    
    No commits yet
    
    Untracked files:
      (use "git add ..." to include in what will be committed)
            .gitignore
            bin/sub/folder/path/script.sh
    
    Ignored files:
      (use "git add -f ..." to include in what will be committed)
            bin/ignore.txt
            bin/sub/folder/path/other.sh
    
    nothing added to commit but untracked files present (use "git add" to track)
    

提交回复
热议问题