.gitignore
文件可以隐藏指定的文件,让Git不对它们进行版本控制。
我该如何告诉.gitignore
忽略除了我正在用Git跟踪的文件之外的全部文件呢?类似这样:
# Ignore everything:
*
# Do not ignore these files:
script.pl
template.latex
.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/*
wp/
),但我想不忽略那个文件夹深处的一些文件(例如,wp/path/to/some/location/*
)。我唯一能让它工作的方法是执行 git add -f wp/path/to/some/location/*
。 - trusktr!*/
异常。 - Tobias Kienzler!*/
在我的无效尝试中对让 .gitignore
文件起作用发挥了关键性作用。非常感谢你。 - racl101如果您想忽略一个目录的所有内容,除了其中一个文件之外,您可以在文件路径中为每个目录编写一对规则。例如,.gitignore
可以忽略pippo
文件夹,但不包括pippo/pluto/paperino.xml
。
pippo/*
!pippo/pluto
pippo/pluto/*
!pippo/pluto/paperino.xml
/*
进行标记。以下内容将无效:
folder
!folder/some-file.txt
但只有这个会:
folder/*
!folder/some-file.txt
pippo/*
!pippo/pluto/paperino.xml
这样做行不通,因为中间的“pluto”文件夹对于Git来说是不存在的,所以“paperino.xml”无法找到一个存在的位置。
foo
和 foo/*
不是同一个东西。为了让这个方法起作用,你 必须 使用 foo/*
作为基础文件夹。 - thislooksfun.gitignore
文件,否则这将无法工作。 .gitignore
文件也将被Git忽略。 - suhailvs在大多数情况下,您应该使用/*
而不是*
或*/
使用*
是有效的,但它会递归地工作。它不会再查看目录。人们建议使用!*/
再次包含列表目录,但实际上最好使用/*
阻止最高级文件夹。
# 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
。
.DS_Store
文件不会被忽略,但通过命令行就不会出现这个问题。为了解决这个问题,我必须将 **/.DS_Store
移到所有其他规则之下。 - Jibran!/nested/
或 !/nested
的情况下执行 /*
然后执行 !/nested/folder/*
(或等效的 !/nested/folder
)!一个记忆技巧是,如果您要将深度嵌套的子文件夹列入白名单,则需要在每个子文件夹中黑名单其子项,一直到最上层,而这些黑名单将以“斜杠星号”结尾... - Ryan Taylor稍微具体一些:
例子:忽略webroot/cache
中的所有内容,但保留webroot/cache/.htaccess
。
请注意cache
文件夹后面的斜杠(/):
失败了
webroot/cache*
!webroot/cache/.htaccess
工作
webroot/cache/*
!webroot/cache/.htaccess
让我们工具化吧!
如@Joakim所说,要忽略一个文件,您可以使用以下内容。
# Ignore everything
*
# But not these files...
!.gitignore
!someFile.txt
但是如果文件在嵌套的目录中,编写规则就有点困难。
例如,如果我们想要跳过除 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
这很难手写。
为了解决这个障碍,我创建了一个名为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
# ignore these
*
# except foo
!foo
我有与楼主类似的问题,但是前十个受欢迎的回答都实际上都没有起作用。
最终我找到了以下解决方法:
错误的语法:
*
!bin/script.sh
正确的语法:
*
!bin
!bin/script.sh
一个可选的前缀“!”表示否定该模式;任何被前面的模式排除的匹配文件将重新包含。如果排除了该文件所在的父目录,则不可能重新包含该文件。Git出于性能原因不会列出已排除的目录,因此对包含文件的任何模式都没有影响,无论它们在何处定义。
这意味着上面的“错误语法”是错误的,因为bin/
被忽略了,所以无法重新包含bin/script.sh
。就是这样。
扩展示例:
$ tree .
.
├── .gitignore
└── bin
├── ignore.txt
└── sub
└── folder
└── path
├── other.sh
└── script.sh
$ cat .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)
!*/
模式似乎重新包含了我试图排除的所有那些文件夹。但是我怀疑这个解决方案在需要排除大量文件/子文件夹时并不理想。 - Hansapplication/*
!application/config/*
!application/index.php
正确做法:
!application/config/*
!application/index.php
application/*
git status
骗了,以此来检查你对.gitignore
的更改是否按预期生效。 - Philippe Rathégit status
不能告诉你它正在查看什么以及忽略了什么内容,那么你如何知道这个功能是否有效? - krisgit config status.showUntrackedFiles no
命令,它会将所有未跟踪的文件隐藏起来。详见man git-config
了解更多细节信息。这就是我保持文件夹结构的方法,同时忽略其他所有内容。你必须在每个目录中有一个README.md文件(或.gitkeep)。
/data/*
!/data/README.md
!/data/input/
/data/input/*
!/data/input/README.md
!/data/output/
/data/output/*
!/data/output/README.md