繁体   English   中英

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

[英]Using .gitignore to ignore everything but specific directories

我的问题是我的 git 存储库中有一堆 WordPress 网站,其中我想有选择地只提交我的themes文件夹的内容,而忽略在 WordPress 中找到的其余冗余文件。

我之前使用过 .gitignore 文件来忽略文件类型,但是可以反过来使用它 - 即忽略除某个文件夹路径之外的所有内容吗?

根(git repo)
- / wordpress
- - / (WordPress 站点 1)/wp-content/themes
- - / (WordPress 站点 2)/wp-content/themes
- - / (WordPress 站点 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-actual-work


注意:我尝试使用双通配符“globs”,但据此功能取决于系统,并且在我的 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 目录,包括子目录和它们的文件。

如果您在模式前加上感叹号 ( ! ),它将否定排除它的任何先前模式。 因此,大概您可以忽略所有内容,然后通过使用此模式只允许您想要的内容。

假设你有一个这样的目录结构:

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

当您只想允许 sites/all/files/private/newspilotsites/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/

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

对我来说,这有效

# 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成为唯一一个在我们完成后仍然显示为 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

在路径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/
        ..

重复该过程,直到该虚拟文件成为唯一一个仍显示为 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

在多次需要此之后,我终于找到了解决方案 [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 上测试

另一个简单的解决方案:

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

在这个例子中,如果.gitignorewordpress 根目录中,你可以删除wordpress

您可以对要“特别”排除在 gitignored 文件夹/文件之外的每个文件夹和内容执行完全相同的操作

结论 :

请务必取消忽略您要取消忽略的路径的所有目录

请务必忽略您要忽略的未忽略目录的所有内容

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

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

如此答案的评论中所述,您必须递归地使用此方法。

在这个例子中,你在./有一个网站设置,你的.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 下的所有内容都不会被忽略。

这是我的解决方案,它假设结帐到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/ repo 中的登台生产服务器。 下面的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

你可以试试这个:

!**/themes/*

哪里

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

这是我如何做到的:

# 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 山脉

我在同一条船上试图在一个 repo 下管理一堆 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文件夹中的项目。 我尝试了此页面中的示例以及此处的一些建议,并最终尝试了此操作:

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

这有效,但我必须忽略我试图避免的每个站点的相同路径。

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

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

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

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

希望这可以帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM