使 git 忽略文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3220629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 08:43:07  来源:igfitidea点击:

Make git ignore files

git

提问by Victor Bjelkholm

I'm trying to make git ignore some of my files and I found one description about how you could do this

我试图让 git 忽略我的一些文件,我找到了一个关于如何做到这一点的描述

From: http://github.com/guides/git-cheat-sheetTO IGNORE SOME FILES

Add a file in the root directory called .gitignore and add some files to it: (comments begin with hash) *.log db/schema.rb db/schema.sql

Git automatically ignores empty directories. If you want to have a log/ directory, but want to ignore all the files in it, add the following lines to the root .gitignore: (lines beginning with ‘!' are exceptions)

log/* !.gitignore

Then add an empty .gitignore in the empty directory:

touch log/.gitignore

来自:http: //github.com/guides/git-cheat-sheet 忽略一些文件

在根目录添加一个名为 .gitignore 的文件,并在其中添加一些文件:(注释以 hash 开头)*.log db/schema.rb db/schema.sql

Git 会自动忽略空目录。如果你想要一个 log/ 目录,但想要忽略其中的所有文件,请将以下几行添加到根 .gitignore 中:(以 '!' 开头的行是例外)

日志/* !.gitignore

然后在空目录中添加一个空的 .gitignore :

触摸日志/.gitignore

So I made a file called .gitignore in my folder for my project and wrote the following in it:

因此,我在我的项目文件夹中创建了一个名为 .gitignore 的文件,并在其中写入了以下内容:

phpMyAdmin/*
nbproject/*
inc/mysql_config.php
!.gitignore

But when I commit, the files isent excluded from the commit...

但是当我提交时,文件没有从提交中排除......

回答by Joe

https://www.gitignore.io/

https://www.gitignore.io/

I created a web utility that can help you generate useful .gitignore files for your project. It will help you ignore operating system, programming language, and IDE generated files.

我创建了一个 Web 实用程序,可以帮助您为项目生成有用的 .gitignore 文件。它将帮助您忽略操作系统、编程语言和 IDE 生成的文件。

回答by eruciform

According to man gitignore:

根据man gitignore

DESCRIPTION

A gitignorefile specifies intentionally untracked files that git should ignore. Note that all the gitignorefiles really concern only files that are not already tracked by git; in order to ignore uncommitted changes in already tracked files, please refer to the git update-index --assume-unchangeddocumentation.

描述

一个gitignore文件指定故意说的git应该忽略未跟踪文件。请注意,所有gitignore文件实际上只涉及 git 尚未跟踪的文件;为了忽略已跟踪文件中未提交的更改,请参阅git update-index --assume-unchanged文档。

So it doesn't help if you've already added them. It's mostly for preventing the addition in the first place. That way, you can ignore .tmpfiles and add a whole directory without worrying that you'll add the .tmpfiles.

因此,如果您已经添加了它们,则无济于事。首先主要是为了防止添加。这样,您可以忽略.tmp文件并添加整个目录,而不必担心会添加.tmp文件。

I believe you can remove them from the index with:

我相信您可以通过以下方式将它们从索引中删除:

git rm --cached file_to_stop_tracking_but_dont_want_to_delete.txt

Update:

更新:

Also, the .gitignoreneeds to be at the base directory or at least above where those directories are. Also, take the "*" out of the directories:

此外,.gitignore需要位于基本目录或至少在这些目录所在的上方。另外,从目录中取出“*”:

phpMyAdmin/
nbproject/
inc/mysql_config.php
!.gitignore

And be careful of phpMyAdmin/vs /phpMyAdminvs phpMyAdmin. Also from man gitignore:

并小心phpMyAdmin/vs /phpMyAdminvs phpMyAdmin。同样来自man gitignore

  • 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 fooand 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 without leading directories.

  • Otherwise, git treats the pattern as a shell glob suitable for consumption by fnmatch(3)with the FNM_PATHNAMEflag: wildcards in the pattern will not match a /in the pathname. For example, Documentation/*.htmlmatches Documentation/git.htmlbut not Documentation/ppc/ppc.html. A leading slash matches the beginning of the pathname; for example, /*.cmatches cat-file.cbut not mozilla-sha1/sha1.c.

  • 如果模式以斜杠结尾,则出于以下描述的目的将其删除,但它只会找到与目录的匹配项。换句话说,foo/将匹配目录foo和其下的路径,但不会匹配常规文件或符号链接foo(这与 pathspec 通常在 git 中的工作方式一致)。

  • 如果模式不包含 slash /,则 git 将其视为 shell glob 模式并检查与没有前导目录的路径名的匹配项。

  • 否则,git 将模式视为适合fnmatch(3)使用FNM_PATHNAME标志的 shell glob :模式中的通配符将与/路径名中的 a 不匹配。例如,Documentation/*.html匹配Documentation/git.html但不匹配Documentation/ppc/ppc.html。前导斜杠匹配路径名的开头;例如,/*.c匹配cat-file.c但不匹配mozilla-sha1/sha1.c