.gitignore exclude folder but include specific subfolder

后端 未结 17 1591
一个人的身影
一个人的身影 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 07:17

    Commit 59856de from Karsten Blees (kblees) for Git 1.9/2.0 (Q1 2014) clarifies that case:

    gitignore.txt: clarify recursive nature of excluded directories

    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. (*)
    (*: unless certain conditions are met in git 2.8+, see below)
    Git doesn't list excluded directories for performance reasons, so any patterns on contained files have no effect, no matter where they are defined.

    Put a backslash ("\") in front of the first "!" for patterns that begin with a literal "!", for example, "\!important!.txt".

    Example to exclude everything except a specific directory foo/bar (note the /* - without the slash, the wildcard would also exclude everything within foo/bar):

     --------------------------------------------------------------
         $ cat .gitignore
         # exclude everything except directory foo/bar
         /*
         !/foo
         /foo/*
         !/foo/bar
     --------------------------------------------------------------
    

    In your case:

    application/*
    !application/**/
    application/language/*
    !application/language/**/
    !application/language/gr/**
    

    You must white-list folders first, before being able to white-list files within a given folder.


    Update Feb/March 2016:

    Note that with git 2.9.x/2.10 (mid 2016?), it might be possible to re-include a file if a parent directory of that file is excluded if there is no wildcard in the path re-included.

    Nguyễn Thái Ngọc Duy (pclouds) is trying to add this feature:

    • commit 506d8f1 for git v2.7.0, reverted in commit 76b620d git v2.8.0-rc0
    • commit 5e57f9c git v2.8.0-rc0,... reverted(!) in commit 5cee3493 git 2.8.0.

    So with git 2.9+, this could have actually worked, but was ultimately reverted:

    application/
    !application/language/gr/
    

提交回复
热议问题