.gitignore排除文件夹但包含特定的子文件夹

chc*_*ist 863 git gitignore

我有文件夹application /我添加到.gitignore.应用程序/文件夹内是文件夹application/language/gr.我该如何包含此文件夹?我试过这个

application/
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)

没有运气......

Chr*_*sen 1461

如果你排除application/,那么它下面的所有东西将永远被排除(即使一些后来的否定排除模式("unignore")可能与下面的东西相匹配application/).

要做你想做的事,你必须"unignore"任何你想要"unignore"的东西的父目录.通常,您最终会成对编写针对此情况的规则:忽略目录中的所有内容,但不忽略某些子目录.

# you can skip this first one if it is not already excluded by prior patterns
!application/

application/*
!application/language/

application/language/*
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)

注意
尾随/*是重要的:

  • 该模式dir/排除了一个名为dir和(隐式)其下的所有内容的目录.
    有了dir/,Git永远不会看任何东西dir,因此永远不会将任何"非排除"模式应用于任何东西dir.
  • 模式dir/*dir自己一无所知; 它只是排除了一切dir.有了dir/*,Git将处理直接内容dir,使其他模式有机会"取消排除"某些内容(!dir/sub/).

  • @seh:是的,尾随的`/*`很重要.如果排除目录,Git将永远不会查看该目录的内容.模式`dir /`排除名为`dir`的目录和(隐式)其下的所有内容."dir/*"模式对"dir"本身一无所知; 它只是排除了"dir"下的所有内容.使用`dir /`,Git永远不会在`dir`下查看任何内容,因此永远不会将任何"un-exclude"模式应用于`dir`下的任何内容.使用`dir/*`,Git将处理`dir`的直接内容,使其他模式有机会"取消排除"某些内容(`!dir/sub /`). (112认同)
  • 只是因为这是正确的并不意味着它不是疯了 (43认同)
  • 尾随星号是否显着?如果是这样,意义上的差异是什么?根据*gitignore*文档中描述的算法,以尾部斜杠结尾匹配该目录下的目录和路径.以星号结尾然后将作为一个圆形图案落入治疗.试验显示星号变体可以工作,但不会以尾部斜杠结尾.我想明白为什么会这样. (11认同)
  • 我无法让这个工作(疯狂.gitignore文件!),所以我只是强行添加cd'ing到我想要的目录后的文件.`git add -f .` (8认同)
  • 啊,这就解释了.无论我多少次阅读*gitignore*文档,我都不知道恢复模式何时不起作用.有了你的解释,现在已经很清楚了.*gitignore*文档需要一个"配方"部分来解释如何执行此操作. (5认同)
  • 注意,您不能依赖git status的输出,它只会告诉您将要添加顶级目录。相反,对顶层目录执行“ git add”,然后“ git status”将(希望)列出已被模式匹配的文件的子集。 (2认同)
  • 不适用于 git 版本```2.10.2.windows.1```。 (2认同)

Von*_*onC 128

提交59856de卡斯滕Blees(kblees) GIT中1.9/2.0(Q1 2014)阐明这种情况下:

gitignore.txt:澄清排除目录的递归性质

一个可选的前缀" !",它否定了模式; 之前模式排除的任何匹配文件将再次包含在内.

如果排除该文件的父目录,则无法重新包含文件.(*)
(*:除非在git 2.8+中满足某些条件,见下文)
Git没有列出出于性能原因排除的目录,因此所包含文件的任何模式都无效,无论它们在何处定义.

对于以文字" " 开头的模式,\在第一个" !" 前面加一个反斜杠(" ")!,例如" \!important!.txt".

排除除特定目录之外的所有内容的示例foo/bar(注意/*- 没有斜杠,通配符也会排除其中的所有内容foo/bar):

 --------------------------------------------------------------
     $ cat .gitignore
     # exclude everything except directory foo/bar
     /*
     !/foo
     /foo/*
     !/foo/bar
 --------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

在你的情况下:

application/*
!application/**/
application/language/*
!application/language/**/
!application/language/gr/**
Run Code Online (Sandbox Code Playgroud)

在能够列出给定文件夹中的文件之前,必须先将文件夹列入白名单.


2016年2月/ 3月更新:

请注意,使用git 2.9.x/2.10(2016年中期?),如果重新包含的路径中没有通配符,则可以重新包含该文件(如果排除该文件的父目录).

NguyễnTháiNgọcDuy(pclouds)正在尝试添加此功能:

所以使用git 2.9+,这可能实际上有效,但最终还原了:

application/
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)

  • Git 2.9昨天发布了.确认答案中提到的`application /` +`!application/language/gr /`模式正如预期的那样工作. (5认同)
  • @RayShan Strange:我已经看到恢复提交取消了该功能,但我还没有看到(以及发行说明https://github.com/git/git/blob/master/Documentation/RelNotes/2.9.0.txt没有提及)任何改进 .gitignore 规则的提交。 (2认同)

rpy*_*yzh 49

@Chris Johnsen的答案很棒,但是对于更新版本的Git(1.8.2或更高版本),你可以使用双星号模式来获得更简便的解决方案:

# assuming the root folder you want to ignore is 'application'
application/**/*

# the subfolder(s) you want to track:
!application/language/gr/
Run Code Online (Sandbox Code Playgroud)

这样,您就不必"unignore"要跟踪的子文件夹的父目录.


使用Git 2.17.0(不确定在此版本之前的早期版本.可能回到1.8.2),使用**模式结合排除每个子目录导致文件的工作.例如:

# assuming the root folder you want to ignore is 'application'
application/**

# Explicitly track certain content nested in the 'application' folder:
!application/language/
!application/language/gr/
!application/language/gr/** # Example adding all files & folder in the 'gr' folder
!application/language/gr/SomeFile.txt # Example adding specific file in the 'gr' folder
Run Code Online (Sandbox Code Playgroud)

  • 唉,这失败了(Git 1.8.4.msysgit.0),因为模式`**`可以匹配零子文件夹,`*`将匹配`language`并将其排除,防止包含`gr`.完整的父母@Chris Johnson推荐链似乎仍然是必要的. (8认同)
  • 听起来很完美,但它对git 2.3.7 ...`/ www/**/*!/ www/config.xml!/ www/res` config.xml不起作用,res目录仍然被忽略. (3认同)

Kat*_*tie 17

关于这一点有很多类似的问题,所以我会发布我之前写过的内容:

我在机器上工作的唯一方法是这样做:

# Ignore all directories, and all sub-directories, and it's contents:
*/*

#Now ignore all files in the current directory 
#(This fails to ignore files without a ".", for example 
#'file.txt' works, but 
#'file' doesn't):
*.*

#Only Include these specific directories and subdirectories:
!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个子目录,我仍然需要拼写出来.

这是来自@ Yarin的评论:https://stackoverflow.com/a/5250314/1696153

这些是有用的主题:

我也试过了

*
*/*
**/**
Run Code Online (Sandbox Code Playgroud)

**/wp-content/themes/**

要么 /wp-content/themes/**/*

这些也不适用于我.很多线索和错误!

  • 这比 javascript 作为一门语言本身更尴尬。为什么这么简单的问题会有这么多不同且荒谬的答案?(我批评的是 git,而不是你的解决方案)。不,git 还不错,但它有一些不好的部分。 (3认同)
  • 最好的答案,谢谢!完美的工作. (2认同)
  • 注意:请记住,.gitignore 文件中的顺序也很重要,因此请确保将“!”规则放在底部。 (2认同)

小智 14

我想跟踪 Nagios 配置文件/etc/nagios/以及/usr/lib64/nagios/plugins/. 为此,我初始化了一个 git 存储库/并使用了以下排除列表:

/*
!etc
etc/*
!etc/nagios
!usr
usr/*
!usr/lib64
usr/lib64/*
!usr/lib64/nagios
usr/lib64/nagios/*
!usr/lib64/nagios/plugins
Run Code Online (Sandbox Code Playgroud)

Git 像这样沿着列表走下去:

/*             exclude everything under / ...
!etc           but include /etc back
etc/*             exclude everything under /etc/...
!etc/nagios       but include /etc/nagios back
!usr           but include /usr back
usr/*             exclude everything under /usr/...
                  and so on...
Run Code Online (Sandbox Code Playgroud)


小智 14

添加一个名为.gitignore子文件夹的文件,然后填充

!/Bin/
Run Code Online (Sandbox Code Playgroud)

这对我有用!


Mak*_*min 12

我的 JetBrains IntelliJ IDEA.gitignore配置,我需要排除整个.idea文件夹,除了.idea/runConfigurations

.idea
!.idea/
.idea/*
!.idea/runConfigurations/
Run Code Online (Sandbox Code Playgroud)

更多详细信息:
https://github.com/daggerok/gitignore-idea-runConfigurations#exclude-idea- except-idearunco​​nfigurations

  • 顺便说一句,对我来说,使用 `.idea/*` 和 `!.idea/runConfigurations/` 就足够了。不需要写`.idea`和`!.idea/`。 (2认同)

小智 11

我发现只有这个确实有效.

**/node_modules/*
!**/node_modules/keep-dir
Run Code Online (Sandbox Code Playgroud)


D. *_*ble 10

最简单也可能是最好的方法是尝试手动添加文件(通常这优先于.gitignore-style 规则):

git add /path/to/module
Run Code Online (Sandbox Code Playgroud)

-f如果文件已被忽略,您可能需要。你甚至可以在-N 意图添加标志,建议您它们添加,但不是马上。我经常为尚未准备好上演的新文件执行此操作。


这是发布在很容易成为重复 QA的答案的副本。我将它重新发布在这里是为了提高可见性——我发现不要把 gitignore 规则弄得一团糟更容易。

  • 感谢您的回答。我必须添加 -f 因为它位于我的 .gitignore 中,如下所示: git add -f path/to/file (2认同)

bla*_*amb 8

我在这里发现了一个类似的情况,默认情况下在 laravel 中,.gitignore忽略所有使用 asterix,然后覆盖公共目录。(这也是与主要答案@Chris Johnsen 相同的解决方案,只是更薄更简洁。)

*
!public
!.gitignore
Run Code Online (Sandbox Code Playgroud)

如果您遇到 OP 场景,这还不够。

如果您要提交的特定子文件夹public,譬如说例如,在你的public/products目录要包括那些一个子文件夹深如文件,包括public/products/a/b.jpg他们不会正确检测,即使你加他们特别喜欢这个!/public/products!public/products/*等。

解决方案是确保为每个路径级别添加一个条目,以覆盖它们。

*
!.gitignore
!public/
!public/*/
!public/products/
!public/products/*
!public/products/*/
!public/products/*/
!public/products/*/*
Run Code Online (Sandbox Code Playgroud)


Ali*_*Ali 8

gitignore - 指定要忽略的故意未跟踪的文件。

排除除特定目录foo/bar之外的所有内容的示例(注意/* - 没有斜杠,通配符也会排除foo/bar中的所有内容):

$ cat .gitignore
# exclude everything except directory foo/bar
/*
!/foo
/foo/*
!/foo/bar
Run Code Online (Sandbox Code Playgroud)

WordPress的另一个例子:

!/wp-content
wp-content/*
!/wp-content/plugins
wp-content/plugins/*
!wp-content/plugins/my-awesome-plugin
Run Code Online (Sandbox Code Playgroud)

更多信息在这里: https: //git-scm.com/docs/gitignore


小智 6

这对我有用:

**/.idea/**
!**/.idea/copyright/
!.idea/copyright/profiles_settings.xml
!.idea/copyright/Copyright.xml
Run Code Online (Sandbox Code Playgroud)


Abd*_*UMI 5

因此,由于许多程序员都使用node。满足此问题的用例是排除node_modules一个模块module-a,例如:

!node_modules/

node_modules/*
!node_modules/module-a/
Run Code Online (Sandbox Code Playgroud)

  • 不适用于git版本```2.10.2.windows.1`''。 (2认同)

Don*_*ong 5

添加其他答案:

!/.vs/              <== include this folder to source control, folder only, nothing else
/.vs/*              <== but ignore all files and sub-folder inside this folder
!/.vs/ProjectSettings.json <== but include this file to source control
!/.vs/config/       <== then include this folder to source control, folder only, nothing else
!/.vs/config/*      <== then include all files inside the folder
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此处输入图片说明

  • 这对我来说是最有用的答案,因为视觉效果很有帮助。谢谢! (2认同)

归档时间:

查看次数:

324313 次

最近记录:

5 年,9 月 前