使用.gitignore忽略一切,但特定的目录
我的问题是,我有一堆的WordPress网站在我的git仓库,其中我想有select地只提交我的themes
文件夹的内容,而忽略在WordPress中find的其余冗余文件。
我以前使用.gitignore文件来忽略文件types,但是可以用其他方式来使用它 – 即忽略所有文件,但是某个文件夹path?
根(git回购)
– / wordpress
– / /(WordPress网站1)/ wp-content / themes
– / /(WordPress网站2)/ wp-content / themes
– / /(WordPress Site 3)/ wp-content / themes
谢谢-
更新:
基于我做了以下的答案,但它不工作。 有任何想法吗?
# Ignore everything: * # Except for wordpress themes: !*/wp-content/themes/*
我也尝试了以下变化:
!*/wp-content/themes* !*wp-content/themes/* !wp-content/themes/* !/wordpress/*/wp-content/themes* !wordpress/*/wp-content/themes*
这些都没有读我的themes
文件夹。
我是这么做的 – 你基本上必须走上这条路,你不能在任何方向上超过一个关卡:
# Ignore everything: * # Except for the themes directories: !wordpress/ !wordpress/*/ !wordpress/*/wp-content/ !wordpress/*/wp-content/themes/ !wordpress/*/wp-content/themes/* !wordpress/*/wp-content/themes/*/* !wordpress/*/wp-content/themes/*/*/* !wordpress/*/wp-content/themes/*/*/*/* !wordpress/*/wp-content/themes/*/*/*/*/*
请注意,您必须明确允许您要包含的每个级别的内容。 因此,如果我在主题下有深层次的子目录5,我仍然需要把它拼出来。
这只是它对我的工作。 如果有人在意通过一切手段提供更明智的解释。
另外,这些答案有帮助:
怎么办-否定-模式-工作在gitignore
怎么办- gitignore排除规则,实际上工作
注:我试过使用双通配符'globs',但根据这个function是依赖于系统,它不能在我的mac:
不工作:
!**/wp-content/themes/ !**/wp-content/themes/**
我尝试了以上,他们没有那么好。 更好的方法是从这里: https : //gist.github.com/444295
# This is a template .gitignore file for git-managed WordPress projects. # # Fact: you don't want WordPress core files, or your server-specific # configuration files etc., in your project's repository. You just don't. # # Solution: stick this file up your repository root (which it assumes is # also the WordPress root directory) and add exceptions for any plugins, # themes, and other directories that should be under version control. # # See the comments below for more info on how to add exceptions for your # content. Or see git's documentation for more info on .gitignore files: # http://kernel.org/pub/software/scm/git/docs/gitignore.html # Ignore everything in the root except the "wp-content" directory. /* !.gitignore !wp-content/ # Ignore everything in the "wp-content" directory, except the "plugins" # and "themes" directories. wp-content/* !wp-content/plugins/ !wp-content/themes/ # Ignore everything in the "plugins" directory, except the plugins you # specify (see the commented-out examples for hints on how to do this.) wp-content/plugins/* # !wp-content/plugins/my-single-file-plugin.php # !wp-content/plugins/my-directory-plugin/ # Ignore everything in the "themes" directory, except the themes you # specify (see the commented-out example for a hint on how to do this.) wp-content/themes/* # !wp-content/themes/my-theme/
如果在感叹号( !
)的前面添加一个模式,它将排除之前排除该模式的模式。 所以,大概,你可以忽略一切,然后只允许使用这种模式你想要的东西。
修改第一行
*
改变它
/*
试试这些答案:
让.gitignore忽略除几个文件以外的所有内容
# Ignore everything * # But not these files... !.gitignore !script.pl !template.latex # etc... # ...even if they are in subdirectories !*/
如何让Git忽略除子目录以外的所有内容?
这将忽略根文件和根目录,然后取消忽略根bin目录:
/* /*/ !/bin/
这样你可以得到所有的bin目录,包括子目录及其文件。
Yarin的答案为我工作,这是我的版本(我不需要/wordpress
子目录):
* !.gitignore !/wp-content/ !/wp-content/themes/ !/wp-content/themes/* !/wp-content/themes/*/* !/wp-content/themes/*/*/* !/wp-content/themes/*/*/*/* !/wp-content/themes/*/*/*/*/* # I don't want to track this one, so it's included here, after the negated patterns. wp-content/themes/index.php
对于那些寻求更清洁的解决scheme,请尝试以下。
正如在这个答案的评论中所提到的,你必须recursion地使用这个方法。
在这个例子中,你的.git
文件夹和.gitignore
文件所在的./
处有一个网站设置,在./wordpress
有一个WordPress安装设置。 要正确地忽略除了主题目录( wordpress/wp-content/themes/my-theme
) 之外的./wordpress
目录下的所有wordpress/wp-content/themes/my-theme
,您将不得不recursion地忽略每个目录,直到您希望允许的目录:
wordpress/* wordpress/wp-content/* wordpress/wp-content/themes/* !wordpress/wp-content !wordpress/wp-content/themes !wordpress/wp-content/themes/my-theme
使用通配符忽略并允许(或者忽略'from')目录本身的原因使Git能够在忽略里面的所有内容之前先查看目录内部。 然后,我们告诉Git忽略“我们指定的目录”之外的所有内容。 下面是相同的语法,但是按照Git如何看待它:
wordpress/* # Ignore everything inside here !wordpress/wp-content # Apart from this directory wordpress/wp-content/* # Ignore everything inside here !wordpress/wp-content/themes # Apart from this directory wordpress/wp-content/themes/* # Ignore everything inside here !wordpress/wp-content/themes/my-theme # Apart from this directory
希望这有助于更好地理解recursion方法的需求。
假设你有这样的目录结构:
– sites – -all – – – -files – – – – – -private – – – – – – – -newspilot – -default – – – -files – – – – – -private – – – – – – – -newspilot
当你想只允许site / all / files / private / newspilot和sites / default / files / private / newspilot时 ,你可能首先尝试这个:
sites/*/files/* !sites/*/files/private/newspilot
这是错误的! gitignore的棘手的事情是, 你必须先允许包含父(“私人”)目录 ,才可以允许其子目录(“newspilot”)包含在提交中。
sites/*/files/* !sites/*/files/private sites/*/files/private/* !sites/*/files/private/newspilot
http://www.christianengvall.se/gitignore-exclude-folder-but-include-a-subfolder/
晚到派对,但这是我用于未知深度(接受的解决scheme需要已知的深度)
/* !.gitignore !wp-content/ !*/
这样wp-content下的所有东西都不会被忽略。
即使在多次回到这个问题之后,我总是被困在某个地方。 我已经提出了一步一步的详细过程:
首先使用git add
来添加实际的内容。
它会显示相关的文件添加到索引,而其他所有人仍未追踪。 这有助于逐步构build.gitignore
。
$ git add wp-content/themes/my-theme/* $ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css Untracked files: wp-admin/ wp-content/plugins/ wp-content/themes/twentyeleven/ wp-content/themes/twentytwelve/ ... wp-includes/ ...
在您的目录中添加一个临时DUMMY.TXT
文件:
$ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css Untracked files: wp-admin/ wp-content/plugins/ wp-content/themes/twentyeleven/ wp-content/themes/twentytwelve/ ... wp-content/themes/my-theme/DUMMY.TXT <<< ... wp-includes/ ...
我们现在的目标是构build规则,使得这个DUMMY.TXT
成为唯一一个在完成时仍然显示为Untracked的规则。
开始添加规则:
.gitignore
/*
首先是忽略一切。 未追踪的文件应该全部消失,只有索引文件应显示:
$ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css
在pathwp-content
添加第一个目录
/* !/wp-content
现在Untracked文件将再次出现,但只有wp-content
的内容
$ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css Untracked files: wp-content/plugins/ wp-content/themes/twentyeleven/ wp-content/themes/twentytwelve/ ..
忽略第一个dir /wp-content/*
和un-ignore !/wp-content/themes
/* !/wp-content /wp-content/* !/wp-content/themes
现在Untracked文件将进一步缩小到只有wp-content/themes
$ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css Untracked files: wp-content/themes/twentyeleven/ wp-content/themes/twentytwelve/ ..
重复这个过程,直到那个虚拟文件是唯一仍然显示为Untracked的文件:
/* !/wp-content /wp-content/* !/wp-content/themes /wp-content/themes/* !/wp-content/themes/my-theme
$ git status Changes to be committed: new file: wp-content/themes/my-theme/index.php new file: wp-content/themes/my-theme/style.css Untracked files: wp-content/themes/my-theme/DUMMY.TXT
以下是我如何做到的:
# Ignore everything at root: /* # Except for directories: !wordpress/(WordPress Site 1)/wp-content/themes/ !wordpress/(WordPress Site 1)/wp-content/themes/ !wordpress/(WordPress Site 1)/wp-content/themes/ # Except files: !README #Except files of type: !*.txt
这是为我工作。 允许您忽略除特定文件或文件夹以外的所有内容
macOS塞拉利昂
我在同一条船上试图在一个回购协议下pipe理一堆Wordpress网站。 我的目录结构如下所示:
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
我想跟踪每个网站的app/public
文件夹内的项目。 我尝试了这个页面中的示例以及这里的一些build议,最后尝试了这个:
/(WordPress Site 1)/* !(WordPress Site 1)/app (WordPress Site 1)/app/* !(WordPress Site 1)/app/public
哪些工作,但我将不得不忽略每个网站,我试图避免相同的path。
然后我用*
replace了这个网站的名字,这对我来说是个窍门。 所以这就是我最终使用的:
/*/* !*/app */app/* !*/app/public
这有效地忽略了网站文件夹中的所有内容,同时捕获了我在根中创build的任何网站的app/public
文件夹中的所有内容。
请注意,它不会忽略根目录中的文件,只是目录:)
希望这可以帮助。