为 Heroku 和 GitHub 分开 gitignore 文件?

Separate gitignore files for Heroku and GitHub?

我有一个文件是我的 heroku 应用程序所必需的,但我不希望它出现在我的 GitHub 存储库中。

有没有办法让 .gitignore 只在推送到 GitHub 时使用?

此外,有没有其他方法可以避免将那个文件推送到 GitHub?

如果您已经跟踪了这个文件,在推送到 GitHub 之前删除它(甚至在 .gitignore 中忽略它)将 不会 删除它来自该推送存储库的历史记录。

这意味着了解 为什么 您不想在 GitHub 上使用该文件很重要。

如果出于安全考虑,因为它包含敏感信息,您可能会:

  • more work to do
  • 将该文件外部化到任何本地存储库之外,以确保它不会被包含在内。

更一般地说,不,没有 conditional/separate .gitignore 用于两个不同的远程存储库。

在您的情况下,您可能必须编写 git 推送操作脚本才能玩 as in here,才能玩 git update-index --(no-)assume-unchanged <file>

VonC 的回答很好,但这里有更多信息。

Is there any way to have a .gitignore that is only used when pushing to GitHub?

.gitignore 永远不会与 git push 一起发挥作用:

  • git push 提交 进行操作,因此如果您希望将文件包含在您的 Heroku 构建中,则必须提交该文件
  • Git的忽略系统只会阻止文件被跟踪;它对跟踪文件没有影响

理论上您可以维护一个 long-running heroku 分支,该分支与 master (或您的主分支是什么)保持同步,但也包含您的 Heroku-only 文件,但在我看来,没有理由跳过这些障碍。只需包含您的 Procfile 或主存储库中的任何内容。它有什么危害?

请注意,如果您有 相反 的情况,您想要在 Heroku 上省略一些跟踪文件 ,您可以使用 .slugignore file:

If your repository contains files not necessary to run your app, you may wish to add these to a .slugignore file in the root of your repository. Examples of files you may wish to exclude from the slug:

  • Art sources (like .psd files)
  • Design documents (like .pdf files)
  • Test data

...

The .slugignore file causes files to be removed after you push code to Heroku and before the buildpack runs.