.gitignore exclude folder but include specific subfolder

后端 未结 17 1627
一个人的身影
一个人的身影 2020-11-21 06:36

I have the folder application/ which I add to the .gitignore. Inside the application/ folder is the folder application/language

相关标签:
17条回答
  • 2020-11-21 06:58

    This worked for me:

    **/.idea/**
    !**/.idea/copyright/
    !.idea/copyright/profiles_settings.xml
    !.idea/copyright/Copyright.xml
    
    0 讨论(0)
  • 2020-11-21 07:06

    I've found only this actually works.

    **/node_modules/*
    !**/node_modules/keep-dir
    
    0 讨论(0)
  • 2020-11-21 07:07

    Especially for the older Git versions, most of the suggestions won't work that well. If that's the case, I'd put a separate .gitignore in the directory where I want the content to be included regardless of other settings and allow there what is needed.

    For example: /.gitignore

    # ignore all .dll files
    *.dll
    

    /dependency_files/.gitignore

    # include everything
    !*
    

    So everything in /dependency_files (even .dll files) are included just fine.

    0 讨论(0)
  • 2020-11-21 07:13

    I wanted to track jquery production js files and this worked:

    node_modules/*
    !node_modules/jquery
    node_modules/jquery/*
    !node_modules/jquery/dist/*
    
    0 讨论(0)
  • 2020-11-21 07:14

    Just another example of walking down the directory structure to get exactly what you want. Note: I didn't exclude Library/ but Library/**/*

    # .gitignore file
    Library/**/*
    !Library/Application Support/
    !Library/Application Support/Sublime Text 3/
    !Library/Application Support/Sublime Text 3/Packages/
    !Library/Application Support/Sublime Text 3/Packages/User/
    !Library/Application Support/Sublime Text 3/Packages/User/*macro
    !Library/Application Support/Sublime Text 3/Packages/User/*snippet
    !Library/Application Support/Sublime Text 3/Packages/User/*settings
    !Library/Application Support/Sublime Text 3/Packages/User/*keymap
    !Library/Application Support/Sublime Text 3/Packages/User/*theme
    !Library/Application Support/Sublime Text 3/Packages/User/**/
    !Library/Application Support/Sublime Text 3/Packages/User/**/*macro
    !Library/Application Support/Sublime Text 3/Packages/User/**/*snippet
    !Library/Application Support/Sublime Text 3/Packages/User/**/*settings
    !Library/Application Support/Sublime Text 3/Packages/User/**/*keymap
    !Library/Application Support/Sublime Text 3/Packages/User/**/*theme
    

    > git add Library

    > git status

    On branch master
    Your branch is up-to-date with 'origin/master'.
    Changes to be committed:
      (use "git reset HEAD <file>..." to unstage)
    
        new file:   Library/Application Support/Sublime Text 3/Packages/User/Default (OSX).sublime-keymap
        new file:   Library/Application Support/Sublime Text 3/Packages/User/ElixirSublime.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/Package Control.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/Preferences.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/RESTer.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/SublimeLinter/Monokai (SL).tmTheme
        new file:   Library/Application Support/Sublime Text 3/Packages/User/TextPastryHistory.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/ZenTabs.sublime-settings
        new file:   Library/Application Support/Sublime Text 3/Packages/User/adrian-comment.sublime-macro
        new file:   Library/Application Support/Sublime Text 3/Packages/User/json-pretty-generate.sublime-snippet
        new file:   Library/Application Support/Sublime Text 3/Packages/User/raise-exception.sublime-snippet
        new file:   Library/Application Support/Sublime Text 3/Packages/User/trailing_spaces.sublime-settings
    
    0 讨论(0)
  • 2020-11-21 07:15

    If you exclude application/, then everything under it will always be excluded (even if some later negative exclusion pattern (“unignore”) might match something under application/).

    To do what you want, you have to “unignore” every parent directory of anything that you want to “unignore”. Usually you end up writing rules for this situation in pairs: ignore everything in a directory, but not some certain subdirectory.

    # you can skip this first one if it is not already excluded by prior patterns
    !application/
    
    application/*
    !application/language/
    
    application/language/*
    !application/language/gr/
    

    Note
    The trailing /* is significant:

    • The pattern dir/ excludes a directory named dir and (implicitly) everything under it.
      With dir/, Git will never look at anything under dir, and thus will never apply any of the “un-exclude” patterns to anything under dir.
    • The pattern dir/* says nothing about dir itself; it just excludes everything under dir. With dir/*, Git will process the direct contents of dir, giving other patterns a chance to “un-exclude” some bit of the content (!dir/sub/).
    0 讨论(0)
提交回复
热议问题