将文件保存在远程,但通过 Git 克隆 repo 将忽略该特定文件

Keep file on remote, but cloning repo via Git will ignore that specific file

我有几个配置文件想保留 GitHub 但忽略进一步的更改。通过忽略进一步的变化,我的意思是 "If anyone clones the repo they will get the default version of those files and can edit those locally but when run git status those files will not appear."

所以我 did/try 到目前为止是:

  1. 我添加了这些文件并将初始 commit/push 设为 GitHub。
  2. 稍后将这些文件添加到 .gitignore 并再次提交到 GitHub。 但是当有人克隆那个 repo 时,这些文件仍然被跟踪。

于是我尝试了两种选择:

  1. git rm --cached .
    git add .
    git commit .....`  
    

但在这种情况下,后来的提交实际上删除了这些文件。

  1. 尝试 git update-index --assume-unchanged [filepath] 并且它有效,但我不希望有人克隆 repo 然后 运行 这个命令。没有这个命令有什么办法可以实现我的目标吗?

总结:

我认为这是不可能的,因为它违反了 git 的宪法。 不过有一些解决方法。

  1. 将默认设置管理为分布副本,存储在远程仓库中并在版本控制下,例如 config.php-dist(config.php.dist) 用于分发并将 config.php 添加到 . git忽略,稍后从远程仓库克隆后,将该文件重命名为 config.php 之类的名称并在本地调整设置。由于 config.php 添加到 .gitignore 因此本地设置副本永远不会出现 在 git 状态列表中。
  2. 或维护 .env 文件以管理不同环境的不同设置,根据环境从 config.php 文件中读取这些数据,如下所示:https://github.com/laravel/laravel.