.gitignore exclude folder but include specific sub

2018-12-31 01:24发布

I have the folder application/ which I add to the .gitignore. Inside the application/ folder is the folder application/language/gr. How can I include this folder? I've tried this

application/
!application/language/gr/

with no luck...

标签: git gitignore
14条回答
旧人旧事旧时光
2楼-- · 2018-12-31 02:20

@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
查看更多
像晚风撩人
3楼-- · 2018-12-31 02:20

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.

查看更多
登录 后发表回答