我的问题是我的git仓库中有一堆WordPress网站,其中我想有选择地只提交我的themes
文件夹的内容,而忽略了WordPress中的其余冗余文件。
我之前使用.gitignore文件来忽略文件类型,但它可以反过来使用 - 即忽略一切但是某个文件夹路径?
root(git repo) - / wordpress - - /(WordPress网站1)/ wp-content / themes - - /(WordPress Site 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个子目录,我仍然需要拼写出来。
这只是它对我有用的方式。如果有人愿意通过各种方式提供更明智的解释。
此外,这些答案很有帮助: how-do-negated-patterns-work-in-gitignore how-do-gitignore-exclusion-rules-actually-work
注意:我尝试使用双通配符'globs',但根据this,功能是系统相关的,它在我的mac上不起作用:
不工作:
!**/wp-content/themes/
!**/wp-content/themes/**
我需要忽略所有内容,但不要忽略一个包含子目录的文件夹。
对我来说,这是有效的
# Ignore everything
/*
# Not these directories
!folder/
# Not these files
!.gitignore
!.env
!file.txt
即使在多次回到这个问题之后,我总是被困在这个地方。我已经提出了一步一步的详细过程:
首先只需使用git add
添加实际内容。
它将显示添加到索引的相关文件,而其他所有文件仍未跟踪。这有助于逐步构建.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/
...
我们现在的目标是构建规则,使得这个DUMMY.TXT
成为唯一一个在我们完成时仍然显示为未经训练的人。
开始添加规则:
.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
在路径wp-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/
..
忽略第一个模具qazxsw poi和unignore qazxsw poi中的所有内容
/wp-content/*
现在Untracked文件将进一步缩小到只有!/wp-content/themes
/*
!/wp-content
/wp-content/*
!/wp-content/themes
重复该过程,直到该虚拟文件仍然显示为未跟踪:
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/
..
你可以试试这个:
/*
!/wp-content
/wp-content/*
!/wp-content/themes
/wp-content/themes/*
!/wp-content/themes/my-theme
哪里:
在多次需要这个之后我终于找到了解决方案[1]:
$ 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
逐行说明:
!**/themes/*
本身(和潜在的/*/
/*.*
!.git*
!/wordpress
)。[1]限制(我知道):
.gitignore
会破坏整个方案)。.gitattributes
)的目录中名称中不能包含/*
(例如wordpress
不起作用)。如果它确实有.
,则删除第2行并找到另一种方法来排除根目录下的所有文件。wordpress.1
进行测试我是这样做的:
.
这对我有用。允许您忽略除特定文件或文件夹之外的所有内容
macOS sierra
这是我的解决方案,假设结帐到git version 2.17.1.windows.2
# 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
和测试:
wp-content
所以它正确地忽略了我不想要的一切,我想要保留的任何东西。
根据# Ignore everything except directories
*
!*/
# except everything in the child theme and its required plugin
!/themes/mytheme-child/**
!/plugins/my-plugin/**
# and this file
!.gitignore
的说法,Gitlab配置了受保护分支的存储库镜像
当代码被推送到受保护的分支时,它将被镜像到git version 2.20.1 (Apple Git-117)
$ git check-ignore -v .foo foo foo/ themes/foo themes/foo/bar themes/mytheme-child \
themes/mytheme-child/foo plugins/foo plugins/my-plugin plugins/my-plugin/foo .gitignore
.gitignore:2:* .foo
.gitignore:2:* foo
.gitignore:2:* foo/
.gitignore:2:* themes/foo
.gitignore:2:* themes/foo/bar
.gitignore:2:* themes/mytheme-child
.gitignore:6:!/themes/mytheme-child/** themes/mytheme-child/foo
.gitignore:2:* plugins/foo
.gitignore:2:* plugins/my-plugin
.gitignore:7:!/plugins/my-plugin/** plugins/my-plugin/foo
.gitignore:10:!.gitignore .gitignore
bare repo中的登台和生产服务器。然后,以下https://docs.gitlab.com/ee/workflow/repository_mirroring.html钩子(在/opt/checkout/repo.git/
中)将代码签出到工作目录中。
post-receive
有关更多信息,请参阅/opt/checkout/repo.git/hooks/post-receive
我正在试图在一个回购下管理一堆Wordpress网站。我的目录结构如下所示:
#!/bin/bash
BRANCH=staging
GIT_DIR=/opt/checkout/repo.git
GIT_WORK_TREE=/opt/bitnami/apps/wordpress/htdocs/wp-content
# on push, checkout the code to the working tree
git checkout -f "${BRANCH}"
# ensure permissions are correct
sudo chown -R bitnami:daemon "${GIT_WORK_TREE}"
sudo chmod -R go-w "${GIT_WORK_TREE}"
我想跟踪每个站点的https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps文件夹中的项目。我尝试了这个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
哪个有效,但我不得不忽略每个网站的相同路径,我试图避免。
然后我用page替换了网站的名称,这对我来说就是这个伎俩。所以这就是我最终使用的:
/(WordPress Site 1)/*
!(WordPress Site 1)/app
(WordPress Site 1)/app/*
!(WordPress Site 1)/app/public
这有效地忽略了站点文件夹中的所有内容,同时捕获我在根目录中创建的任何站点的*
文件夹中的所有内容。
请注意,它不会忽略根目录中的文件,只是目录:)
希望这可以帮助。
另一种简单方案:
您想要忽略所有Wordpress文件,而不是您的主题(例如)。
.gitignore,内容是:
/*/*
!*/app
*/app/*
!*/app/public
在此示例中,如果.gitignore位于root wordpress目录中,则可以删除wordpress
您可以对要“异常”保留gitignored文件夹/文件的每个文件夹和内容执行完全相同的操作
结论:
请务必取消忽略要取消签名的路径的所有目录
但
请务必忽略要忽略的所有未签名目录的内容
我尝试了上述内容,但效果并不好。一个更好的方法如下: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/
修改第一行
*
改为
/*
如果为带有感叹号(!
)的模式添加前缀,则会取消之前排除它的任何模式。所以,大概你可以忽略一切,然后只使用这个模式允许你想要的东西。
试试这些答案:
Make .gitignore ignore everything except a few files
# Ignore everything * # But not these files... !.gitignore !script.pl !template.latex # etc... # ...even if they are in subdirectories !*/
How do I tell Git to ignore everything except a subdirectory?
这将忽略根文件和根目录,然后取消忽略根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
假设您有这样的目录结构:
– sites
– -all
– – – -files
– – – – – -private
– – – – – – – -newspilot
– -default
– – – -files
– – – – – -private
– – – – – – – -newspilot
如果您只想允许sites / 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/
对于那些寻求更清洁解决方案的人,请尝试以下方法。
正如this回答的评论中所提到的,你必须递归地使用这个方法。
在此示例中,您在./
上设置了一个网站,其中包含.git
文件夹和.gitignore
文件,以及./wordpress
中的WordPress安装设置。要正确忽略./wordpress
目录下除主题目录本身(wordpress/wp-content/themes/my-theme
)之外的所有内容,您必须以递归方式忽略并允许每个目录直到您希望允许的目录:
wordpress/*
wordpress/wp-content/*
wordpress/wp-content/themes/*
!wordpress/wp-content
!wordpress/wp-content/themes
!wordpress/wp-content/themes/my-theme
使用通配符忽略并允许(或忽略“除了”)目录本身的原因使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
希望这有助于人们更好地理解递归方法的必要性。
迟到了,但这是我用于未知深度的东西(接受的解决方案需要已知的深度)
/*
!.gitignore
!wp-content/
!*/
这样,wp-content下的所有内容都不会被忽略。