Make .gitignore ignore everything except a few files

前端 未结 23 2612
无人共我
无人共我 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 <file>..." to include in what will be committed)
            .gitignore
            bin/sub/folder/path/script.sh
    
    Ignored files:
      (use "git add -f <file>..." 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)
    
    0 讨论(0)
  • 2020-11-22 00:48

    To ignore some files in a directory, you have to do this in the correct order:

    For example, ignore everything in folder "application" except index.php and folder "config" pay attention to the order.

    You must negate want you want first.

    FAILS

    application/*

    !application/config/*

    !application/index.php

    WORKS

    !application/config/*

    !application/index.php

    application/*

    0 讨论(0)
  • 2020-11-22 00:49

    The simplest way that I go about this is to force add a file. It will be accounted for in git even if it is buried or nested inside a git-ignored subdirectory tree.

    For example:

    x64 folder is excluded in .gitignore:

    x64/
    

    But you want to include the file myFile.py located in x64/Release/ directory. Then you have to:

    git add -f x64/Release/myFile.py
    

    You can do this for multiple files of files that match a pattern e.g.

    git add -f x64/Release/myFile*.py
    

    and so on.

    0 讨论(0)
  • 2020-11-22 00:50

    An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources.

    # Ignore everything
    *
    
    # But not these files...
    !.gitignore
    !script.pl
    !template.latex
    # etc...
    
    # ...even if they are in subdirectories
    !*/
    
    # if the files to be tracked are in subdirectories
    !*/a/b/file1.txt
    !*/a/b/c/*
    
    0 讨论(0)
  • 2020-11-22 00:50

    I seem to have found something that worked for me which no one else mentioned.

    # Ignore everything
    *
    
    # But not these files...
    !.gitignore
    !script.pl
    !template.latex
    # etc...
    
    # And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
    !subdir/
    !subdir/**/*
    

    Basically, it seems to negate a sub-directory from being ignored, you have to have two entries, one for the sub-directory itself !subdir/ and then another one which expands to all files and folders under it !subdir/**/*

    0 讨论(0)
  • 2020-11-22 00:52
    # ignore these
    *
    # except foo
    !foo
    
    0 讨论(0)
提交回复
热议问题