2

假设您有一个版本号在两个分支中增加的文件,如何防止 Git 静默自动合并定义版本的两行更改行?

*   Git automatically merges here, but shouldn't
|\
| * change same line to the same new text
* | change some line here
 \|
  * prior history/root commit

在我们的例子中,我们有一个支持迁移的数据库模式。我们的主模式文件定义了当前的模式版本。如果两个人更改架构,例如在不同的表中添加一个数据库列,并且都将架构版本增加+1 , Git 会默默地合并所有内容,但结果会被破坏。

我建议在任何行上添加一个特殊标记,以使 Git 不会自动合并此特定行。有没有像这样的功能我不知道或如何实现?

这是一个 shell 命令列表,用于创建一个说明问题的示例 Git 历史记录:

$ git init test
Initialized empty Git repository in $PWD/test/.git/
$ cd test/
$ echo "version 1" > file
$ git add file
$ git commit -m "add file v1"
[master (root-commit) 4ef6950] add file v1
 1 file changed, 1 insertion(+)
 create mode 100644 file
$ git checkout -b a
Switched to a new branch 'a'
$ echo "version 2" > file
$ git commit -a -m "bump to v2"
[a 85dba39] bump to v2
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git checkout -b b master
Switched to a new branch 'b'
$ echo "version 2" > file
$ git commit -a -m "bump to v2 in b"
[b b0fcf46] bump to v2 in b
 1 file changed, 1 insertion(+), 1 deletion(-)
$ git merge a
Merge made by the 'recursive' strategy.          
$ git status                                # shouldn't be clean
On branch b
nothing to commit, working directory clean
4

2 回答 2

2

我不认为你所要求的是可能的。
相同的变化是相同的变化是相同的变化。

有两件事浮现在脑海中,您可以如何解决您的问题:

  1. 让开发人员在同一行上维护具有独特性的注释,例如当前时间到第二个,unix 时间,......当文件更改时,两个分支上不太可能相同的东西(不要使用用户特定的东西,一个用户可以同时进行更改并忘记它们)

  2. 使用预提交挂钩。在钩子中检查提交是否是合并提交,然后尝试确定是否存在您描述的情况,两个分支都更改了该行并在这种情况下中止合并。

于 2017-01-18T20:38:15.063 回答
-1

这听起来更像是一个进程问题,而不是 git 的问题。您应该尽一切可能避免在多次提交中进行完全相同的文本更改。这将有助于避免在后来的考古探险中混淆提交历史。您应该找到方法来更改您的数据库模式,而不会在您的版本历史记录中重复出现这种情况。

于 2017-01-18T20:45:33.973 回答