Make .gitignore ignore everything except a few files

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

    You want to use /* instead of * or */ in most cases

    Using * is valid, but it works recursively. It won't look into directories from then on out. People recommend using !*/ to whitelist directories again, but it's actually better to blacklist the highest level folder with /*

    # Blacklist files/folders in same directory as the .gitignore file
    /*
    
    # Whitelist some files
    !.gitignore
    !README.md
    
    # Ignore all files named .DS_Store or ending with .log
    **/.DS_Store
    **.log
    
    # Whitelist folder/a/b1/ and folder/a/b2/
    # trailing "/" is optional for folders, may match file though.
    # "/" is NOT optional when followed by a *
    !folder/
    folder/*
    !folder/a/
    folder/a/*
    !folder/a/b1/
    !folder/a/b2/
    !folder/a/file.txt
    
    # Adding to the above, this also works...
    !/folder/a/deeply
    /folder/a/deeply/*
    !/folder/a/deeply/nested
    /folder/a/deeply/nested/*
    !/folder/a/deeply/nested/subfolder
    

    The above code would ignore all files except for .gitignore, README.md, folder/a/file.txt, folder/a/b1/ and folder/a/b2/ and everything contained in those last two folders. (And .DS_Store and *.log files would be ignored in those folders.)

    Obviously I could do e.g. !/folder or !/.gitignore too.

    More info: http://git-scm.com/docs/gitignore

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

    I got this working

    # Vendor
    /vendor/braintree/braintree_php/*
    !/vendor/braintree/braintree_php/lib
    
    0 讨论(0)
  • 2020-11-22 00:42

    That's what have worked for me, I wanted to commit only one Cordova plugin to the repo:

    ...
    plugins/*
    !plugins/cordova-plugin-app-customization
    
    0 讨论(0)
  • 2020-11-22 00:43

    To exclude folder from .gitignore, the following can be done.

    !app/
    
    app/*
    !app/bower_components/
    
    app/bower_components/*
    !app/bower_components/highcharts/
    

    This will ignore all files/subfolders inside bower_components except for /highcharts.

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

    I tried all answers as given here above, but none worked for me. After reading the gitignore documentation (here) i found out that if you exclude a folder first that the filenames in the subfolder are not being indexed. So if you use the exclamation mark afterwards to include a file, it is not found in the index and thus not being included in your git client.

    That was the way to finding the solution. I started with adding exceptions for all subfolders in my folder tree to get it working, which is a hell of a job. Afterwards i was able to compact the detailed configuration to the configuration below, which is a bit contrary to the documentation..

    Working .gitignore:

    # Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
    /Pro/*
    !Pro/3rdparty/
    
    # Ignore the '3rdparty' folder, except for the 'domain' subfolder
    /Pro/3rdparty/*
    !Pro/3rdparty/domain/
    
    # Ignore the 'domain' folder, except for the 'modulename' subfolder
    Pro/3rdparty/domain/*
    !Pro/3rdparty/domain/modulename/
    

    As result i see in my git client that only the two files inside the Pro/3rdparty/domain/modulename/ folder are being staged for the next commit, and that was exactly what i was looking for.

    And if you need to whitelist several subfolders of the same folder then group the exclamation mark lines below the exclude statement like this:

    # Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
    /Pro/*
    !Pro/3rdparty/
    
    # Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
    /Pro/3rdparty/*
    !Pro/3rdparty/domain/
    !Pro/3rdparty/hosting/
    
    # Ignore the 'domain' folder, except for the 'modulename' subfolder
    Pro/3rdparty/domain/*
    !Pro/3rdparty/domain/modulename/
    
    # Ignore the 'hosting' folder, except for the 'modulename' subfolder
    Pro/3rdparty/hosting/*
    !Pro/3rdparty/hosting/modulename/
    

    Else it wont work as expected.

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

    Nothing worked for me so far because I was trying to add one jar from lib.

    This did not worked:

    build/*
    !build/libs/*
    !build/libs/
    !build/libs/myjarfile.jar 
    

    This worked:

    build/*
    !build/libs
    
    0 讨论(0)
提交回复
热议问题