让git忽略文件 [英] Make git ignore files
问题描述
我试图让git忽略我的一些文件,并且找到了一个关于如何执行此操作的描述。
From: http://github.com/guides/git-cheat-sheet
忽略一些文件
在根目录
中添加一个名为.gitignore的文件,并添加一些文件
:(注释以散列开头)
* .log db / schema.rb db / schema.sql
Git会自动忽略空的
目录。如果你想拥有一个
的日志/目录,但是想忽略所有的
文件,可以将下面的
行添加到根目录.gitignore :(行
开头'!'是例外)
log / *!.gitignore
然后在空格中添加一个空的.gitignore
空目录:
touch log / .gitignore
所以我在我的项目文件夹中创建了一个名为.gitignore的文件,并在其中写入以下内容:
phpMyAdmin / *
nbproject / *
inc / mysql_config.php
!.gitignore
但是,当我提交时,文件isent被排除在提交...
根据 man gitignore :
描述
gitignore
文件指定故意未记录的文件t帽子混帐应该忽略。注意
所有gitignore
文件确实只关注那些还没有被git跟踪的
的文件;为了忽略已跟踪文件中的未提交更改,
请参阅 git update-index --assume-unchanged 文档。
因此,如果你已经添加了它们,那就没有帮助。这主要是为了防止首先添加。这样,您可以忽略 .tmp
文件并添加一个完整的目录,而不用担心会添加 .tmp
我相信你可以通过以下方式将它们从索引中删除:
c> git rm --cached file_to_stop_tracking_but_dont_want_to_delete.txt更新:
另外, .gitignore
需要位于基本目录或者至少位于这些目录的上方。另外,从目录中取出*:
phpMyAdmin /
nbproject /
inc / mysql_config.php
!.gitignore
请注意 phpMyAdmin /
vs / phpMyAdmin
vs phpMyAdmin
。同样来自 gitignore :
如果模式以斜线结尾,则为了描述后面的
而将其删除,但它只会找到一个与目录匹配。
换句话说,foo /
会匹配一个目录foo
和
it下的路径,但不会匹配常规文件或符号链接foo
(这是
与pathspec在git中的一般工作方式一致)。
- http://www.opengroup.org/onlinepubs/000095399/functions/fnmatch.html =noreferrer>
如果该模式不包含斜线
/
,git会将其视为shell
glob模式并检查在没有前导目录的情况下匹配路径名。fnmatch(3)
与FNM_PATHNAME
标志:模式
中的通配符与路径名中的/
不匹配。例如,Documentation / *。html
匹配Documentation / git.html
但不是文档/ PPC / ppc.html
。
前导斜杠与路径名的开头相匹配;例如,/ *。c
匹配cat-file.c
但不是mozilla-sha1 / sha1.c
。
I'm trying to make git ignore some of my files and I found one description about how you could do this
From: http://github.com/guides/git-cheat-sheet TO 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
So I made a file called .gitignore in my folder for my project and wrote the following in it:
phpMyAdmin/*
nbproject/*
inc/mysql_config.php
!.gitignore
But when I commit, the files isent excluded from the commit...
According to man gitignore:
DESCRIPTION
A
gitignore
file specifies intentionally untracked files that git should ignore. Note that all thegitignore
files 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-unchanged documentation.
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 .tmp
files and add a whole directory without worrying that you'll add the .tmp
files.
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 .gitignore
needs to be at the base directory or at least above where those directories are. Also, take the "*" out of the directories:
phpMyAdmin/
nbproject/
inc/mysql_config.php
!.gitignore
And be careful of phpMyAdmin/
vs /phpMyAdmin
vs phpMyAdmin
. Also from 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 directoryfoo
and paths underneath it, but will not match a regular file or a symbolic linkfoo
(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 theFNM_PATHNAME
flag: wildcards in the pattern will not match a/
in the pathname. For example,Documentation/*.html
matchesDocumentation/git.html
but notDocumentation/ppc/ppc.html
. A leading slash matches the beginning of the pathname; for example,/*.c
matchescat-file.c
but notmozilla-sha1/sha1.c
.
这篇关于让git忽略文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!