本文共 6166 字,大约阅读时间需要 20 分钟。
本文翻译自:
What is the proper syntax for the .gitignore
file to ignore files in a directory? .gitignore
文件忽略目录中文件的正确语法是什么?
Would it be 可不可能是
config/databases.ymlcache/*log/*data/sql/*lib/filter/base/*lib/form/base/*lib/model/map/*lib/model/om/*
or 要么
/config/databases.yml/cache/*/log/*/data/sql/*/lib/filter/base/*/lib/form/base/*/lib/model/map/*/lib/model/om/*
? ?
参考:
If you want to put a .gitignore file at the top level and make it work for any folder below it use /**/
. 如果要将.gitignore文件放在顶层并使其适用于下面的任何文件夹,请使用/**/
。
Eg to ignore all *.map
files in a /src/main/
folder and sub-folders use: 例如,要忽略/src/main/
文件夹和子文件夹中的所有*.map
文件,请使用:
/src/main/**/*.map
I'm maintaining a GUI and CLI based service that allows you to generate .gitignore
templates very easily at . 我正在维护基于GUI和CLI的服务,允许您在轻松生成.gitignore
模板。
You can either type the templates you want in the search field or install the command line alias and run 您可以在搜索字段中键入所需的模板,也可以安装命令行别名并运行
$ gi swift,osx
Both examples in the question are actually very bad examples that can lead to data loss! 问题中的两个例子实际上是非常糟糕的例子,可能导致数据丢失!
My advice: never append /*
to directories in .gitignore files, unless you have a good reason! 我的建议:永远不要将/*
附加到.gitignore文件中的目录,除非你有充分的理由!
A good reason would be for example what Jefromi wrote: "if you intend to subsequently un-ignore something in the directory" . 一个很好的理由是例如Jefromi写的: “如果你打算随后忽略目录中的某些东西” 。
The reason why it otherwise shouldn't be done is that appending /*
to directories does on the one hand work in the manner that it properly ignores all contents of the directory, but on the other hand it has a dangerous side effect: 不应该这样做的原因是,对目录的附加/*
一方面以正确忽略目录的所有内容的方式工作,但另一方面它具有危险的副作用:
If you execute git stash -u
(to temporarily stash tracked and untracked files) or git clean -df
(to delete untracked but keep ignored files) in your repository, all directories that are ignored with an appended /*
will be irreversibly deleted ! 如果在git stash -u
执行git stash -u
(暂时存储已跟踪和未跟踪的文件)或git clean -df
(删除未跟踪但保留被忽略的文件),则所有被忽略并附加/*
将被不可逆转地删除 !
I had to learn this the hard way. 我必须以艰难的方式学习这一点。 Somebody in my team was appending /*
to some directories in our .gitignore. 我团队中的某个人正在将/*
附加到我们的.gitignore中的某些目录中。 Over the time I had occasions where certain directories would suddenly disappear. 随着时间的推移,某些目录会突然消失。 Directories with gigabytes of local data needed by our application. 我们的应用程序需要具有千兆字节本地数据的目录。 Nobody could explain it and I always hat to re-download all data. 没有人可以解释它,我总是重新下载所有数据。 After a while I got a notion that it might have to do with git stash
. 过了一会儿,我得到了一个可能与git stash
的想法。 One day I wanted to clean my local repo (while keeping ignored files) and I was using git clean -df
and again my data was gone. 有一天,我想清理我的本地git clean -df
(同时保持被忽略的文件),我正在使用git clean -df
,我的数据又消失了。 This time I had enough and investigated the issue. 这次我受够了并调查了这个问题。 I finally figured that the reason is the appended /*
. 我终于想通了原因是附加了/*
。
I assume it can be explained somehow by the fact that directory/*
does ignore all contents of the directory but not the directory itself. 我假设它可以通过某种方式解释,即directory/*
确实忽略了目录的所有内容而不是目录本身。 Thus it's neither considered tracked nor ignored when things get deleted. 因此,当事情被删除时,它既不被视为被跟踪也不被忽略。 Even though git status
and git status --ignored
give a slightly different picture on it. 即使git status
和git status --ignored
也会给出一个略有不同的图片。
Here is how to reproduce the behaviour. 以下是如何重现行为。 I'm currently using Git 2.8.4. 我目前正在使用Git 2.8.4。
A directory called localdata/
with a dummy file in it ( important.dat
) will be created in a local git repository and the contents will be ignored by putting /localdata/*
into the .gitignore
file. 将在本地git存储库中创建名为localdata/
且其中包含虚拟文件的目录( important.dat
),并将/localdata/*
放入.gitignore
文件中以忽略内容。 When one of the two mentioned git commands is executed now, the directory will be (unexpectedly) lost. 当现在执行两个提到的git命令之一时,该目录将(意外地)丢失。
mkdir testcd testgit initecho "/localdata/*" >.gitignoregit add .gitignoregit commit -m "Add .gitignore."mkdir localdataecho "Important data" >localdata/important.dattouch untracked-file
If you do a git status --ignored
here, you'll get: 如果你做一个git status --ignored
在这里git status --ignored
,你会得到:
On branch masterUntracked files: (use "git add..." to include in what will be committed) untracked-fileIgnored files: (use "git add -f ..." to include in what will be committed) localdata/
Now either do 现在要么做
git stash -ugit stash pop
or 要么
git clean -df
In both cases the allegedly ignored directory localdata
will be gone! 在这两种情况下,据称被忽略的目录localdata
都将消失!
Not sure if this can be considered a bug, but I guess it's at least a feature that nobody needs. 不确定这是否可以被视为一个错误,但我想这至少是一个没人需要的功能。
I'll report that to the git development list and see what they think about it. 我将把它报告给git开发列表,看看他们对它的看法。
A sample .gitignore file can look like one below for a Android Studio project 对于Android Studio项目,示例.gitignore文件可能如下所示
# built application files*.apk*.ap_# files for the dex VM*.dex# Java class files*.class# generated filesbin/gen/# Local configuration file (sdk path, etc)local.properties#Eclipse*.pydevproject.project.metadatabin/**tmp/**tmp/**/**.tmp*.bak*.swp*~.niblocal.properties.classpath.settings/.loadpathYourProjetcName/.gradle/YourProjetcName/app/build/*/YourProjetcName/.gradle/*/YourProjetcName/app/build/# External tool builders.externalToolBuilders/# Locally stored "Eclipse launch configurations"*.launch# CDT-specific.cproject# PDT-specific.buildpath# Proguard folder generated by Eclipseproguard/# Intellij project files*.iml*.ipr*.iws.idea//buildbuild/*/build/*/*/build/*/*/*/build/*.bin*.lockYourProjetcName/app/build/.gradle/local.properties/.idea/workspace.xml/.idea/libraries.DS_Store.gradle/app/build/*app/build/# Local configuration file (sdk path, etc)local.properties/YourProjetcName/build/intermediates/lint-cache/api-versions-6-23.1.binappcompat_v7_23_1_1.xmlprojectFilesBackupbuild.gradleYourProjetcName.imlYourProjetcName.imlgradlewgradlew.batlocal.propertiessettings.gradle.gradle.ideaandroidbuildgradle
The first one. 第一个。 Those file paths are relative from where your .gitignore file is. 这些文件路径与.gitignore文件的位置相对。
转载地址:http://wscnb.baihongyu.com/