我需要包括一些文件在我的GitHub回购,但不跟踪他们的变化。我怎样才能做到呢?一个示例用例是包含敏感用户信息(比如登录凭证)的文件。

例如,我将这个框架的新安装部署到一个新的客户端,我想下载以下文件(它们有默认值CHANGEME),我只需要对这个客户端进行特定的更改(数据库凭据,电子邮件地址信息,自定义CSS)。

// The production config files. I want the files but they need to be updated to specific client needs
application/config/production/config.php
application/config/production/database.php
application/config/production/tank_auth.php
// Index page, defines the environment (production|development)
/index.php
// All of the css/js cache (keep the folder but not the contents)
/assets/cache/*
// Production user based styling (color, fonts etc) needs to be updated specific to client needs
/assets/frontend/css/user/frontend-user.css

目前如果我运行

git clone git@github.com:user123/myRepo.git httpdocs

然后编辑上面的文件,一切都很好。直到我发布一个热修复或补丁,并运行git拉。然后覆盖我的所有更改。


当前回答

对于我的Code Igniter项目,我在repo中保留了database.php.example和config.php.example。

然后我将config.php和applications/config/database.php添加到.gitignore文件中。

因此,最后,当我部署时,我可以复制.example文件(到config.php和database.php),自定义它们,它们不会被GIT跟踪。

其他回答

从git 2.24.1版本开始,推荐的方法是跟踪存储库中文件的模板,将它们复制到新文件中并在本地修改它们(忽略.gitignore中的新文件名):

Users often try to use the assume-unchanged and skip-worktree bits to tell Git to ignore changes to files that are tracked. This does not work as expected, since Git may still check working tree files against the index when performing certain operations. In general, Git does not provide a way to ignore changes to tracked files, so alternate solutions are recommended. For example, if the file you want to change is some sort of config file, the repository can include a sample config file that can then be copied into the ignored name and modified. The repository can even include a script to treat the sample file as a template, modifying and copying it automatically.

我会把它们放在你的.gitignore文件中,并根据需要手动复制它们。

因此,骨架文件名将在git中,忽略文件名将不在git中(但将在.gitignore中)。这样,当手动步骤将它们从“模板”复制到“实际”时(可能比骨架更好的名称),它们会立即被忽略。

从另一个答案的注释中抽出一个答案:

$ git update-index——skip-worktree FILENAME

似乎比"假设不变"更好

更多信息可以在这里阅读:Git - 'assume-unchanged'和'skip-worktree'之间的区别

确保添加“文件名的完整路径”(相对根路径)

git update-index——skip-worktree FILENAME。

如果您没有给出文件的完整路径。它会给出一个错误

无法标记文件response.json

对于我的Code Igniter项目,我在repo中保留了database.php.example和config.php.example。

然后我将config.php和applications/config/database.php添加到.gitignore文件中。

因此,最后,当我部署时,我可以复制.example文件(到config.php和database.php),自定义它们,它们不会被GIT跟踪。