没有完全简单的方法可以做到这一点。
从根本上来说,文件是tracked在 Git 中当且仅当它在索引中。索引(通常,最初)是从某个提交填充的,因此是某个先前的提交决定是否要跟踪文件。假设存在提交集T and U类似,只是有些文件不在提交中U提交中的T. Then:
git checkout any-T-sub-i-commit
导致文件位于索引中(并因此被跟踪),同时:
git checkout any-U-sub-j-commit
导致文件不在索引中(因此未被跟踪)。
The same holds in a more general fashion for operations like merging: when you work with commits from set T, you work with the ones that have the files; when you work with commits from set U, you work with the ones that lack the files. If you merge any Ti commit with any Uj commit, the effect on any such file—whether it's added, removed, or conflicted—depends on whether the merge-base commit is in set T or set U, and the specific changes to those files within commit Ti with respect to the merge base commit.
当然,当文件移入或移出索引时,Git 也会同时将它们复制到工作树中或从工作树中删除(通常要注意不要删除未保存但珍贵的数据)。因此,这意味着工作树文件将消失并重新出现,具体取决于您是否签出T提交或U commit.
同时,让我们看看什么是捆绑,至少在抽象意义上是这样。捆绑包的本质在于它包含at least所有的数据git fetch
or git push
会通过电线发送,之后git fetch
or git push
用于最小化这些数据的通信过程。 (它可以包含额外的数据,这些数据将被忽略。)这个最小数据包含必须复制的所有对象(带注释的标签、提交、树和 blob)以及引用名称及其值。
要从捆绑包中排除某些文件集,您需要专门捆绑U承诺,而不是任何T承诺。就目前而言,这很好:如果您复制了所有分支,并区分T提交并U通过分支名称提交,您可以很容易地实现这一点。但结果是每次你制作一个新的Tcommit 你必须做出相应的U提交,反之亦然。事实上,你的工作量增加了一倍。
The standard recommendation that applies to configuration files in general applies here as well: Do not commit any configuration, ever. Commit only sample or default or template configurations. Use some kind of wrapper to convert these sample configurations to real configurations. (The wrapper can also be committed, of course, if it's something you write yourself, such as a shell script or Python program or whatever.) You may now maintain, and version-control, these sample / default configurations. Cloning the repository obtains the samples, and updating from the clone—git fetch
followed by a merge or rebase—updates the samples, but does not touch the actual configuration. Depending on how smart the wrapper is and what's available in your output format,1 it can even auto-detect that the sample/default input has changed, and warn or fail any runs that use the prescribed tool (i.e., the wrapper itself) until the real configuration is updated to match any required changes coming from the sample/default/template configuration.
这仍然不是微不足道的,特别是,您可能必须编写一个包装器,并教育用户运行特定系统的正确方法。但这是您可能实现的最微不足道的事情。
1In this particular case, your output is most likely the YAML files for ansible. This means you can hide all kinds of useful sample/default-config information in comments, for instance.