git - .gitignore 排除特定文件

标签 git gitignore

我正在尝试使用 .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/

相关文章:

git - 如何保持浅层 git 克隆最新(同时保持浅层)

git - 将 git 钩子(Hook)添加到谷歌云存储库

git - 如何获取整个 git 历史的文件夹大小

git - inotify:除了 .gitignore 中的文件外,还可以观察文件变化吗?

gitignore 不忽略特定的 bin 文件夹

git - 如何删除 .gitignore 中列出但仍在存储库中的文件?

git - Magit:我可以从之前的提交中撤销一个 hunk 吗?

Git 从分支中删除一个文件,将其保留在 master 中

Git ignore 不忽略指定的文件夹/文件

git - 撤消 git rm 。 -r --缓存或提交更改?