Make .gitignore会忽略除少数文件之外的所有内容

And*_*rew 1531 git gitignore

我知道.gitignore文件隐藏了Git版本控制中指定的文件.我有一个项目(LaTeX),它在运行时会生成许多额外的文件(.auth,.dvi,.pdf,日志等),但我不希望这些文件被跟踪.

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

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

# Ignore everything
*

# But not these files...
script.pl
template.latex
# etc...
Run Code Online (Sandbox Code Playgroud)

Joa*_*son 2204

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

# 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/*
Run Code Online (Sandbox Code Playgroud)

  • @PandaWood这个问题提到了[这里](http://stackoverflow.com/questions/9162919/git-whitelisting-files-in-a-complex-directory-structure)(stackoverflow.com).这是由第一个表达式匹配所有内容引起的,包括目录,你永远不会否定.要修复,添加(!*/),它匹配当前目录中的所有子目录(递归).这对父答案没有帮助,即将特定文件列入白名单(如果它们在子目录中,您可能希望手动指定完整路径,或使用特定于目录的.gitignore文件). (38认同)
  • 我无法让这个工作.我忽略了一个文件夹(例如`wp /`),但我不想忽略该文件夹中的一些文件(例如`wp/path/to/some/location/*`).我可以让它工作的唯一方法是`git add -f wp/path/to/some/location/*` (33认同)
  • 看起来这也"忽略"子文件夹,这样任何名称为'script.pl'的文件都不会在根目录中找到.我现在正在使用它(使用*.pl)并且所有*.pl文件都被忽略,即使.gitignore文件下面的子目录中有很多文件也被忽略 (11认同)
  • @trusktr使用simont的`!*/`异常 (11认同)
  • 最后一条规则的Geez:`!*/`在我徒劳地尝试争论一个`.gitignore`文件工作方面发挥了重要作用.非常感谢你. (3认同)
  • @huyz不是真的,当.gitignore在repo里面时它的变化不会被忽略(和所有文件一样)..gitignore在添加之前忽略了自己,可以通过执行'git add -f .gitignore'(在尝试时提到)来解决. (2认同)
  • 如果您想忽略除一个_目录_之外的所有目录怎么办?无法使其发挥作用.. (2认同)
  • 请参阅我的答案,而不是使用 `*` 和 `!*/` 只使用 `/*`,它是等效的,并且在我看来更有意义。 (2认同)
  • @trusktr 我遇到了同样的问题,我使用了 @giuseppe-galano (您可以在下面找到),这解决了我的问题。只需按照“wp/* !wp/path/to/some/location/”的顺序写入即可。在我的例子中 `wp/* !wp/wp-content/` (2认同)
  • 重要提示:“*”指文件,而“**”指子目录。此外,Git 从上到下进行解释,因此后面的语句优先(它们可以覆盖前面的语句)。 (2认同)

Giu*_*ano 588

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

的.gitignore

pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案,但值得注意的是,对于后来遇到这个问题的人来说,`foo`和`foo/*`**不是**相同.为此,你需要**使用`foo/*`作为基本文件夹 (49认同)
  • @Mayank Pippo是意大利语中的Goofy,而Paperino是唐老鸭,冥王星和英语一样.过去,他们曾经是一个非常常见的变量名三元组,用于编程样本.很高兴再次阅读它们.. :) (19认同)
  • 对我不起作用:node_modules/*!node_modules/bootstrap (6认同)
  • 你必须包含 `!.gitignore` 否则这将不起作用。`.gitignore` 文件也不会被 git 跟踪 (2认同)
  • @simple_human 如果他忽略存储库根目录中的所有内容,或者如果他有 .gitignore 文件需要关心,在提到的目录中,那将是正确的。 (2认同)

Rya*_*lor 220

您想要使用/*而不是**/在大多数情况下使用

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

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

# Whitelist some files
!.gitignore
!README.md

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

# Whitelist 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
Run Code Online (Sandbox Code Playgroud)

上面的代码会忽略所有的文件,除了.gitignore,README.md,folder/a/file.txt,folder/a/b1/folder/a/b2/和包含在那些过去的两个文件夹的一切.(和.DS_Store*.log文件将这些文件夹中被忽略.)

显然我可以做!/folder或不做!/.gitignore.

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

  • 我内在的语法荧光笔对你帖子的标题有一种奇怪的反应. (6认同)
  • 非常聪明,谢谢.IMO,这是使用最少包含规则的WordPress网站的最佳方式.此外,未经您的明确许可,不会删除或添加任何新的插件,主题或WP更新.使用GitHub Desktop时,我发现`.DS_Store`文件不会被忽略,但是通过命令行这不是问题.为了解决这个问题,我不得不将"**/.DS_Store"移到所有其他规则之下. (3认同)
  • 格拉西亚斯!您还可以将文件夹中的文件列入白名单..!/some/folder/file.txt (2认同)
  • @dallin 没有区别。一个将文件夹列入白名单,另一个将该文件夹的子文件夹列入白名单,两者的作用相同。唯一的问题是文件夹或文件的父级必须先列入白名单,然后才能将其列入白名单,因此您不能执行 `/*` 然后 `!/nested/folder/*` (或等效的 `!/nested/folder` )而不先执行 `!/nested/` 或 `!/nested`!(可能还有中间的“/nested/*”)。要记住的一个技巧是,如果您将一个深层嵌套的子文件夹列入白名单,则需要将每个子文件夹中的子文件夹一直黑名单,并且这些黑名单将以“斜杠星号”结尾...... (2认同)

Nik*_*Nik 69

更具体一点:

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

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

失败

webroot/cache*
!webroot/cache/.htaccess
Run Code Online (Sandbox Code Playgroud)

作品

webroot/cache/*
!webroot/cache/.htaccess
Run Code Online (Sandbox Code Playgroud)


Suv*_*apa 54

# ignore these
*
# except foo
!foo
Run Code Online (Sandbox Code Playgroud)


sla*_*nje 27

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

例如,忽略文件夹"application"中的所有内容,除了index.php和文件夹"config" 注意顺序.

你必须首先否定你想要的东西.

失败

application/*

!application/config/*

!application/index.php

作品

!application/config/*

!application/index.php

application/*

  • 错误.在"忽略一切"之后你否定了. (36认同)
  • 如果`git status`不会告诉你它在看什么以及什么被忽略,那你怎么知道这是否有效? (8认同)
  • 不要被 `git status` 愚弄以检查您对 `.gitignore` 的更改是否按预期工作 (3认同)

frn*_*ntn 24

有与 OP 类似的问题,但前 10 名赞成的答案中没有一个实际有效
我终于发现了以下内容

错误的语法:

*
!bin/script.sh
Run Code Online (Sandbox Code Playgroud)

正确的语法:

*
!bin
!bin/script.sh
Run Code Online (Sandbox Code Playgroud)

来自gitignore 手册页的解释:

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

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

扩展示例:

$树。

.
??? .gitignore
??? bin
    ??? ignore.txt
    ??? sub
        ??? folder
            ??? path
                ??? other.sh
                ??? script.sh
Run Code Online (Sandbox Code Playgroud)

$猫.gitignore

*
!.gitignore
!bin
!bin/sub
!bin/sub/folder
!bin/sub/folder/path
!bin/sub/folder/path/script.sh
Run Code Online (Sandbox Code Playgroud)

$ git status --untracked-files --ignored

On branch master

No commits yet

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

Ignored files:
  (use "git add -f <file>..." 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)
Run Code Online (Sandbox Code Playgroud)


Rob*_*anu 19

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


小智 15

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

!app/

app/*
!app/bower_components/

app/bower_components/*
!app/bower_components/highcharts/
Run Code Online (Sandbox Code Playgroud)

这将忽略bower_components除里面的所有文件/子文件夹/highcharts.


Kat*_*tie 13

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

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

# 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 and files if you wish:
!wordpress/somefile.jpg
!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/**/*

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

  • 这是唯一包含某些文件扩展名的唯一方法。更好的是,它使ASCII艺术变得很棒:https://gist.github.com/Wolfgange3311999/91c671f5e4d3c56cf5a1 (2认同)

Dan*_*sen 12

不确定是否已经指出了这一点,但我在使用我!希望 gitignore 忽略的文件夹前面的 忽略被忽略文件夹内的子文件夹时遇到了麻烦。

但是我发现被忽略的文件夹的*路径中没有。我的 .gitignore 看起来像这样:

/folder/
!/folder/subfolder
Run Code Online (Sandbox Code Playgroud)

子文件夹仍然被忽略,但添加*后文件夹使其工作起来像这样

/folder/*
!/folder/subfolder
Run Code Online (Sandbox Code Playgroud)


Rei*_*ken 11

这里有很多复杂的答案......这是我在上面没有看到的非常简单的东西,并且在许多情况下都运行良好:

# ignore all files that have an extension
**/*.*

# except for these extensions
!**/*.extension1
!**/*.extension2
Run Code Online (Sandbox Code Playgroud)

这不会忽略任何无扩展名文件,但如果您有一堆具有相同或相似名称的文件,您可以为这些文件添加排除项

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


Fab*_*one 10

我有一个子文件夹的问题.

不起作用:

/custom/*
!/custom/config/foo.yml.dist
Run Code Online (Sandbox Code Playgroud)

作品:

/custom/config/*
!/custom/config/foo.yml.dist
Run Code Online (Sandbox Code Playgroud)


Dmi*_*nko 8

这对我有用,我只想在回购中提交一个Cordova插件:

...
plugins/*
!plugins/cordova-plugin-app-customization
Run Code Online (Sandbox Code Playgroud)


Nik*_*kos 8

我处理此问题的最简单方法是强制添加文件。即使它被埋藏或嵌套在 git 忽略的子目录树中,它也会在 git 中被考虑。

例如:

x64 文件夹被排除在 .gitignore 中:

x64/
Run Code Online (Sandbox Code Playgroud)

myFile.py但您想包含位于目录中的文件x64/Release/。那么你必须:

git add -f x64/Release/myFile.py
Run Code Online (Sandbox Code Playgroud)

您可以对匹配模式的多个文件执行此操作,例如

git add -f x64/Release/myFile*.py
Run Code Online (Sandbox Code Playgroud)

等等。


Mar*_*nda 7

我有来自凉亭的Jquery和Angular.鲍尔安装了它们

/public_html/bower_components/jquery/dist/bunch-of-jquery-files
/public_html/bower_components/jquery/src/bunch-of-jquery-source-files
/public_html/bower_components/angular/angular-files
Run Code Online (Sandbox Code Playgroud)

最小化的jquery位于dist目录中,angular位于angular目录内.我只需要将最小化的文件提交给github.有些人篡改了.gitignore这就是我设法让人想起......

/public_html/bower_components/jquery/*
!public_html/bower_components/jquery/dist
/public_html/bower_components/jquery/dist/*
!public_html/bower_components/jquery/dist/jquery.min.js
/public_html/bower_components/angular/*
!public_html/bower_components/angular/angular.min.js
Run Code Online (Sandbox Code Playgroud)

希望有人能发现这个有用.


Tim*_* B. 7

我尝试了上面给出的所有答案,但没有一个对我有用.在阅读了gitignore文档(这里)之后,我发现如果你首先排除了一个文件夹,那么子文件夹中的文件名就没有被索引.因此,如果您之后使用感叹号来包含文件,则它在索引中找不到,因此不会包含在您的git客户端中.

这是找到解决方案的方法.我开始为我的文件夹树中的所有子文件夹添加例外以使其正常工作,这是一个很糟糕的工作.之后我能够将详细配置压缩到下面的配置,这与文档有点相反.

工作.gitignore:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' subfolder
/Pro/3rdparty/*
!Pro/3rdparty/domain/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/
Run Code Online (Sandbox Code Playgroud)

结果我在我的git客户端中看到,只有Pro/3rdparty/domain/modulename /文件夹中的两个文件正在进行下一次提交,这正是我想要的.

如果您需要将同一文件夹的多个子文件夹列入白名单,请将排除语句下方的感叹号行分组,如下所示:

# Ignore the 'Pro' folder, except for the '3rdparty' subfolder 
/Pro/*
!Pro/3rdparty/

# Ignore the '3rdparty' folder, except for the 'domain' & 'hosting' subfolders
/Pro/3rdparty/*
!Pro/3rdparty/domain/
!Pro/3rdparty/hosting/

# Ignore the 'domain' folder, except for the 'modulename' subfolder
Pro/3rdparty/domain/*
!Pro/3rdparty/domain/modulename/

# Ignore the 'hosting' folder, except for the 'modulename' subfolder
Pro/3rdparty/hosting/*
!Pro/3rdparty/hosting/modulename/
Run Code Online (Sandbox Code Playgroud)

否则它不会按预期工作.


the*_*e64 7

我知道我参加晚会很晚,但这是我的答案。

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

# Ignore everything
*

# But not these files...
!.gitignore
!someFile.txt
Run Code Online (Sandbox Code Playgroud)

但是,如果文件位于嵌套目录中,则手动编写规则会有点困难。

例如,如果我们要跳过git项目中的所有文件,而不是a.txt中的文件aDir/anotherDir/someOtherDir/aDir/bDir/cDir。然后,我们.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
Run Code Online (Sandbox Code Playgroud)

上面给定的.gitignore文件将跳过除目录之外的所有目录和文件!aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt

您可能已经注意到,很难定义这些规则。

为了解决这个问题,我创建了一个名为git-do-not-ignore的简单控制台应用程序,它将为您生成规则。我主持的项目github上有详细的说明。

使用范例

java -jar git-do-not-ignore.jar "aDir/anotherDir/someOtherDir/aDir/bDir/cDir/a.txt"
Run Code Online (Sandbox Code Playgroud)

输出量

!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
Run Code Online (Sandbox Code Playgroud)

还有提供一个简单的Web版本在这里

谢谢。


zok*_*zok 6

这是我的方法:

# Ignore everything
*

# Whitelist anything that's a directory
!*/

# Whitelist some files
!.gitignore

# Whitelist this folder and everything inside of it
!wordpress/wp-content/themes/my-theme/**

# Ignore this folder inside that folder
wordpress/wp-content/themes/my-theme/node_modules

# Ignore this file recursively
**/.DS_Store
Run Code Online (Sandbox Code Playgroud)

用于gig status -u递归地查看未跟踪目录中的单个文件- git status您只会看到文件夹,这可能使您误以为它们中的所有内容都已被跟踪


Mil*_*uss 6

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

/data/*
!/data/README.md

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

!/data/output/
/data/output/*
!/data/output/README.md
Run Code Online (Sandbox Code Playgroud)


d.r*_*aev 5

如果您需要忽略除少数文件和少数文件夹以外的所有内容,则为简单的解决方案:

/*
!.gitignore
!showMe.txt
!my_visible_dir
Run Code Online (Sandbox Code Playgroud)

魔术已经存在/*(如上所述),它会(而不是递归)忽略(根)文件夹中的所有内容。


raf*_*191 5

我得到了这个工作

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib
Run Code Online (Sandbox Code Playgroud)