我需要包括一些文件在我的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拉。然后覆盖我的所有更改。


当前回答

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

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

其他回答

给出的答案只是部分地解决了问题,但却带来了更多的问题。

我需要保存“Web.Local”。配置文件”。 对我来说唯一有效的解决方法是:

1. 从项目中排除该文件

2. 创建该文件的单独副本Web.LocalTemplate.config

现在有了Web.LocalTemplate。config”是由Git跟踪的,所有开发人员都可以使用它作为起点。然而,“Web.Local。Config”,不属于项目的一部分将被自动忽略。

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

git update-index——skip-worktree FILENAME。

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

无法标记文件response.json

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

$ git update-index——skip-worktree FILENAME

似乎比"假设不变"更好

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

从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.

Git有一个不同的解决方案。首先更改您不想被跟踪的文件,并使用以下命令:

git update-index --assume-unchanged FILE_NAME

如果您想再次跟踪更改,请使用以下命令:

git update-index --no-assume-unchanged FILE_NAME

git-update-index文档