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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5241644/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 10:18:08  来源:igfitidea点击:

Using .gitignore to ignore everything but specific directories

gitgitignoreglob

提问by Yarin

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 themesfolders, 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)
- / wordpress
- - / (WordPress Site 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes
- - / (WordPress Site 3)/wp-content/themes

root (git repo)
- / wordpress
- - / (WordPress Site 1)/wp-content/themes
- - / (WordPress Site 2)/wp-content/themes
- - / (WordPress Site 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 themesfolders.

这些都没有读取我的themes文件夹。

采纳答案by Yarin

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-gitignore-exclusion-rules-actually-work

此外,这些答案很有帮助:
how-do-negated-patterns-work-in-gitignore
how-do-gitignore-exclusion-rules-actually-work



NOTE:I tried using double-wildcard 'globs' but according to thisthat functionality is system dependent and it didn't work on my mac:

注意:我尝试使用双通配符“globs”,但据此功能取决于系统,并且在我的 mac 上不起作用:

Did NOT work:

不工作:

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

回答by dwenaus

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/

回答by u4751247

modify the first line

修改第一行

*

change it to

将其更改为

/*

回答by irritate

Try these answers:

试试这些答案:

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

使 .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?

This ignores root files & root directories, then un-ignores the root bin directory:

/*
/*/
!/bin/

This way you get all of the bin directory, including subdirectories and their files.

我如何告诉 Git 忽略除子目录之外的所有内容?

这将忽略根文件和根目录,然后取消忽略根 bin 目录:

/*
/*/
!/bin/

这样你就可以得到所有的 bin 目录,包括子目录和它们的文件。

回答by dappawit

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.

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

回答by Matias Seguel

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

回答by Peeter Kokk

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/newspilotand 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/

回答by laggingreflex

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 addto add the actual content.

首先只是用于git add添加实际内容。

It'll show the relevant files added to the index while all others still untracked. This helps contructing .gitignorestep 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.TXTfile 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.TXTbe the only one still showing up as Untracked when we're done.

我们现在的目标是构建规则,DUMMY.TXT以便在我们完成后,这是唯一一个仍然显示为 Untracked的规则。

Start adding the rules:

开始添加规则:

.gitignore

.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 文件将再次显示,但只有 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/
        ..

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

回答by cabgfx

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

回答by Oliver Tappin

For those looking for a cleanersolution, please try the following.

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

As mentioned in the comments of thisanswer, you have to use this method recursively.

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

In this example, you have a website setup at ./where your .gitfolder and .gitignorefile is located and a WordPress installation setup in ./wordpress. To correctly ignore the everything under the ./wordpressdirectory apart fromthe 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:

在此示例中,您在文件夹和文件./所在的位置设置了网站,在. 要正确忽略目录下除了主题目录本身 ( )之外的所有内容,您必须递归地忽略并允许每个目录,直到您希望允许的目录:.git.gitignore./wordpress./wordpresswordpress/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. We then tell Git to ignore everything 'apart from' the directory we have specified. Here's the same syntax but in order of how Git is looking at it:

使用通配符忽略并允许(或忽略“除了”)目录本身的原因使 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

Hopefully this helps someone better understand the need for the recursive method.

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