gpt4 book ai didi

git - 在两个不同的分支中有相同文件的两个不同副本

转载 作者:太空狗 更新时间:2023-10-29 14:17:49 26 4
gpt4 key购买 nike

你知道我怎么能在 git 的两个不同分支中拥有同一个文件的两个不同副本吗?

假设我有一个名为 config 的文件。
我想要实现的是在 master 分支中的 dev 分支中拥有此文件的副本。

重要的是文件不能在 gitignored 中被忽略。

最佳答案

您可以 checkout 2 个分支机构。修改文件并提交到每个分支。

如果您想 merge 分支并在每个分支中有 2 个单独的文件(不同的内容),请将更改提交到分支,然后在您的 .gitattributed 中设置所需的文件以始终抓取我们的 文件 - 这将导致此文件永远不会被覆盖

阅读所有相关信息并在此处进行设置:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes#Merge-Strategies

You can also use Git attributes to tell Git to use different merge strategies for specific files in your project.

One very useful option is to tell Git to not try to merge specific files when they have conflicts, but rather to use your side of the merge over someone else’s.

This is helpful if a branch in your project has diverged or is specialized, but you want to be able to merge changes back in from it, and you want to ignore certain files.

config_file merge=ours

作为@torec在他的评论中提到:

Beware of what I consider a bug in git: if git is able to trivially merge the file it will not run your custom merge driver. Git always tries a simple 2-way merge first, before attempting the 3-way merge

所以另一种选择是使用 smudge/clear 过滤器来确保您拥有正确的文件。

涂抹

阅读所有相关信息并在此处进行设置:
https://git-scm.com/book/en/v2/Customizing-Git-Git-Attributes

It turns out that you can write your own filters for doing substitutions in files on commit/checkout.

These are called clean and smudge filters.

In the .gitattributes file, you can set a filter for particular paths and then set up scripts that will process files just before they’re checked out (“smudge”, see Figure 8-2) and just before they’re staged (“clean”, see Figure 8-3).

These filters can be set to do all sorts of fun things.

enter image description here

关于git - 在两个不同的分支中有相同文件的两个不同副本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36778678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com