git .gitignore 排除特定文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30227858/
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
.gitignore exclude specific file
提问by user1995781
I am trying to use .gitignore
file to exclude templates.js
located in public/app/
folder. My .gitignore
file looks like this:
我正在尝试使用.gitignore
文件来排除templates.js
位于public/app/
文件夹中。我的.gitignore
文件看起来像这样:
/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js
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
?
但是当我签入 Git Gui 时,该templates.js
文件仍然显示在其中。如何使用.gitignore
?
回答by Willem Van Onsem
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
仅在您git add
提交文件时才进行咨询:换句话说,不会基于.gitignore
.
First you better modify the .gitignore
such that the file is no longer added. Add the following line to the .gitignore
file:
首先,您最好修改该.gitignore
文件,以便不再添加该文件。将以下行添加到.gitignore
文件中:
public/app/template.js
Next you need to exclude the file from the repository. Probably you don't want to remove the filefrom your file system, this can be done with:
接下来,您需要从存储库中排除该文件。可能您不想从文件系统中删除该文件,这可以通过以下方式完成:
git rm --cached 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).
该--cached
标志确保文件不会从您的文件系统中删除。(如果不重要,您可以使用git rm public/app/template.js
,但这将删除文件)。
Background
背景
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
没有主动使用的原因是因为您有时可能想要覆盖.gitignore
. 比如说你不想跟踪*.log
文件,你可以*.log
在.gitignore
. 但是,如果您想跟踪某个特定的对象,则可以添加git add -f some.log
. 该-f
标志强制git
添加文件。
回答by BTownTKD
Once a file has been 'added' to the git repository once, it will continue to be tracked regardless of whether or not it is listed in the .gitignore file.
一旦文件被“添加”到 git 存储库一次,它将继续被跟踪,无论它是否在 .gitignore 文件中列出。
I'm guessing you added the template.js file previously, in which case it will continue to have changes tracked, until you remove it from the repository. Once the file has been removed from the repository, it will be properly ignored via the .gitignore file.
我猜您之前添加了 template.js 文件,在这种情况下,它将继续跟踪更改,直到您将其从存储库中删除。从存储库中删除文件后,它将通过 .gitignore 文件正确忽略。
The way to remove a file from a git repository (command-line) without deleting it is
从 git 存储库(命令行)中删除文件而不删除它的方法是
git rm --cached FILENAME
Then once you do that and try to commit with the file in your gitignore, it won't do it.
然后一旦你这样做并尝试在你的 gitignore 中提交文件,它就不会这样做。
Note that if you specifically add a file that you have in your gitignore, your manual adding of the file will override the gitignore.
请注意,如果您专门添加了 gitignore 中的文件,则手动添加该文件将覆盖 gitignore。
回答by Ross
If you are already tracking a file (git add
on it and git commit
), .gitignore
will not help.
如果您已经在跟踪文件(git add
对它和git commit
),.gitignore
将无济于事。
I believe what you want is to stop tracking a file, and thenuse .gitignore
to exclude it. To stop tracking a file but keep the file, use:
我相信你想要的是停止跟踪一个文件,然后用.gitignore
它来排除它。要停止跟踪文件但保留文件,请使用:
git rm --cached <file>
git rm --cached <file>
回答by Thomas Hughes
In this case you can stop tracking templates.js
with
在这种情况下,你可以停止跟踪templates.js
与
git rm --cached public/app/templates.js
As others have explained, .gitignore
only ignores content to be added. For example, if you frequently use git add .
and have templates.js
in your .gitignore
, then git will not add it.
正如其他人所解释的,.gitignore
只忽略要添加的内容。例如,如果您经常使用git add .
并templates.js
在您的.gitignore
.git 中添加它,则 git 不会添加它。