问:

我了解 .gitignore 文件掩盖了 Git 版本控制中的指定文件。我有一个项目 (LaTeX) 在运行时会生成大量额外文件(.auth、.dvi、.pdf、日志等),但我不希望跟踪这些文件。

我知道我可以(也许应该)将所有这些文件放在项目中的一个单独的子文件夹中,因为我可以忽略该文件夹。

但是,是否有任何可行的方法将输出文件保留在项目树的根目录中并使用 .gitignore 忽略除我使用 Git 跟踪的文件之外的所有内容?就像是

# Ignore everything
*

# But not these files...
script.pl
template.latex
# etc...

答1:

HuntsBot周刊–不定时分享成功产品案例,学习他们如何成功建立自己的副业–huntsbot.com

一个可选的前缀!这否定了模式;任何被先前模式排除的匹配文件都将再次包含在内。如果否定模式匹配,这将覆盖较低优先级的模式源。

# Ignore everything
*

# But not these files...
!.gitignore
!script.pl
!template.latex
# etc...

# ...even if they are in subdirectories
!*/

# if the files to be tracked are in subdirectories
!*/a/b/file1.txt
!*/a/b/c/*

看起来这也“忽略”了子文件夹,因此无论如何都不会在根目录以外的任何地方找到任何名为“script.pl”的文件。我现在正在使用它(使用 *.pl)并且所有 *.pl 文件都被忽略,即使 .gitignore 文件下面的子目录中有很多文件

@PandaWood 提出了这个问题here(stackoverflow.com)。这是由第一个匹配所有内容(包括目录)的表达式引起的,您永远不会否定它。要修复,添加 (!*/),它匹配当前目录中的任何子目录(递归)。这无助于将特定文件列入白名单的父答案(如果它们位于子目录中,您可能需要手动指定完整路径,或使用特定于目录的 .gitignore 文件)。

我无法让它工作。我忽略了一个文件夹(例如 wp/),但我不想忽略该文件夹深处的一些文件(例如 wp/path/to/some/location/*)。我可以让它工作的唯一方法是做git add -f wp/path/to/some/location/*

@trusktr 使用 simont 的 !*/ 异常

天哪,最后一条规则:!*/ 使我徒劳地尝试使 .gitignore 文件正常工作。非常感谢。

答2:

一个优秀的自由职业者,应该有对需求敏感和精准需求捕获的能力,而huntsbot.com提供了这个机会

如果你想忽略一个目录的全部内容,除了其中的一个文件,你可以为文件路径中的每个目录编写一对规则。例如 .gitignore 忽略 pippo/pluto/paperino.xml 以外的 pippo 文件夹

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml

请注意,如果您只是在上面写了:

pippo/*
!pippo/pluto/paperino.xml

它不起作用,因为中间的 pluto 文件夹对 Git 不存在,所以 paperino.xml 找不到存在的地方。

很好的答案,但对于以后遇到此问题的任何人来说,值得注意的是 foo 和 foo/* 不相同。为此,您需要使用 foo/* 作为基本文件夹

@Mayank Pippo 在意大利语中是 Goofy,而 Paperino 是唐老鸭,而 Pluto 在英语中是一样的。在过去,它们曾经是用于编程示例的非常常见的三元组变量名。很高兴再次阅读它们.. :)

对我不起作用: node_modules/* !node_modules/bootstrap

您必须包括 !.gitignore 否则这将不起作用。 .gitignore 文件也不会被 git 跟踪

@simple_human 如果他忽略存储库根目录中的所有内容,或者如果他在提到的目录中有 .gitignore 文件要关心,那将是真的。

答3:

huntsbot.com聚合了超过10+全球外包任务平台的外包需求,寻找外包任务与机会变的简单与高效。

在大多数情况下,您希望使用 /* 而不是 * 或 */

使用 * 是有效的,但它以递归方式工作。从那时起,它不会查看目录。人们建议再次使用 !/ 来包含目录,但实际上最好使用 / 将最高级别的文件夹列入黑名单

# Blocklist files/folders in same directory as the .gitignore file
/*

# Includelist some files
!.gitignore
!README.md

# Ignore all files named .DS_Store or ending with .log
**/.DS_Store
**.log

# Includelist folder/a/b1/ and folder/a/b2/
# trailing "/" is optional for folders, may match file though.
# "/" is NOT optional when followed by a *
!folder/
folder/*
!folder/a/
folder/a/*
!folder/a/b1/
!folder/a/b2/
!folder/a/file.txt

# Adding to the above, this also works...
!/folder/a/deeply
/folder/a/deeply/*
!/folder/a/deeply/nested
/folder/a/deeply/nested/*
!/folder/a/deeply/nested/subfolder

上面的代码将忽略除 .gitignore、README.md、folder/a/file.txt、folder/a/b1/ 和 folder/a/b2/ 以及最后两个文件夹中包含的所有文件之外的所有文件。 (并且这些文件夹中的 .DS_Store 和 *.log 文件将被忽略。)

显然我也可以这样做,例如 !/folder 或 !/.gitignore。

更多信息:http://git-scm.com/docs/gitignore

非常聪明,谢谢。 IMO,对于包含规则最少的 WordPress 网站来说,这是最好的方法。此外,未经您的明确许可,不会删除或添加任何新插件、主题或 WP 更新。使用 GitHub Desktop 时,我发现 .DS_Store 文件不会被忽略,但通过命令行这不是问题。为了解决这个问题,我不得不将 **/.DS_Store 移到所有其他规则之下。

我的内部语法荧光笔对您的帖子标题有奇怪的反应。

格拉西亚斯!您还可以将文件夹中的文件列入白名单.. !/some/folder/file.txt

@dallin 没有区别。一个将文件夹列入白名单,另一个将文件夹的子文件夹列入白名单,它们的作用相同。唯一的问题是必须先将文件夹或文件的父级列入白名单,然后才能将其列入白名单,因此如果不先执行 !/nested/ 或 !/nested,您将无法执行 /* 然后 !/nested/folder/*(或等效的 !/nested/folder) ! (可能介于 /nested/* 之间)。要记住的一个技巧是,如果您将深度嵌套的子文件夹列入白名单,则需要将每个子文件夹中的子文件夹一直涂黑,而这些黑名单将以“斜线星”结尾...

为什么 **/.DS_Store 有反斜杠,而 **.log 没有?

答4:

HuntsBot周刊–不定时分享成功产品案例,学习他们如何成功建立自己的副业–huntsbot.com

更具体一点:

示例:忽略 webroot/cache 中的所有内容 - 但保留 webroot/cache/.htaccess。

注意 cache 文件夹后面的斜杠 (/):

失败

webroot/cache*
!webroot/cache/.htaccess

作品

webroot/cache/*
!webroot/cache/.htaccess

如果您使用的是 VS Code,请注意不要在将其应用于具有 .gitkeep 的文件夹后误读文件颜色,因为文件夹名称将变为绿色,但 .gitkeep 文件将变为灰色,就像所有其他被忽略的文件一样; .gitkeep 看起来像是被忽略了,但您应该看到右侧有一个“U”,表明它已被检测到

答5:

huntsbot.com汇聚了国内外优秀的初创产品创意,可按收入、分类等筛选,希望这些产品与实践经验能给您带来灵感。

# ignore these
*
# except foo
!foo

答6:

huntsbot.com精选全球7大洲远程工作机会,涵盖各领域,帮助想要远程工作的数字游民们能更精准、更高效的找到对方。

让我们工具化!

正如@Joakim 所说,要忽略文件,您可以使用如下所示的内容。

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt

但如果文件位于嵌套目录中,则编写这些规则有点困难。

例如,如果我们要跳过所有文件,但不跳过位于 aDir/anotherDir/someOtherDir/aDir/bDir/cDir 中的 a.txt。然后,我们的 .gitignore 将是这样的

# Skip all files
*

# But not `aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt`
!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

这很难手写。

为了解决这个障碍,我创建了一个名为 git-do-not-ignore 的 Web 应用程序,它将为您生成规则。

工具

https://theapache64.github.io/git-do-not-ignore/

演示

样本输入

aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

样本输出

!aDir/
aDir/*
!aDir/anotherDir/
aDir/anotherDir/*
!aDir/anotherDir/someOtherDir/
aDir/anotherDir/someOtherDir/*
!aDir/anotherDir/someOtherDir/aDir/
aDir/anotherDir/someOtherDir/aDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/
aDir/anotherDir/someOtherDir/aDir/bDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/
aDir/anotherDir/someOtherDir/aDir/bDir/cDir/*
!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

干得好,好工具!它为我节省了很多工作。

出于某种原因,我不允许从该页面复制结果...

完美,现在工作!

这个工具很棒。感谢分享。

正是我想要的。节省了时间,精力,而且这使它没有错误!很棒的应用程序!感谢您创建它!

答7:

打造属于自己的副业,开启自由职业之旅,从huntsbot.com开始!

要忽略目录中的某些文件,您必须按正确的顺序执行此操作:

例如,忽略文件夹“application”中除 index.php 和文件夹“config”之外的所有内容,注意顺序。

你必须首先否定你想要的。

失败

application/*

!application/config/*

!application/index.php

作品

!application/config/*

!application/index.php

application/*

错误的。你在“忽略一切”之后否定。

不要被 git status 愚弄,检查您对 .gitignore 的更改是否按预期工作

如果 git status 不会告诉您它正在查看什么以及忽略了什么,那么您如何知道这是否有效?

@kris 先清除 git 缓存。首先执行“git rm --cached *”然后您可以执行“git add --all”然后“git status”将显示所有新的 gitignore 更改。

你也可以使用**

答8:

一个优秀的自由职业者,应该有对需求敏感和精准需求捕获的能力,而huntsbot.com提供了这个机会

有与 OP 类似的问题,但前 10 名赞成的答案都没有真正起作用。我终于发现了以下内容

错误的语法:

*
!bin/script.sh

正确的语法:

*
!bin
!bin/script.sh

来自 gitignore man page 的解释:

可选前缀“!”这否定了模式;任何被先前模式排除的匹配文件都将再次包含在内。如果排除了该文件的父目录,则无法重新包含该文件。出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都无效,无论它们是在哪里定义的。

这意味着上面的“错误语法”是错误的,因为 bin/script.sh 不能被重新包含,因为 bin/ 被忽略了。就这样。

扩展示例:

$树。

.
├── .gitignore
└── bin
    ├── ignore.txt
    └── sub
        └── folder
            └── path
                ├── other.sh
                └── script.sh

$ 猫 .gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh

$ git status --untracked-files --ignored

On branch master

No commits yet

Untracked files:
  (use "git add ..." to include in what will be committed)
        .gitignore
        bin/sub/folder/path/script.sh

Ignored files:
  (use "git add -f ..." to include in what will be committed)
        bin/ignore.txt
        bin/sub/folder/path/other.sh

nothing added to commit but untracked files present (use "git add" to track)

谢谢伙计,这应该是公认的答案!比其他帖子更漂亮,更容易阅读

答9:

HuntsBot周刊–不定时分享成功产品案例,学习他们如何成功建立自己的副业–huntsbot.com

这就是我在忽略其他所有内容的同时保留文件夹结构的方式。您必须在每个目录(或 .gitkeep)中有一个 README.md 文件。

/data/*
!/data/README.md

!/data/input/
/data/input/*
!/data/input/README.md

!/data/output/
/data/output/*
!/data/output/README.md

这就是我要找的。但这真的是允许一个文件在一个被忽略的嵌套目录中的最简单的方法吗?我的有好几层深...

我认为您只需要为该文件创建一个例外,并且必须遵循父目录。

不幸的是,我认为这是最简单的方法,是的,如果这样可以使事情变得更容易,则必须遵循父目录。

答10:

huntsbot.com提供全网独家一站式外包任务、远程工作、创意产品分享与订阅服务!

您可以使用 git config status.showUntrackedFiles no,所有未跟踪的文件将对您隐藏。详见man git-config。

如果您只想提交多个文件中的几个,这很棒!

答11:

与HuntsBot一起,探索全球自由职业机会–huntsbot.com

要从 .gitignore 中排除文件夹,可以执行以下操作。

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/

这将忽略 bower_components 中除 /highcharts 之外的所有文件/子文件夹。

原文链接:https://www.huntsbot.com/qa/aLZP/make-gitignore-ignore-everything-except-a-few-files?lang=zh_CN&from=csdn

huntsbot.com聚合了超过10+全球外包任务平台的外包需求,寻找外包任务与机会变的简单与高效。

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐