我正在尝试使用 .gitignore
文件来排除位于 public/app/
文件夹中的 templates.js
。我的 .gitignore
文件如下所示:
/vendor
/node_modules
composer.phar
composer.lock
.DS_Store
templates.js
但是当我检查 Git Gui 时,templates.js
文件仍然显示在其中。如何使用 .gitignore
专门排除文件?
最佳答案
与名称“忽略”所暗示的相反。 .gitignore
仅在您 git add
files 时被查询:换句话说,不会根据 排除已经添加到存储库(的索引)的文件.gitignore
.
首先,您最好修改 .gitignore
以便不再添加该文件。将以下行添加到 .gitignore
文件中:
public/app/template.js
接下来您需要从存储库中排除该文件。可能您不想从您的文件系统中删除该文件,这可以通过以下方式完成:
git rm --cached public/app/template.js
--cached
标志确保文件不会从您的文件系统中删除。 (如果不重要,可以使用 git rm public/app/template.js
,但这会删除文件)。
背景
未主动使用 .gitignore
的原因是您有时可能想要覆盖 .gitignore
。比如说你不想跟踪 *.log
文件,你可以在 .gitignore
中指定 *.log
。但是如果有一个特定的你想跟踪你可以添加 git add -f some.log
。 -f
标志强制 git
添加文件。
关于git - .gitignore 排除特定文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30227858/