Yar*_*rin 121 git glob gitignore
我的问题是我的git仓库里有一堆WordPress网站,其中我想有选择地只提交我themes文件夹的内容,而忽略了WordPress中的其余冗余文件.
我之前使用.gitignore文件来忽略文件类型,但它可以反过来使用 - 即忽略一切但是某个文件夹路径?
root(git repo)
- /wordpress
- - /(WordPress Site 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/*
Run Code Online (Sandbox Code Playgroud)
我也尝试了以下变化:
!*/wp-content/themes*
!*wp-content/themes/*
!wp-content/themes/*
!/wordpress/*/wp-content/themes*
!wordpress/*/wp-content/themes*
Run Code Online (Sandbox Code Playgroud)
这些都没有读取我的themes文件夹.
Yar*_*rin 117
这就是我做的方式 - 你基本上必须走上路径,你不能在任何方向上通过多个级别:
# 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/*/*/*/*/*
Run Code Online (Sandbox Code Playgroud)
请注意您必须明确允许要包含的每个级别的内容.因此,如果我在主题下有5个子目录,我仍然需要拼写出来.
这只是它对我有用的方式.如果有人愿意通过各种方式提供更明智的解释.
此外,这些答案很有帮助:
怎么做 - 否定 - 模式 - 在gitignore工作
怎么办-gitignore-exclusion-rules-actual-work
注意:我尝试使用双通配符'globs'但根据此功能是系统相关的,它在我的mac上不起作用:
不工作:
!**/wp-content/themes/
!**/wp-content/themes/**
Run Code Online (Sandbox Code Playgroud)
dwe*_*aus 82
我尝试了上述内容,但效果并不好.一个更好的方法如下: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/
Run Code Online (Sandbox Code Playgroud)
吴毅凡*_*吴毅凡 20
修改第一行
*
Run Code Online (Sandbox Code Playgroud)
改为
/*
Run Code Online (Sandbox Code Playgroud)
试试这些答案:
Make .gitignore会忽略除少数文件之外的所有内容
Run Code Online (Sandbox Code Playgroud)# Ignore everything * # But not these files... !.gitignore !script.pl !template.latex # etc... # ...even if they are in subdirectories !*/
这将忽略根文件和根目录,然后取消忽略根bin目录:
Run Code Online (Sandbox Code Playgroud)/* /*/ !/bin/这样您就可以获得所有bin目录,包括子目录及其文件.
假设你有一个这样的目录结构:
– sites
– -all
– – – -files
– – – – – -private
– – – – – – – -newspilot
– -default
– – – -files
– – – – – -private
– – – – – – – -newspilot
Run Code Online (Sandbox Code Playgroud)
当您只想允许 sites/all/files/private/newspilot和sites/default/files/private/newspilot 时,您可以首先尝试以下操作:
sites/*/files/*
!sites/*/files/private/newspilot
Run Code Online (Sandbox Code Playgroud)
这是错误的!gitignore 的棘手之处在于,您必须首先允许包含父目录(“私有”),然后才能允许其子目录(“newspilot”)包含在提交中。
sites/*/files/*
!sites/*/files/private
sites/*/files/private/*
!sites/*/files/private/newspilot
Run Code Online (Sandbox Code Playgroud)
http://www.christianengvall.se/gitignore-exclude-folder-but-include-a-subfolder/
亚林的答案对我有用,这是我的版本(我不需要子目录/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
Run Code Online (Sandbox Code Playgroud)
即使在多次回到这个问题之后,我也总是被困在某个地方。我想出了一个详细的过程,一步一步来:
首先只是用于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/
...
Run Code Online (Sandbox Code Playgroud)
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/
...
Run Code Online (Sandbox Code Playgroud)
我们现在的目标是构建规则,DUMMY.TXT以便在我们完成后,这是唯一一个仍然显示为 Untracked的规则。
开始添加规则:
.gitignore
/*
Run Code Online (Sandbox Code Playgroud)
第一个就是忽略一切。未跟踪的文件应该全部消失,只有索引的文件应该显示:
$ git status
Changes to be committed:
new file: wp-content/themes/my-theme/index.php
new file: wp-content/themes/my-theme/style.css
Run Code Online (Sandbox Code Playgroud)
添加路径中的第一个目录 wp-content
/*
!/wp-content
Run Code Online (Sandbox Code Playgroud)
现在 Untracked 文件将再次显示,但只有 havewp-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/
..
Run Code Online (Sandbox Code Playgroud)
忽略第一个目录中的所有内容/wp-content/*并取消忽略!/wp-content/themes
/*
!/wp-content
/wp-content/*
!/wp-content/themes
Run Code Online (Sandbox Code Playgroud)
现在未跟踪的文件将进一步缩小到仅 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/
..
Run Code Online (Sandbox Code Playgroud)
重复该过程,直到该虚拟文件成为唯一一个仍显示为 Untracked 的文件:
/*
!/wp-content
/wp-content/*
!/wp-content/themes
/wp-content/themes/*
!/wp-content/themes/my-theme
Run Code Online (Sandbox Code Playgroud)
$ 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
Run Code Online (Sandbox Code Playgroud)
我需要忽略所有内容,但不要忽略一个包含子目录的文件夹。
对我来说,这有效
# Ignore everything
/*
# Not these directories
!folder/
# Not these files
!.gitignore
!.env
!file.txt
Run Code Online (Sandbox Code Playgroud)
在多次需要此之后,我终于找到了解决方案 [1]:
/*/
/*.*
!.git*
!/wordpress
Run Code Online (Sandbox Code Playgroud)
逐行解释:
.gitignore自己(和可能.gitattributes)。[1]限制(我知道):
/*会破坏整个方案)。wordpress在本例中)不能包含.在名称中(例如wordpress.1不起作用)。如果它确实有.,则删除第 2 行并找到另一种方法来排除根目录下的所有文件。git version 2.17.1.windows.2另一个简单的解决方案:
您想要忽略所有 Wordpress 文件,但不是您的主题(例如)。
.gitignore,内容是:
# All wordpress + content of revert ignored directories
wordpress/*
wordpress/wp-content/*
wordpress/wp-content/themes/*
# Revert ignoring directories
!wordpress/
!wordpress/wp-content/
!wordpress/wp-content/themes/
!wordpress/wp-content/themes/my_theme
Run Code Online (Sandbox Code Playgroud)
在此示例中,如果.gitignore位于WordPress 根目录中,则可以删除WordPress
您可以对您想要“特别”保留在 gitignored 文件夹/文件之外的每个文件夹和内容执行完全相同的操作
结论 :
确保取消忽略要取消忽略的路径的所有目录
但
请务必忽略您想要忽略的未忽略目录的所有内容
| 归档时间: |
|
| 查看次数: |
70319 次 |
| 最近记录: |