【问题标题】:Gitignore all folders beginning with a periodGitignore 以句点开头的所有文件夹
【发布时间】:2016-04-28 15:03:36
【问题描述】:

我想使用.gitignore 文件来忽略所有以句点开头的文件夹(linux 的隐藏文件夹)。

我不知道语法,但我确信它很简单。

效果如何?

【问题讨论】:

  • 这不是那个问题的重复,因为这里只需要点文件夹,而不是点文件。
  • 我需要先commit新的gitignore文件才能生效

标签: git gitignore glob


【解决方案1】:

使用以下模式之一:

# ignore all . files but include . folders
.*
!.*/

# ignore all . files and . folders
.*

# Dont ignore .gitignore (this file)
# This is just for verbosity, you can leave it out if
# .gitignore is already tracked or if you use -f to
# force-add it if you just created it
!/.gitignore

# ignore all . folders but include . files
.*/

What is this pattern?

.* - 这个模式告诉 git 忽略所有以 . 开头的文件

! - 这告诉 git 不要忽略该模式。在你的情况下/.gitignore

可以在这个答案中找到演示:
Git: how to ignore hidden files / dot files / files with empty file names via .gitignore?

【讨论】:

  • 明确包含 .gitignore 可能是描述性的,但如果 .gitignore 已经被跟踪,或者添加了 -f,则没有必要。 :-) 除此之外,它没有回答问题,因为 OP 只要求提供文件夹,而不是文件
  • 你说得对,我错过了文件夹。文件夹应该是 ** :-)
  • 没有。他想要 only 个文件夹,而您的第二件事与第一件事相同。 .* 将匹配以点开头的任何内容(文件 文件夹),.*/ 将匹配以点开头的任何文件夹
  • 我知道,我向他展示了模式,他可以抓住他需要的东西。我同意
  • 好吧,他不能。当他接受时,我猜他不是故意的。但你的答案显然是错误的。 .* 不会忽略所有点文件,但点文件和点文件夹和 .** 应该相同。仅忽略点文件夹仍然是.*/。 :-)
【解决方案2】:

.*/ 将匹配以点开头并且是文件夹的所有内容

您可以使用以下命令对其进行测试:mkdir test && cd test && git init && mkdir -p .foo .foo/.bar foo/.bar && touch .foo/dummy .foo/.bar/dummy foo/.bar/dummy && git add . && git status && echo '.*/'>.gitignore && git reset && git add . && git status

【讨论】:

    【解决方案3】:

    您应该能够使用双星号通配符,它​​表示任意深度的目录。

    .**/
    

    【讨论】:

    • .*/ 应该不够吗?应该工作公平。更新:是的,它是
    • 如果他只想忽略第一级目录。他说“任何”。这将包括目录深度的目录。
    • 试试下面的命令,你会发现它运行良好:mkdir test && cd test && git init && mkdir -p .foo .foo/.bar foo/.bar && touch .foo/dummy .foo/.bar/dummy foo/.bar/dummy && git add . && git status && echo '.*/'>.gitignore && git reset && git add . && git status
    • 你能举个例子吗?假设我有一个名为 IgnoreMe 的文件夹,它以相同的名称出现在存储库的不同位置和深度。我怎么能忽略它的内容?
    猜你喜欢
    • 2010-12-27
    • 2015-01-15
    • 2023-03-06
    • 2015-10-25
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多