How to make Gitnore all files except two subdirectories?

How to ignore all files in a project except two subdirectories? I don't want to include all of Wordpress in Git, but I want to include custom themes. I have two related directories, so I don't want two separate Git projects.

.gitignore

src/ !src/wp-content/themes/chocolat-child/ !src/wp-content/themes/theme2 

This is a new repository initialization without history or commit. When I check the status, it ignores 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 should be a workaround: http://git-scm.com/docs/gitignore

Additional prefix "!" what denies the pattern; any comparable file excluded by the previous template will be included again. Cannot re-include the file if the parent directory of this file is excluded. Git does not contain excluded directories for performance reasons, so any templates with contained files have no effect, no matter where they are defined. Put a backslash ("\") before the first "!" for patterns that begin with the literal "!" for example, "! important! .txt".

I saw this question, but it was due to a hidden Drupal .gitignore, so it does not solve my problem: Ignoring the directory ... but not a subdirectory or two

version

git version 1.8.1.msysgit.1

+1
source share
2 answers

Ok, I found a way, but it's totally ridiculous! This path will display unused files if a new file is added.

 # Ignore everything in src/ except wp-content/ src/* !src/wp-content/ # Ignore everything in wp-content/ except themes/ src/wp-content/* !src/wp-content/themes/ # Ignore everything in themes/ except for these 2 themes src/wp-content/themes/* !src/wp-content/themes/chocolat-child/ !src/wp-content/themes/othertheme 
0
source

In fact, you can make it a little more elegant. The following should work for you. Just remember the directories, you need to add ** at the end of the template to include all the files in it, but not only the directory itself.

 src/** !src/**/ # for directories !src/wp-content/themes/chocolat-child/** # for files !src/wp-content/themes/othertheme 

If you also want to ignore all files / directories outside the src directory, do this as follows.

 * !*/ # for directories !src/wp-content/themes/chocolat-child/** # for files !src/wp-content/themes/othertheme 

To understand the reasons, see my answer to the SO question . In general, there are 2 rules for the negation pattern in .gitignore .

Rule 1. Files and directories are separated from each other in templates. To include a reverse directory does not mean that its child files / directories are also included back.

Rule 2. It will not include files / directories if their parent directory is still ignored.

0
source

Source: https://habr.com/ru/post/1205307/


All Articles