Using .gitignore to ignore everything but specific

2019-01-05 09:34发布

My issue is that I have a bunch of WordPress websites in my git repo, of which I want to selectively commit only the content of my themes folders, while ignoring the rest of the redundant files found in WordPress.

I've used .gitignore files to ignore file types before, but can it be used the other way around- that is to ignore everything BUT a certain folder path?

root (git repo)
- / wordpress
- - / (WordPress Site 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes
- - / (WordPress Site 3)/wp-content/themes

Thanks-

UPDATE:

Based on the answers I did the following, but it's not working. Any ideas?

# Ignore everything:
*
# Except for wordpress themes:
!*/wp-content/themes/*

I've also tried the following variations:

!*/wp-content/themes*
!*wp-content/themes/*
!wp-content/themes/*
!/wordpress/*/wp-content/themes*
!wordpress/*/wp-content/themes*

None of these read my themes folders.

14条回答
你好瞎i
2楼-- · 2019-01-05 09:58

I am in the same boat trying to manage a bunch of Wordpress sites under one repo. My directory structure looks like this:

root (git repo)
(WordPress Site 1)/app/public/wp-content/themes
(WordPress Site 2)/app/public/wp-content/themes
(WordPress Site 3)/app/public/wp-content/themes

I want to just track the items inside the app/public folder for each site. I tried the samples in this page as well as some of the suggestions here and ended up trying this:

/(WordPress Site 1)/*
!(WordPress Site 1)/app
(WordPress Site 1)/app/*
!(WordPress Site 1)/app/public

which worked but I would have to ignore the same path for each site which I was trying to avoid.

Then I just replaced the name of the site with * and that did the trick for me. So this is what I ended up using:

/*/*
!*/app
*/app/*
!*/app/public

This effectively ignored everything in the site's folder while capturing everything in the app/public folder for any site that I create in the root.

Note that it will not ignore files in the root, just directories :)

Hope this helps.

查看更多
爷的心禁止访问
3楼-- · 2019-01-05 10:00

You can try this:

!**/themes/*

Where:

  • !: negate ignore (include)
  • **: any directory tree (recursion) so you don't have to specify you folder path
  • themes: your folder to include (can be anything)
  • *: any file in that folder, you can include all subfolders as well with ** and instead of including any file (*) you can be specific *.css, *.xy
查看更多
登录 后发表回答