繁体   English   中英

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

[英]Make .gitignore ignore everything except a few files

我知道.gitignore文件隐藏了 Git 版本控制中的指定文件。

我如何告诉.gitignore忽略除我正在使用 Git 跟踪的文件之外的所有内容? 就像是:

# Ignore everything:
*

# Do not ignore these files:
script.pl
template.latex

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

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

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

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

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

pippo/*
!pippo/pluto/paperino.xml

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

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

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

# 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

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

显然,我也可以使用!/folder!/.gitignore

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

更具体一点:

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

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

失败

webroot/cache*
!webroot/cache/.htaccess

作品

webroot/cache/*
!webroot/cache/.htaccess
# ignore these
*
# except foo
!foo

让我们工具化!

正如@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 应用程序,它将为您生成规则。

工具

演示

样本输入

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

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

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

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

失败

application/*

!application/config/*

!application/index.php

作品

!application/config/*

!application/index.php

application/*

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

错误的语法:

*
!bin/script.sh

正确的语法:

*
!bin
!bin/script.sh

来自gitignore 手册页的解释:

可选前缀“!” 这否定了模式; 任何被先前模式排除的匹配文件都将再次包含在内。 如果该文件的父目录被排除,则无法重新包含该文件 出于性能原因,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 <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)

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

/data/*
!/data/README.md

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

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

您可以使用git config status.showUntrackedFiles no并且所有未跟踪的文件都将对您隐藏。 有关详细信息,请参阅man git-config

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

!app/

app/*
!app/bower_components/

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

这将忽略bower_components中的所有文件/子文件夹,除了/highcharts

有很多类似的问题,所以我将发布我之前写的内容:

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

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

请注意,您必须如何明确允许要包含的每个级别的内容。 所以如果我在主题下有 5 个子目录,我仍然需要把它拼出来。

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

这些是有用的主题:

我也试过

*
*/*
**/**

**/wp-content/themes/**

/wp-content/themes/**/*

这些都不适合我。 很多线索和错误!

我是这样做的:

# 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

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

我的子文件夹有问题。

不工作:

/custom/*
!/custom/config/foo.yml.dist

作品:

/custom/config/*
!/custom/config/foo.yml.dist

我尝试了上面给出的所有答案,但没有一个对我有用。 阅读 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/

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

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

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

否则它将无法按预期工作。

这对我有用,我只想向 repo 提交一个 Cordova 插件:

...
plugins/*
!plugins/cordova-plugin-app-customization

这里有很多复杂的答案......这是我在上面看不到的非常简单的东西,并且在许多情况下都能很好地工作:

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

# except for these extensions
!**/*.extension1
!**/*.extension2

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

**/EXTENSIONLESSFILETOIGNORE

我有来自凉亭的 Jquery 和 Angular。 Bower 将它们安装在

/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

最小化的 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

希望有人能发现这很有用。

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

/*
!.gitignore
!showMe.txt
!my_visible_dir

魔法就在/*中(如上所述),它会忽略(根)文件夹中的所有内容,但不会递归。

我解决这个问题的最简单方法是强制添加文件。 即使它被掩埋或嵌套在 git 忽略的子目录树中,它也会在 git 中进行说明。

例如:

x64 文件夹不包含在 .gitignore 中:

x64/

但是您想包含位于x64/Release/目录中的文件myFile.py 然后你必须:

git add -f x64/Release/myFile.py

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

git add -f x64/Release/myFile*.py

等等。

我得到了这个工作

# Vendor
/vendor/braintree/braintree_php/*
!/vendor/braintree/braintree_php/lib

到目前为止,没有什么对我有用,因为我试图从 lib 中添加一个 jar。

这没有用:

build/*
!build/libs/*
!build/libs/
!build/libs/myjarfile.jar 

这有效:

build/*
!build/libs

我对单个文件的否定也有一些问题。 我能够提交它们,但我的 IDE (IntelliJ) 总是抱怨被跟踪的被忽略的文件。

git ls-files -i --exclude-from .gitignore

显示了我以这种方式排除的两个文件:

public/
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

最后,这对我有用:

public/*
!public/typo3conf/
public/typo3conf/*
!public/typo3conf/LocalConfiguration.php
!public/typo3conf/PackageStates.php

关键是首先否定了typo3conf/文件夹。

此外,似乎语句的顺序无关紧要 相反,您需要明确否定所有子文件夹,然后才能否定其中的单个文件。

文件夹!public/typo3conf/和文件夹内容public/typo3conf/*对于 .gitignore 来说是两个不同的东西。

伟大的线程! 这个问题困扰了我一段时间;)

我似乎找到了其他人没有提到的对我有用的东西。

# Ignore everything
*

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

# And if you want to include a sub-directory and all sub-directory and files under it, but not all sub-directories
!subdir/
!subdir/**/*

基本上,它似乎否定了一个子目录被忽略,你必须有两个条目,一个用于子目录本身!subdir/另一个用于扩展它下的所有文件和文件夹!subdir/**/*

这是一场灾难,在使用git 16 年(2005 年)之后,当位于目录层次结构的深处时,没有简单而明显的方法来排除一个文件,否则应该被忽略。 相反,我们需要诉诸疯狂的事情,例如反复挖掘结构并以正确的顺序排除和包含目录。 一年 4 次你需要做的事情是不可能记住的。

唯一聪明但非常奇怪的选择是使用git add --force 当您不单独处理 gitted repo 时,某些时候肯定会失败。

所以我写了一个小 bash 函数来为你解决这个问题,以便于复制粘贴。 让我先解释一下单线:

f=0; y='';for x in $(echo "uploads/rubbish/stuff/KEEP_ME/a.py" | tr '\/' '\n'); do y="$y/$x"; if [ $(($f)) -eq 0 ]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo;

# output:
!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a.py

描述

  • 有一个if语句来调整第一个项目没有得到/
  • tr '\/' '\n' - 将标准 POSIX 路径中的/转换为\n (以获取列表)
  • y="$y/$x" - 在上一个目录之后添加下一个子目录(在列表中)。
  • echo -e '!'"$y/\n$y/*" -
    打印 2 行子目录项,第 1 行带有! prefix ,第二个是/* postfix
  • sed '$d' - 删除最后一行
  • sed -z 's/..$//' - 从最后一行删除最后 2 个字符/*

然后我们创建函数:

function gitkeep () { f=0; y='';for x in $(echo "$1" | tr '\/' '\n'); do y="$y/$x"; if [[ f -eq 0 ]]; then y="$x"; f=1; fi; echo -e '!'"$y/\n$y/*"; done | sed '$d' |sed -z 's/..$//'; echo; }

# gitkeep "uploads/rubbish/stuff/KEEP_ME/a"

!uploads/
uploads/*
!uploads/rubbish/
uploads/rubbish/*
!uploads/rubbish/stuff/
uploads/rubbish/stuff/*
!uploads/rubbish/stuff/KEEP_ME/
uploads/rubbish/stuff/KEEP_ME/*
!uploads/rubbish/stuff/KEEP_ME/a

这里我将算术if语句简化为if [[ f -eq 0 ]]; .

享受!

要旨

# Ignore everything
*

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

可能包括:

!.gitignore

参考

来自https://git-scm.com/docs/gitignore

一个可选的前缀“ ! ”否定模式; 任何被先前模式排除的匹配文件都将再次包含在内。 如果排除了该文件的父目录,则无法重新包含该文件。 出于性能原因,Git 不会列出排除的目录,因此包含文件的任何模式都无效,无论它们是在哪里定义的。 对于以文字“ ! ”开头的模式,例如“ \!important!.txt ”,在第一个“ ! ”前面放置一个反斜杠(“ \ ”)。

...

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

 $ cat .gitignore # exclude everything except directory foo/bar /* !/foo /foo/* !/foo/bar

通过github.com/gitinclude,您可以下载一个命令行工具(适用于 windows、linux 和 osx),用于包含深度嵌套的文件集。

.gitinclude中定义文件集并执行该工具(通过双击或命令行)。 您的.gitignore现已更新。

不确定是否已经指出了这一点,但我无法使用!忽略忽略文件夹中的子文件夹。 在我希望 gitignore 忽略的文件夹前面。

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

/folder/
!/folder/subfolder

子文件夹仍然被忽略,但是在文件夹之后添加*使它像这样工作

/folder/*
!/folder/subfolder

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM