git 中有一个唯一文件的不同分支

Different branch with one unique file in git

我有一个带有 git 服务器的项目。我们是两个开发人员,我们每个人都必须为某些 api 使用不同的令牌。我的主分支、开发分支和测试分支还有另一个 api 键。 所有 api 代币都存储在一个 config.json 文件中,这意味着 5 个不同 config.json.

问题是:当将一个分支提交或合并到另一个分支时,我希望 config.json 不变。

我已经尝试过使用 .gitignore 文件和 git update-index --assume-unchanged config.json 的一些解决方案,但其中 none 似乎适用于我的情况。 我还找到了 .git/info/except 文件,它似乎确实是我需要的,但无论我尝试什么,config.json 都会出现 "modified".

因此,如果您有其他可能的解决方案,或者我的 exet 不起作用的原因,那将非常有帮助!

问候,Eiji


"Lack of better" 解法:

我用过@gus3001的方案。 我制作了一个文件夹配置,其中包含所有不同的配置(开发/主/测试/等),另一个 preset.json,在 .gitignore.

中引用

因此在每个分支开关上,我们只需要修改忽略文件中的一个短字符串即可。 它仍然会令人困惑和冒险,所以如果您有更好的解决方案,请分享。

我通常如何解决这个问题是将依赖于用户的配置放在一个单独的文件中。因此,在您的情况下,我会从 config.json 中取出 api-token 并将其放入 git-ignored 文件中,也许称为 user-config.json

这可能意味着您必须更改代码才能从新 user-config.json 获取令牌,但好处是您实际上可以在 [=10= 中跟踪非用户相关的配置] 通常情况下,不用担心它会被 git 忽略,仅当令牌不同但其他内容发生变化时不会。

tl;dr

用户相关的配置应该在一个单独的 git-忽略文件中以避免此类问题。

我想你要找的是 skip-worktree flag:

The skip-worktree bit can be defined in one (long) sentence: when reading an entry, if it is marked as skip-worktree, then Git pretends its working directory version is up to date and read the index version instead.

您可以在 config.json 文件上设置 skip-worktree 标志,方法是:

git update-index --skip-worktree config.json

跳过工作树与假设不变

skip-worktree 标志与 assume-unchanged 的不同之处在于 assume-unchanged 意味着 Git 不会检查 给定的文件在工作目录中进行修改,因为它假定文件 没有更改 :

You can set "assume unchanged" bit to paths you have not changed to cause Git not to do this check. Note that setting this bit on a path does not mean Git will check the contents of the file to see if it has changed — it makes Git to omit any checking and assume it has not changed.

文档进一步 clarifies this point:

When the "assume unchanged" bit is on, the user promises not to change the file and allows Git to assume that the working tree file matches what is recorded in the index.

assume-unchanged 标志确实是一个性能优化 如果您碰巧在一个比较文件异常慢的文件系统上1 或者——更有可能——如果你正在处理 不经常更改的文件。

另一方面,skip-worktree标志告诉Git完全忽略工作目录中的文件副本,只依赖关于索引中记录的内容。由于这个原因 skip-worktreeassume-unchanged 具有 更高的优先级 如果两者都在给定文件上设置。此外,值得注意的是,每当上游修改文件时,assume-unchanged 就会被取消设置,而 skip-worktree 会一直保持到您手动取消设置为止。


  1. 老实说,我想不出任何符合该描述的现代文件系统。