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

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

.gitignore exclude specific file

gitgitignore

提问by user1995781

I am trying to use .gitignorefile to exclude templates.jslocated in public/app/folder. My .gitignorefile 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.jsfile 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. .gitignoreis only consulted when you git addfiles: 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 .gitignoresuch that the file is no longer added. Add the following line to the .gitignorefile:

首先,您最好修改该.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 --cachedflag 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 .gitignoreis not proactively used is because you sometimes might want to override the .gitignore. Say for instance you don't want to track *.logfiles, you can specify *.login the .gitignore. But if there is a specific one you want to track you can add git add -f some.log. The -fflag forces gitto 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 addon it and git commit), .gitignorewill not help.

如果您已经在跟踪文件(git add对它和git commit),.gitignore将无济于事。

I believe what you want is to stop tracking a file, and thenuse .gitignoreto 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.jswith

在这种情况下,你可以停止跟踪templates.js

git rm --cached public/app/templates.js

As others have explained, .gitignoreonly ignores content to be added. For example, if you frequently use git add .and have templates.jsin your .gitignore, then git will not add it.

正如其他人所解释的,.gitignore只忽略要添加的内容。例如,如果您经常使用git add .templates.js在您的.gitignore.git 中添加它,则 git 不会添加它。