.gitignore排除特定文件 [英] .gitignore exclude specific file
问题描述
我试图使用 .gitignore
文件来排除位于中的
文件夹。我的 templates.js
public / app / .gitignore
文件如下所示:
I am trying to use .gitignore
file to exclude templates.js
located in public/app/
folder. My .gitignore
file looks like this:
/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js
但是,当我检查Git Gui时, templates.js
文件仍然显示在其中。如何使用 .gitignore
?
But when I check in the Git Gui, the templates.js
file is still show in it. How can I specifically exclude a file using .gitignore
?
推荐答案
与名称忽略可能暗示的内容形成鲜明对比。 .gitignore
仅在您 git add
文件时被查阅:换句话说,一个已添加到)存储库不会根据 .gitignore
。
In contrast to what the name "ignore" might suggest. .gitignore
is only consulted when you git add
files: in other words a file already added to the (index of the) repository will not be excluded based on the .gitignore
.
排除。首先,您最好修改 .gitignore
,以便不再添加文件。将以下行添加到 .gitignore
文件中:
First you better modify the .gitignore
such that the file is no longer added. Add the following line to the .gitignore
file:
public/app/template.js
接下来,您需要从存储库中排除该文件。可能您不想从文件系统中删除文件,这可以通过以下方式完成:
Next you need to exclude the file from the repository. Probably you don't want to remove the file from your file system, this can be done with:
git rm --cached public/app/template.js
--cached
标志确保文件不会从文件系统中删除。 (如果不重要,可以使用 git rm public / app / template.js
,但这会移除文件)。
The --cached
flag ensures the file will not be removed from your file system. (If not important, you can use git rm public/app/template.js
, but this will remove the file).
不主动使用原因 .gitignore
是因为你有时可能想重写 .gitignore
。比如说你不想跟踪 *。log
文件,你可以指定 * .log
code>的.gitignore 。但是如果有一个你想跟踪的特定的,你可以添加 git add -f some.log
。 -f
标记强制 git
添加文件。
The reason .gitignore
is not proactively used is because you sometimes might want to override the .gitignore
. Say for instance you don't want to track *.log
files, you can specify *.log
in the .gitignore
. But if there is a specific one you want to track you can add git add -f some.log
. The -f
flag forces git
to add the file.
这篇关于.gitignore排除特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!