简体   繁体   English

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

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

My issue is that I have a bunch of WordPress websites in my git repo, of which I want to selectively commit only the content of my themes folders, while ignoring the rest of the redundant files found in WordPress.我的问题是我的 git 存储库中有一堆 WordPress 网站,其中我想有选择地只提交我的themes文件夹的内容,而忽略在 WordPress 中找到的其余冗余文件。

I've used .gitignore files to ignore file types before, but can it be used the other way around- that is to ignore everything BUT a certain folder path?我之前使用过 .gitignore 文件来忽略文件类型,但是可以反过来使用它 - 即忽略除某个文件夹路径之外的所有内容吗?

root (git repo)根(git repo)
- / wordpress - / wordpress
- - / (WordPress Site 1)/wp-content/themes - - / (WordPress 站点 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes - - / (WordPress 站点 2)/wp-content/themes
- - / (WordPress Site 3)/wp-content/themes - - / (WordPress 站点 3)/wp-content/themes

Thanks-谢谢-

UPDATE:更新:

Based on the answers I did the following, but it's not working.根据答案,我做了以下操作,但它不起作用。 Any ideas?有任何想法吗?

# Ignore everything:
*
# Except for wordpress themes:
!*/wp-content/themes/*

I've also tried the following variations:我还尝试了以下变体:

!*/wp-content/themes*
!*wp-content/themes/*
!wp-content/themes/*
!/wordpress/*/wp-content/themes*
!wordpress/*/wp-content/themes*

None of these read my themes folders.这些都没有读取我的themes文件夹。

Here's how I did it - you essentially have to walk up the paths, you can't wildcard more than one level in any direction:我是这样做的 - 你基本上必须沿着路径走,你不能在任何方向上通配一个以上的级别:

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

Notice how you have to explicitly allow content for each level you want to include.请注意您必须如何明确允许您想要包含的每个级别的内容。 So if I have subdirectories 5 deep under themes, I still need to spell that out.所以如果我在主题下面有 5 个子目录,我仍然需要把它拼出来。

This is only how it worked for me.这只是它对我的工作方式。 If someone cares to offer a more informed explanation by all means.如果有人愿意提供更明智的解释。

Also, these answers helpful:此外,这些答案很有帮助:
how-do-negated-patterns-work-in-gitignore How-do-negated-patterns-work-in-gitignore
how-do-gitignore-exclusion-rules-actually-work How-do-gitignore-exclusion-rules-actual-work


NOTE: I tried using double-wildcard 'globs' but according to this that functionality is system dependent and it didn't work on my mac:注意:我尝试使用双通配符“globs”,但据此功能取决于系统,并且在我的 mac 上不起作用:

Did NOT work:不工作:

!**/wp-content/themes/
!**/wp-content/themes/**

I tried the above and they didn't work so well.我尝试了上述方法,但效果不佳。 A better approach is as follows from here: https://gist.github.com/444295更好的方法如下: 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/

modify the first line修改第一行

*

change it to将其更改为

/*

Try these answers:试试这些答案:

Make .gitignore ignore everything except a few files 使 .gitignore 忽略除少数文件之外的所有内容

# 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? 我如何告诉 Git 忽略除子目录之外的所有内容?

This ignores root files & root directories, then un-ignores the root bin directory:这将忽略根文件和根目录,然后取消忽略根 bin 目录:

 /* /*/ !/bin/

This way you get all of the bin directory, including subdirectories and their files.这样你就可以得到所有的 bin 目录,包括子目录和它们的文件。

If you prefix a pattern with an exclamation point ( ! ) it negates any previous pattern which excluded it.如果您在模式前加上感叹号 ( ! ),它将否定排除它的任何先前模式。 So, presumably, you could ignore everything, then only allow what you want by using this pattern.因此,大概您可以忽略所有内容,然后通过使用此模式只允许您想要的内容。

Say you have a directory structure like this:假设你有一个这样的目录结构:

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

When you want to only allow sites/all/files/private/newspilot and sites/default/files/private/newspilot , you might, at first, try this:当您只想允许 sites/all/files/private/newspilotsites/default/files/private/newspilot 时,您可以首先尝试以下操作:

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

This is wrong!这是错误的! The tricky thing with gitignore is that you have to first allow the parent ("private") directory to be included , before you can allow its child directory ("newspilot") to be included in commits. 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/ http://www.christianengvall.se/gitignore-exclude-folder-but-include-a-subfolder/

I needed to ignore everything but not one folder with subdirectories.我需要忽略所有内容,但不是一个带有子目录的文件夹。

For me, this works对我来说,这有效

# Ignore everything
/*

# Not these directories
!folder/

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

I always get stuck somewhere on this even after coming back to this question numerous times.即使在多次回到这个问题之后,我也总是被困在某个地方。 I've come up with a detailed process of doing it step by step:我想出了一个详细的过程,一步一步来:

First just use git add to add the actual content.首先只需使用git add添加实际内容。

It'll show the relevant files added to the index while all others still untracked.它将显示添加到索引中的相关文件,而所有其他文件仍未被跟踪。 This helps contructing .gitignore step by step.这有助于逐步构建.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/
        ...

Add a temporary DUMMY.TXT file in your directory:在您的目录中添加一个临时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/
        ...

Our goal now is to construct the rules such that this DUMMY.TXT be the only one still showing up as Untracked when we're done.我们现在的目标是构建规则,使这个DUMMY.TXT成为唯一一个在我们完成后仍然显示为 Untracked 的规则。

Start adding the rules:开始添加规则:

.gitignore

/*

First one is just to ignore everything.第一个就是忽略一切。 Untracked files should be all gone, only indexed files should be showing:未跟踪的文件应该全部消失,只有索引的文件应该显示:

$ git status

    Changes to be committed:
        new file:   wp-content/themes/my-theme/index.php
        new file:   wp-content/themes/my-theme/style.css

Add the first dir in the path wp-content在路径wp-content添加第一个目录

/*
!/wp-content

Now the Untracked files will show up again, but only have wp-content 's contents现在 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/
        ..

Ignore everything in the first dir /wp-content/* and un-ignore !/wp-content/themes忽略第一个目录中的所有/wp-content/*并取消忽略!/wp-content/themes

/*
!/wp-content

/wp-content/*
!/wp-content/themes

Now the Untracked files will further narrow down to only 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/
        ..

Repeat the process till that dummy file is the only one still showing as Untracked:重复该过程,直到该虚拟文件成为唯一一个仍显示为 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

After having a need for this multiple times I have finally found a solution [1]:在多次需要此之后,我终于找到了解决方案 [1]:

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

Explanation line by line:逐行解释:

  1. Ignore all directories at the root.忽略根目录下的所有目录
  2. Ignore all files with extensions at the root.忽略所有以根为扩展名的文件。
  3. Allow .gitignore itself (and potentially .gitattributes ).允许.gitignore本身(以及可能的.gitattributes )。
  4. Allow the desired directory with all subdirectories允许包含所有子目录的所需目录

[1] Limitations (that I'm aware of): [1]限制(我知道):

  1. It does not ignore files without an extension at the root (and adding /* would break the entire scheme).它不会忽略根目录没有扩展名的文件(并且添加/*会破坏整个方案)。
  2. The directory that you want to include in line 4 ( wordpress in this case) cannot have .您要包含在第 4 行(本例中为wordpress )的目录不能有. in the name (eg wordpress.1 would not work).在名称中(例如wordpress.1不起作用)。 If it does have a .如果它有一个. , then remove line 2 and find another way to exclude all files at the root. ,然后删除第 2 行并找到另一种排除根目录下所有文件的方法。
  3. Tested only on Windows with git version 2.17.1.windows.2仅在git version 2.17.1.windows.2 Windows 上测试

Another easy solution :另一个简单的解决方案:

You want to ignore all Wordpress files, but not your theme (for example).您想忽略所有 Wordpress 文件,但不想忽略您的主题(例如)。

.gitignore , content is : .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

In this example, you can remove wordpress if .gitignore is in root wordpress directory在这个例子中,如果.gitignorewordpress 根目录中,你可以删除wordpress

You can do exactly the same with every folders and content you want to "exceptionally" keep out of gitignored folders/files您可以对要“特别”排除在 gitignored 文件夹/文件之外的每个文件夹和内容执行完全相同的操作

Conclusion :结论 :

Be sure to unignore all directories of a path that you want to unignore请务必取消忽略您要取消忽略的路径的所有目录

BUT

Be sure to ignore all content of unignored directories that you want to ignore请务必忽略您要忽略的未忽略目录的所有内容

Yarin's answer worked for me, here's my version (I don't need the /wordpress sub-directory): 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

For those looking for a cleaner solution, please try the following.对于那些正在寻找更清洁解决方案的人,请尝试以下操作。

As mentioned in the comments of this answer, you have to use this method recursively.如此答案的评论中所述,您必须递归地使用此方法。

In this example, you have a website setup at ./ where your .git folder and .gitignore file is located and a WordPress installation setup in ./wordpress .在这个例子中,你在./有一个网站设置,你的.git文件夹和.gitignore文件所在的位置和 WordPress 安装设置在./wordpress To correctly ignore the everything under the ./wordpress directory apart from the theme directory itself ( wordpress/wp-content/themes/my-theme ), you will have to recursively ignore and allow each directory up until the directory you wish to allow:要正确忽略./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

The reason for ignoring with a wildcard and allowing (or, ignoring 'apart from') the directory itself enables Git to look inside the directory first before ignoring everything inside.使用通配符忽略并允许(或忽略“除了”)目录本身的原因使 Git 在忽略目录中的所有内容之前先查看目录内部。 We then tell Git to ignore everything 'apart from' the directory we have specified.然后我们告诉 Git 忽略“除了”我们指定的目录之外的所有内容。 Here's the same syntax but in order of how Git is looking at it:这是相同的语法,但按照 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

Hopefully this helps someone better understand the need for the recursive method.希望这有助于人们更好地理解递归方法的必要性。

late to the party but here's what I use for unknown depth (the accepted solution requires known depth)迟到了,但这是我用于未知深度的方法(公认的解决方案需要已知深度)

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

this way everything under wp-content is not ignored.这样 wp-content 下的所有内容都不会被忽略。

Here's my solution, which assumes a checkout into 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

and testing:和测试:

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

so it correctly ignores everything I don't want and nothing I want to keep.所以它正确地忽略了我不想要的一切,也没有我想保留的东西。

Releasing Code发布代码

Gitlab is configured with repository mirrors for protected branches, according to https://docs.gitlab.com/ee/workflow/repository_mirroring.html根据https://docs.gitlab.com/ee/workflow/repository_mirroring.html,Gitlab配置了用于受保护分支的存储库镜像

When code is pushed to a protected branch, it will be mirrored to the staging and production servers in the /opt/checkout/repo.git/ bare repo.当代码被推送到受保护的分支时,它将被镜像到/opt/checkout/repo.git/ repo 中的登台生产服务器。 The following post-receive hook (in /opt/checkout/repo.git/hooks/post-receive ) will then checkout the code into the working directory.下面的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}"

For more information, see https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps有关更多信息,请参阅https://www.digitalocean.com/community/tutorials/how-to-set-up-automatic-deployment-with-git-with-a-vps

You can try this:你可以试试这个:

!**/themes/*

Where :哪里

  • ! : negate ignore (include) : 否定忽略(包括)
  • **: any directory tree (recursion) so you don't have to specify you folder path **:任何目录树(递归),因此您不必指定文件夹路径
  • themes: your folder to include (can be anything)主题:您要包含的文件夹(可以是任何内容)
  • *: any file in that folder, you can include all subfolders as well with ** and instead of including any file (*) you can be specific *.css, *.xy *:该文件夹中的任何文件,您也可以使用 ** 包含所有子文件夹,而不是包含任何文件 (*),您可以是特定的 *.css、*.xy

Here's how I did it:这是我如何做到的:

# 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

This is what worked for me.这对我有用。 Allows you to ignore everything except specific files or folders允许您忽略除特定文件或文件夹之外的所有内容

macOS sierra macOS 山脉

I am in the same boat trying to manage a bunch of Wordpress sites under one repo.我在同一条船上试图在一个 repo 下管理一堆 Wordpress 网站。 My directory structure looks like this:我的目录结构如下所示:

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

I want to just track the items inside the app/public folder for each site.我只想跟踪每个站点的app/public文件夹中的项目。 I tried the samples in this page as well as some of the suggestions here and ended up trying this:我尝试了此页面中的示例以及此处的一些建议,并最终尝试了此操作:

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

which worked but I would have to ignore the same path for each site which I was trying to avoid.这有效,但我必须忽略我试图避免的每个站点的相同路径。

Then I just replaced the name of the site with * and that did the trick for me.然后我只是用*替换了网站的名称,这对我有用。 So this is what I ended up using:所以这就是我最终使用的:

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

This effectively ignored everything in the site's folder while capturing everything in the app/public folder for any site that I create in the root.这有效地忽略了站点文件夹中的所有内容,同时为我在根目录中创建的任何站点捕获app/public文件夹中的所有内容。

Note that it will not ignore files in the root, just directories :)请注意,它不会忽略根目录中的文件,只会忽略目录:)

Hope this helps.希望这可以帮助。

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

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