gpt4 book ai didi

git - Git 中如何产生交叉 merge ?

转载 作者:太空狗 更新时间:2023-10-29 13:07:04 24 4
gpt4 key购买 nike

我一直在浏览“git merge-base”手册页,但我无法理解多个 merge 基础是如何发展的。具体来说,我对手册页中的以下插图很感兴趣:

When the history involves criss-cross merges, there can be more than one best common
ancestor for two commits. For example, with this topology:
---1---o---A
\ /
X
/ \
---2---o---o---B
both 1 and 2 are merge-bases of A and B. Neither one is better than the other (both
are best merge bases). When the --all option is not given, it is unspecified which
best one is output.

我就是不明白怎么会出现这种情况。我试图使用测试存储库在分支之间重新创建这种交叉 merge 情况,但我无法复制它。在所有情况下,我总是以 A 和 B 都指向的 1 个 merge 提交结束(而不是 A 和 B 指向独立的 merge 提交,如图所示)。

谁能说明这种情况是如何发生的?这是常见情况还是错误情况?

最佳答案

I just can't understand how such a situation could be created. I have attempted to recreate this criss-cross merge situation between branches using a test repository but I cannot replicate it. In all cases, I always wind up with 1 merge commit to which both A and B point to (instead of A and B pointing to independent merge commits as the diagram illustrates).

Can anyone illustrate how this situation can arise?

交叉 merge 可以以不同的方式出现。举个例子,当你有两个分支引用指向同一个 merge 提交(其中一个分支被 checkout )并且你运行 git commit --amend 时。以下玩具示例就是这样做的:

# set things up
cd ~/Desktop
mkdir crisscross
cd crisscross
git init

# make an initial commit
printf "bar\n" > README.md
git add README.md
git commit -m "add README"

# add a line at the top of the file and commit
sed -i '' '1s/^/foo\n/' README.md
git commit -am "add foo line"

# create and checkout a branch pointing at the initial commit
git checkout -b other master^

# add a line at the bottom of the file and commit
printf "baz\n" >> README.md
git commit -am "add baz line"

# do a merge and make both branches point to this merge commit
git merge master
git checkout master
git merge other

在这个阶段,git log --graph --oneline --decorate --all的输出是

*   30b9175 (HEAD, master, other) Merge branch 'master' into other
|\
| * f318ead add foo line
* | 31875d9 add baz line
|/
* 8b313a0 add README

现在修改最后一次提交(更多细节参见How does git commit --amend work, exactly?):

git commit --amend

之后,git log --graph --oneline --decorate --all 的输出将是

*   69bbcbe (HEAD, master) Amended merge commit
|\
| | * 30b9175 (other) Merge branch 'master' into other
| | |\
| |/ /
|/| /
| |/
| * f318ead add foo line
* | 31875d9 add baz line
|/
* 8b313a0 add README

好了:历史现在包含交叉 merge 。

Is this a common situation or an error situation?

如上所示,可能会出现这种情况。这可能是不可取的,但不应将其视为“错误状态”。

关于git - Git 中如何产生交叉 merge ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26370185/

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