Make .gitignore ignore everything except a few files

前端 未结 23 2613
无人共我
无人共我 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:54

    I know I am late for the party,but here's my answer.

    As @Joakim said, to ignore a file, you can use something like below.

    # Ignore everything
    *
    
    # But not these files...
    !.gitignore
    !someFile.txt
    

    but if the file is with in nested directories, it's little bit hard to manually write the rules.

    For example, if we want to skip all files in a git project, but not a.txt which is located in aDir/anotherDir/someOtherDir/aDir/bDir/cDir. Then, our .gitignore will be something like this

    # Skip all files
    *
    
    # But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
    !aDir/
    aDir/*
    !aDir/anotherDir/
    aDir/anotherDir/*
    !aDir/anotherDir/someOtherDir/
    aDir/anotherDir/someOtherDir/*
    !aDir/anotherDir/someOtherDir/aDir/
    aDir/anotherDir/someOtherDir/aDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/
    aDir/anotherDir/someOtherDir/aDir/bDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
    aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
    

    The above given .gitignore file will skip all dirs and files except !aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

    As you may have noted, it's hard to define those rules.

    To solve this hurdle, I've created a simple console application named git-do-not-ignore which will generate the rules for you. I've hosted the project in github with detailed instruction.

    Usage Example

    java -jar git-do-not-ignore.jar "aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt"
    

    Output

    !aDir/
    aDir/*
    !aDir/anotherDir/
    aDir/anotherDir/*
    !aDir/anotherDir/someOtherDir/
    aDir/anotherDir/someOtherDir/*
    !aDir/anotherDir/someOtherDir/aDir/
    aDir/anotherDir/someOtherDir/aDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/
    aDir/anotherDir/someOtherDir/aDir/bDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
    aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
    !aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt
    

    There's also a simple web version available here

    Thank you.

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

    Simple solution if you need to ignore everything except few files and few root folders:

    /*
    !.gitignore
    !showMe.txt
    !my_visible_dir
    

    The magic is in /* (as described above) it ignores everything in the (root) folder BUT NOT recursively.

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

    I had a problem with subfolder.

    Does not work:

    /custom/*
    !/custom/config/foo.yml.dist
    

    Works:

    /custom/config/*
    !/custom/config/foo.yml.dist
    
    0 讨论(0)
  • 2020-11-22 00:56

    I also had some issues with the negation of single files. I was able to commit them, but my IDE (IntelliJ) always complained about ignored files, which are tracked.

    git ls-files -i --exclude-from .gitignore
    

    Displayed the two files, which I've excluded this way:

    public/
    !public/typo3conf/LocalConfiguration.php
    !public/typo3conf/PackageStates.php
    

    In the end, this worked for me:

    public/*
    !public/typo3conf/
    public/typo3conf/*
    !public/typo3conf/LocalConfiguration.php
    !public/typo3conf/PackageStates.php
    

    The key was the negation of the folder typo3conf/ first.

    Also, it seems that the order of the statements doesn't matter. Instead, you need to explicitly negate all subfolders, before you can negate single files in it.

    The folder !public/typo3conf/ and the folder contents public/typo3conf/* are two different things for .gitignore.

    Great thread! This issue bothered me for a while ;)

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

    A little more specific:

    Example: Ignore everything in webroot/cache - but keep webroot/cache/.htaccess.

    Notice the slash (/) after the cache folder:

    FAILS

    webroot/cache*
    !webroot/cache/.htaccess
    

    WORKS

    webroot/cache/*
    !webroot/cache/.htaccess
    
    0 讨论(0)
  • 2020-11-22 00:59

    You can use git config status.showUntrackedFiles no and all untracked files will be hidden from you. See man git-config for details.

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