.gitignore文件忽略目录中的文件的正确语法是什么?

会是吗

config/databases.yml
cache/*
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/*

?


第一个。这些文件路径与你的.gitignore文件的位置是相对的。


它将是:

config/databases.yml
cache
log
data/sql
lib/filter/base
lib/form/base
lib/model/map
lib/model/om

甚至可能是:

config/databases.yml
cache
log
data/sql
lib/*/base
lib/model/map
lib/model/om

如果过滤器和表单是lib中唯一需要忽略basesubdirectory的目录(可以将其视为可以使用星号的示例)。


应该是前者。使用扩展名,而不是文件夹结构。

例如,我的例子c#开发忽略文件:

#OS junk files
[Tt]humbs.db
*.DS_Store

#Visual Studio files
*.[Oo]bj
*.user
*.aps
*.pch
*.vspscc
*.vssscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.[Cc]ache
*.ilk
*.log
*.lib
*.sbr
*.sdf
ipch/
obj/
[Bb]in
[Dd]ebug*/
[Rr]elease*/
Ankh.NoLoad

#Tooling
_ReSharper*/
*.resharper
[Tt]est[Rr]esult*

#Project files
[Bb]uild/

#Subversion files
.svn

# Office Temp Files
~$*

更新

我想我应该从下面的评论中提供一个更新。虽然没有直接回答OP的问题,但请参阅以下有关.gitignore语法的更多示例。

社区wiki(不断更新):

.gitignore用于Visual Studio项目和解决方案

更多关于特定语言使用的例子可以在这里找到(感谢Chris McKnight的评论):

https://github.com/github/gitignore


模式的格式

A blank line matches no files, so it can serve as a separator for readability. A line starting with # serves as a comment. An optional prefix ! which negates the pattern; any matching file excluded by a previous pattern will become included again. If a negated pattern matches, this will override lower precedence patterns sources. If the pattern ends with a slash, it is removed for the purpose of the following description, but it would only find a match with a directory. In other words, foo/ will match a directory foo and paths underneath it, but will not match a regular file or a symbolic link foo (this is consistent with the way how pathspec works in general in git). If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file). Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3) with the FNM_PATHNAME flag: wildcards in the pattern will not match a / in the pathname. For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html or tools/perf/Documentation/perf.html. A leading slash matches the beginning of the pathname. For example, /*.c matches cat-file.c but not mozilla-sha1/sha1.c.

你可以在这里找到更多

git help gitignore 或 男人吉特诺


前导斜杠表示忽略条目仅对.gitignore文件所在的目录有效。指定*。O将忽略该目录下的所有. O文件和所有子dirs,而/*. O将忽略该目录下的所有。O会忽略dir目录下的它们,而/foo/*。O只会忽略/foo/*. O。


包含斜杠的路径相对于包含.gitignore文件的目录-通常是存储库的顶层,尽管您也可以将它们放在子目录中。

因此,由于在您给出的所有示例中,路径都包含斜杠,因此两个版本是相同的。唯一需要使用前导斜杠的时候是路径中还没有斜杠的时候。例如,要在存储库的顶层忽略foo,请使用/foo。简单地编写foo将忽略存储库中任何名为foo的东西。

通配符也是多余的。如果你想忽略整个目录,简单地命名它:

lib/model/om

使用通配符的唯一原因是如果你打算随后取消忽略目录中的某些内容:

lib/model/om/*      # ignore everything in the directory
!lib/model/om/foo   # except foo

如果你想把一个.gitignore文件放在顶层,并使它适用于它下面的任何文件夹,请使用/**/。

例如,忽略所有*。映射/src/main/文件夹和子文件夹中的文件使用:

/src/main/**/*.map

我正在维护一个基于GUI和CLI的服务,允许您在https://www.gitignore.io上非常容易地生成.gitignore模板。

您可以在搜索字段中键入所需的模板,也可以安装命令行别名并运行

$gi 斯威夫特,奥斯汀


问题中的两个例子实际上都是非常糟糕的例子,可能导致数据丢失!

我的建议是:永远不要在.gitignore文件的目录中添加/*,除非你有很好的理由!

一个很好的理由是,例如,Jefromi写道:“如果您打算随后取消忽略目录中的某些内容”。

不应该这样做的原因是,向目录追加/*一方面可以正确地忽略目录中的所有内容,但另一方面它有一个危险的副作用:

如果你在你的存储库中执行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. 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. 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. This time I had enough and investigated the issue. I finally figured that the reason is the appended /*.

我假设它可以通过以下事实来解释:目录/*忽略了目录的所有内容,而不是目录本身。因此,当内容被删除时,它既不会被认为是跟踪的,也不会被忽略。尽管git status和git status——ignored给出的图像略有不同。

如何繁殖

下面是如何重现这种行为。我目前使用的是Git 2.8.4。

将在本地git存储库中创建一个名为localdata/的目录,其中包含一个虚拟文件(important.dat),通过将/localdata/*放入.gitignore文件中,内容将被忽略。当现在执行上述两个git命令中的一个时,目录将(意外地)丢失。

mkdir test
cd test
git init
echo "/localdata/*" >.gitignore
git add .gitignore
git commit -m "Add .gitignore."
mkdir localdata
echo "Important data" >localdata/important.dat
touch untracked-file

如果你执行git status,这里忽略,你会得到:

On branch master
Untracked files:
  (use "git add <file>..." to include in what will be committed)

  untracked-file

Ignored files:
  (use "git add -f <file>..." to include in what will be committed)

  localdata/

现在不管怎样

git stash -u
git stash pop

or

git clean -df

在这两种情况下,据称被忽略的目录localdata将消失!

不确定这是否可以被认为是一个bug,但我猜它至少是一个没有人需要的功能。

我会把这个报告给git开发列表,看看他们是怎么想的。


Android Studio项目的.gitignore文件示例如下所示

# built application files
*.apk
*.ap_

# files for the dex VM
*.dex

# Java class files
*.class

# generated files
bin/
gen/

# Local configuration file (sdk path, etc)
local.properties


#Eclipse
*.pydevproject
.project
.metadata
bin/**
tmp/**
tmp/**/*
*.tmp
*.bak
*.swp
*~.nib
local.properties
.classpath
.settings/
.loadpath
YourProjetcName/.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 Eclipse
proguard/

# Intellij project files
*.iml
*.ipr
*.iws
.idea/
/build
build/
*/build/
*/*/build/
*/*/*/build/
*.bin
*.lock
YourProjetcName/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.bin
appcompat_v7_23_1_1.xml
projectFilesBackup
build.gradle
YourProjetcName.iml
YourProjetcName.iml
gradlew
gradlew.bat
local.properties
settings.gradle
.gradle
.idea
android
build
gradle