使用.gitignore忽略除特定目录之外的所有内容

时间:2011-03-09 05:14:29

标签: git gitignore glob

我的问题是我的git repo中有一堆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文件夹。

18 个答案:

答案 0 :(得分:105)

我是这样做的 - 你基本上必须走上路径,你不能在任何方向上通过多个级别:

# 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/**

答案 1 :(得分:75)

我尝试了上述内容并且效果不佳。一个更好的方法如下: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/

答案 2 :(得分:16)

修改第一行

*

将其更改为

/*

答案 3 :(得分:9)

如果您为带有感叹号(!)的模式添加前缀,则会取消之前排除它的任何模式。所以,大概你可以忽略一切,然后只使用这种模式允许你想要的东西。

答案 4 :(得分:9)

尝试以下答案:

  

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目录,包括子目录及其文件。

答案 5 :(得分:4)

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

答案 6 :(得分:4)

假设您有这样的目录结构:

– sites
– -all
– – – -files
– – – – – -private
– – – – – – – -newspilot
– -default
– – – -files
– – – – – -private
– – – – – – – -newspilot

如果您想仅允许网站/所有/文件/私人/新闻报道网站/默认/文件/私人/新闻报道,您可能会首先尝试这样:

sites/*/files/*
!sites/*/files/private/newspilot

这是错误的! gitignore最棘手的事情是你必须首先允许包含父(" private")目录,在允许其子目录(" newspilot")包含在提交中之前。

sites/*/files/*
!sites/*/files/private
sites/*/files/private/*
!sites/*/files/private/newspilot

http://www.christianengvall.se/gitignore-exclude-folder-but-include-a-subfolder/

答案 7 :(得分:3)

对于那些寻找清洁解决方案的人,请尝试以下操作。

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 )下的所有内容,您必须以递归方式忽略并允许每个目录直到您希望的目录允许:

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

使用通配符忽略并允许(或忽略“除了”)目录本身的原因使Git能够在忽略内部所有内容之前首先查看目录。然后我们告诉Git忽略“我们指定的目录之外的所有内容”。这是相同的语法,但按照Git如何看待它的顺序:

clc;
word = actxserver('Word.Application');
word.Visible = 1;
document = word.Documents.Add;
selection = word.Selection;
selection.TypeText('Hello world. ');
selection.TypeText('My name is Professor Kitchin');
selection.TypeParagraph;
selection.TypeText('How are you today?');
selection.TypeParagraph;
selection.TypeText('Big Finale');
selection.Style='Heading 1';
selection.TypeParagraph;
a=34;
b=23;
c=a+b;
H1 = document.Styles.Item('Heading 1');
H1.Font.Name = 'Garamond';
H1.Font.Size = 20;
H1.Font.Bold = 1;
%H1.Font.TextColor.RGB=60000; % some ugly color green

selection.TypeParagraph
selection.TypeText('That is all for today!')
document.SaveAs2([pwd '/test.docx']);

希望这有助于人们更好地理解递归方法的必要性。

答案 8 :(得分:3)

派对迟到了,但这是我用于未知深度的内容(接受的解决方案需要已知的深度)

/*
!.gitignore
!wp-content/
!*/

这样,wp-content下的所有内容都不会被忽略。

答案 9 :(得分:3)

我需要忽略所有内容,但不要忽略一个包含子目录的文件夹。

对我而言,这是有效的

# Ignore everything
/*

# Not these directories
!folder/

# Not these files
!.gitignore
!.env
!file.txt

答案 10 :(得分:2)

即使在多次回到这个问题之后,我总是被困在某个地方。我已经提出了一步一步的详细过程:

首先只需使用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/
        ..

忽略第一个目录/wp-content/*中的所有内容并取消忽略!/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

$ 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

答案 11 :(得分:2)

您可以尝试以下方法:

!**/themes/*

位置

  • :否定忽略(包括)
  • **:任何目录树(递归),因此您不必指定文件夹路径
  • 主题:您要包含的文件夹(可以是任何内容)
  • *:该文件夹中的任何文件,您都可以将所有子文件夹都包含在**中,而不是包含任何文件(*),您可以将其指定为* .css,*。xy

答案 12 :(得分:2)

多次需要后,我终于找到了解决方法[1]:

/*/
/*.*
!.git*
!/wordpress

逐行说明:

  1. 忽略所有目录
  2. 忽略所有带有扩展名的文件。
  3. 允许.gitignore本身(并可能允许.gitattributes)。
  4. 允许所需的目录包含所有子目录

[1] 限制(我知道):

  1. 它不会忽略没有扩展名的文件(并且添加/*会破坏整个方案)。
  2. 您要包含在第4行中的目录(在这种情况下为wordpress)名称中不能包含.(例如wordpress.1将不起作用)。如果确实有.,请删除第2行,并找到另一种方法来排除根目录中的所有文件。
  3. 仅在具有git version 2.17.1.windows.2的Windows上进行测试

答案 13 :(得分:1)

以下是我的表现:

# 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 sierra

答案 14 :(得分:1)

这是我的解决方案,它假定签入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

和测试:

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

因此它正确地忽略了我不需要的所有内容,也不想保留任何内容。

发布代码

根据https://docs.gitlab.com/ee/workflow/repository_mirroring.html

,为Gitlab配置了受保护分支的存储库镜像

将代码推送到受保护的分支后,它将被镜像到/opt/checkout/repo.git/裸仓库中的 staging production 服务器。然后,下面的post-receive钩子(位于/opt/checkout/repo.git/hooks/post-receive中)会将代码检出到工作目录中。

#!/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

答案 15 :(得分:0)

在大多数情况下,这是唯一被忽略的文件,因为您拥有wp-config-sample.php,可以与他人共享。

/wp-config.php

答案 16 :(得分:0)

另一个简单的解决方案:

您要忽略所有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

在此示例中,如果 .gitignore 位于 wordpress根目录

中,则可以删除 wordpress

您可以对要“例外”地保存在gitignored文件夹/文件之外的每个文件夹和内容执行完全相同的操作

结论:

  

确保取消忽略您要忽略的路径的所有目录

但是

  

请确保忽略您要忽略的未忽略目录的所有内容

答案 17 :(得分:0)

我正在试图在一个回购下管理一堆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文件夹中的项目。我尝试了这个page中的示例以及这里的一些建议,最后尝试了这个:

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

哪个有效,但我不得不忽略我试图避免的每个站点的相同路径。

然后我用*替换了网站的名称,这对我来说很有用。所以这就是我最终使用的:

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

这有效地忽略了网站文件夹中的所有内容,同时捕获了我在根目录中创建的任何网站的app/public文件夹中的所有内容。

请注意,它不会忽略根目录中的文件,只是目录:)

希望这有帮助。