How do I ignore all files in a project except two subdirectories? I don’t want to include all of WordPress in Git, but I do want to include the customized themes. I have two sibling directories so I don’t want two separate Git projects either.
.gitignore
src/
!src/wp-content/themes/chocolat-child/
!src/wp-content/themes/theme2
It is a fresh repository initialization with no history or commits. When I check status, it is ignoring the subdirectories.
>git status
# Initial commit
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
# .gitignore
# .project
# .settings/
I saw this section in the documentation, but there has to be a workaround: http://git-scm.com/docs/gitignore
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. 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”.
I saw this question, but it was due to a hidden Drupal .gitignore, so it doesn’t solve my problem: Ignoring a directory…but not a subdirectory or two
version
git version 1.8.1.msysgit.1
Actually you can make it a bit more graceful. The following should work for you. Just remember for directories, you need to add
**
at the end of the pattern to include all files under it but not only the directory itself back.If you want to also ignore all files/directories outside
src
directory, make it as follows.To understand the reasons, please refer to my answer to a SO question. In general, there’re 2 rules for the negating pattern in
.gitignore
.Rule 1. Files and directories are separatedly handled in the patterns. To include a directory back doesn’t mean its child files/directories are also included back.
Rule 2. It won’t include files/directories back if their parent directory is still ignored.
Ok I found a way, but this is completely ridiculous! This way will list untracked files if a new file is added.