将主机合并到每个主机定制分支时避免合并冲突 [英] Avoiding merge conflicts when merging master into per-host customized branch

查看:112
本文介绍了将主机合并到每个主机定制分支时避免合并冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有的dotfiles都存放在每个机器的分支
中的 git repository中。我遇到了一个问题,我无法解决这个问题,为此我使用 git 来阻止我从
中取出。我想知道其他人
如何解决它。



我将有一个 master 分支,其中只包含
模板的定义和定义,这些定义和定义对于所有分支应该是常见的
,例如:

  export EMAIL =@ EMAIL @
export SURFRAW_google_results = 1000

我将用机器分支
上正确的电子邮件替换 @EMAIL @ ,并将其提交但 SURFRAW_google_results 将保持不变。对于
的例子,在 work 分支上我会有这个:

  export EMAIL =user@corp.com
export SURFRAW_google_results = 1000

现在,我决定将 SURFRAW_google_results = 1000 更改为10.它应该是全局共享的
,所以我首先将它改为 master

  export EMAIL =@ EMAIL @
export SURFRAW_google_results = 10

然后在我将工作转换为<$ c

$ $ $ $ $ $ g $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ b

现在我发生冲突,因为行I
上面的行发生了变化:


 <<<<<<< a60114eea1c98d2354a07bd4fd0cdd63cba62e93 
导出EMAIL =@ EMAIL @
导出SURFRAW_google_results = 10
=======
导出EMAIL =user@corp.com
export SURFRAW_google_results = 1000
>>>>>>> m

如果 bash 我可以快速通过采购机制逃避
包括特定于机器的部分,但是
configs不支持包括/采购其他配置,例如 .fluxbox / keys .screenrc

解决方案

在分支之间(这会导致
(图片来自定制Git - Git Attributes ,来自 Pro Git book



这个想法是在版本中管理这些点文件,但使用模板占位符的实际值(取决于分支)。

生成的实际点文件仍然被忽略(由 .gitignore )。

这意味着您的实际工作树不会变脏。



具有实际值的文件也可以版本化,但名称不同合并/重新绑定)



涂抹脚本根据分支名称选择正确的值文件,并根据涂抹脚本应用于的点文件模板生成正确的点文件一个 git checkout



为了让每个分支有不同的表情,我建议调用一个脚本,看着th的名字e当前分支。

在我以前的答案中查看一个示例最佳实践 - Git + Build自动化 - 保持配置分离

 #!/ bin / sh 
branch = $(git rev-parse - 符号--abbrev-ref HEAD)

OP 提到,为了在结帐时应用涂抹脚本,需要删除索引。这是真的,因为我在这里解释过


索引是中间人,用于将事物从工作树移动到对象存储区,并将事物从对象存储区移至工作树。

blockquote>

通过删除索引,您可以强制git恢复它,这意味着应用任何内容过滤器驱动程序(如果存在)。

请参阅 git:创建涂抹过滤器后重新签出文件


I want to store all my dotfiles in git repository with separate branch for each machine. I have a problem I can't solve that blocks me from using git for that purpose. I'd like to know how other people solved it.

I'm going to have a single master branch which contains only template definitions and definitions which are supposed to be common for all branches, for example:

export EMAIL="@EMAIL@"
export SURFRAW_google_results=1000

I will replace @EMAIL@ with my correct e-mail on machine branches and commit it but SURFRAW_google_results will stay the same. For example, on work branch I'll have this:

export EMAIL="user@corp.com"
export SURFRAW_google_results=1000

Now, I decided to change SURFRAW_google_results=1000 to 10. It's supposed to be shared globally, so I first I change it on master:

export EMAIL="@EMAIL@"
export SURFRAW_google_results=10

and then on I rebase work onto master:

$ git checkout work
$ git rebase master

And now I get conflict because the line that is above the line I changed is different:

<<<<<<< a60114eea1c98d2354a07bd4fd0cdd63cba62e93
export EMAIL="@EMAIL@"
export SURFRAW_google_results=10
=======
export EMAIL="user@corp.com"
export SURFRAW_google_results=1000
>>>>>>> m

In case of bash I could quickly get away with including a machine-specific part by sourcing mechanism but how about configs that do not support including/sourcing other configs such as .fluxbox/keys or .screenrc?

解决方案

Instead of modifying concurrent data directly between branches (which results in conflict as illustrated in this answer), you can considering content filter driver, using .gitattributes declaration.

(image from "Customizing Git - Git Attributes", from "Pro Git book")

The idea is to manage those dotfiles in version, but with template placeholders in place of the actual values (which depends on the branch).
The generated actual dotfiles remains ignored (by the .gitignore).
That means your actual working tree does not get "dirty".

The files with the actual values can also be versioned, but with different names (so no conflicts when merging/rebasing)

The smudge script select the correct value file depending on the branch name, and generate the correct dotfile based on the dotfile template the smudge script is applied on during a git checkout.

To have a smudge acting differently per branch, I would recommend calling a script which starts by looking the name of the current branch.
See an example in my older answer "Best practice - Git + Build automation - Keeping configs separate".

#!/bin/sh
branch=$(git rev-parse --symbolic --abbrev-ref HEAD)

The OP mentions that, for the smudge script to apply on checkout, removing the index is needed. That is true, as I explained here:

the index is the middle-man for moving things from your work-tree to the object-store AND for moving things from the object-store to your work-tree.

By removing the index, you force git to restore it, which means applying any content filter driver if present.

See "git: re-checkout files after creating smudge filter".

这篇关于将主机合并到每个主机定制分支时避免合并冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆