简体   繁体   English

如何忽略Git中目录中的文件?

[英]How do I ignore files in a directory in Git?

What is the proper syntax for the .gitignore file to ignore files in a directory? .gitignore文件忽略目录中文件的正确语法是什么?

Would it be 可不可能是

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

?

PATTERN FORMAT 模式格式

  • 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). 换句话说, foo/将匹配目录foo和它下面的路径,但是不匹配常规文件或符号链接foo (这与pathpec在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). 如果模式不包含斜杠/ ,git将其视为shell glob模式,并检查相对于.gitignore文件位置的路径名的匹配(相对于工作树的顶层,如果不是来自.gitignore文件)。

  • 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. 否则,git将模式视为适合fnmatch(3)使用FNM_PATHNAME标志的shell glob:模式中的通配符与路径名中的/不匹配。 For example, Documentation/*.html matches Documentation/git.html but not Documentation/ppc/ppc.html or tools/perf/Documentation/perf.html . 例如, Documentation/*.htmlDocumentation/git.html匹配,但不匹配Documentation/git.html Documentation/ppc/ppc.htmltools/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 . 例如,/ * /*.c匹配cat-file.c /*.c但不匹配mozilla-sha1/sha1.c

You can find more here 你可以在这里找到更多

git help gitignore
or 要么
man gitignore

It would be the former. 这将是前者。 Go by extensions as well instead of folder structure. 也可以通过扩展而不是文件夹结构。

Ie my example C# development ignore file: 即我的示例C#development忽略文件:

#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
~$*

Update 更新

I thought I'd provide an update from the comments below. 我想我会从下面的评论中提供更新。 Although not directly answering the OP's question, see the following for more examples of .gitignore syntax. 虽然没有直接回答OP的问题,但有关.gitignore语法的更多示例,请参阅以下内容。

Community wiki (constantly being updated): 社区维基(不断更新):

.gitignore for Visual Studio Projects and Solutions .gitignore for Visual Studio项目和解决方案

More examples with specific language use can be found here (thanks to Chris McKnight's comment): 可以在这里找到更多具有特定语言用法的例子(感谢Chris McKnight的评论):

https://github.com/github/gitignore https://github.com/github/gitignore

Paths which contain slashes are taken to be relative to the directory containing the .gitignore file - usually the top level of your repository, though you can also place them in subdirectories. 包含斜杠的路径被认为是相对于包含.gitignore文件的目录 - 通常是存储库的顶层,但您也可以将它们放在子目录中。

So, since in all of the examples you give, the paths contain slashes, the two versions are identical. 所以,因为在你提供的所有例子中,路径都包含斜杠,两个版本是相同的。 The only time you need to put a leading slash is when there isn't one in the path already. 只有在路径中没有一个时,才需要设置一个前导斜杠。 For example, to ignore foo only at the top level of the repository, use /foo . 例如,要仅在存储库的顶级忽略foo,请使用/foo Simply writing foo would ignore anything called foo anywhere in the repository. 简单地写foo会忽略存储库中任何名为foo的东西。

Your wildcards are also redundant. 你的通配符也是多余的。 If you want to ignore an entire directory, simply name it: 如果要忽略整个目录,只需将其命名为:

lib/model/om

The only reason to use wildcards the way you have is if you intend to subsequently un-ignore something in the directory: 使用通配符的唯一原因是,如果您打算随后忽略目录中的某些内容:

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

A leading slash indicates that the ignore entry is only to be valid with respect to the directory in which the .gitignore file resides. 前导斜杠表示忽略条目仅对.gitignore文件所在的目录有效。 Specifying *.o would ignore all .o files in this directory and all subdirs, while /*.o would just ignore them in that dir, while again, /foo/*.o would only ignore them in /foo/*.o. 指定*.o将忽略此目录和所有子目录中的所有.o文件,而/*.o *.o将在该目录中忽略它们,而/foo/*.o只会忽略它们在/foo/*.o中。

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

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 (删除未跟踪但保留被忽略的文件),则所有被忽略并附加/*将被不可逆转地删除

Some background 一些背景

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 statusgit status --ignored也会给出一个略有不同的图片。

How to reproduce 如何重现

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

If you do a git status --ignored here, you'll get: 如果你做一个git status --ignored在这里git status --ignored ,你会得到:

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/

Now either do 现在要么做

git stash -u
git 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开发列表,看看他们对它的看法。

The first one. 第一个。 Those file paths are relative from where your .gitignore file is. 这些文件路径与.gitignore文件的位置相对。

It would be: 这将是:

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

or possibly even: 甚至可能:

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

in case that filter and form are the only directories in lib that do have a base subdirectory that needs to be ignored (see it as an example of what you can do with the asterics). 如果filterform是lib中唯一具有需要忽略的base子目录的目录(请参阅它作为您可以使用星号进行操作的示例)。

I'm maintaining a GUI and CLI based service that allows you to generate .gitignore templates very easily at https://www.gitignore.io . 我正在维护基于GUI和CLI的服务,允许您在https://www.gitignore.io上轻松生成.gitignore模板。

You can either type the templates you want in the search field or install the command line alias and run 您可以在搜索字段中键入所需的模板,也可以安装命令行别名并运行

$ gi swift,osx

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

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

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