.gitignore exclude folder but include specific subfolder

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

    @Chris Johnsen's answer is great, but with a newer versions of Git (1.8.2 or later), there is a double asterisk pattern you can leverage for a bit more shorthand solution:

    # assuming the root folder you want to ignore is 'application'
    application/**/*
    
    # the subfolder(s) you want to track:
    !application/language/gr/
    

    This way you don't have to "unignore" parent directory of the subfolder you want to track.


    With Git 2.17.0 (Not sure how early before this version. Possibly back to 1.8.2), using the ** pattern combined with excludes for each subdirectory leading up to your file(s) works. For example:

    # assuming the root folder you want to ignore is 'application'
    application/**
    
    # Explicitly track certain content nested in the 'application' folder:
    !application/language/
    !application/language/gr/
    !application/language/gr/** # Example adding all files & folder in the 'gr' folder
    !application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder
    

提交回复
热议问题