gpt4 book ai didi

Git 压缩和 merge

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

谁能告诉我这两个命令有什么区别:

git merge --squash

git merge --no-ff 

最佳答案

我认为你的问题有点误会。 --no-ff--squash 不是对立的,而是略有不同的操作。阅读时请记住这一点。

git merge --squash

help page for merge以下是关于 --squash 的内容:

--squash and --no-squash

Produce the working tree and index state as if a real merge happened (except for the merge information), but do not actually make a commit or move the HEAD, nor record $GIT_DIR/MERGE_HEAD to cause the next git commit command to create a merge commit. This allows you to create a single commit on top of the current branch whose effect is the same as merging another branch (or more in case of an octopus).

With --no-squash perform the merge and commit the result. This option can be used to override --squash.

这有点令人困惑,并且需要一些关于 git 内部结构的知识。首先,我们需要了解常规提交和 merge 提交之间的区别。常规提交有一个父级,并且只是一个应用于它之前的提交的变更集:

A --> B --> C

merge 提交有多个父级,它是树中的一个位置,您将两个或多个沿袭放在一起:

A --> B --> F
/
C --> D - /

看看 ABCD 是常规提交,但是 F 是 merge 提交,因为它有多个父级(BD)?这就是 git merge --no-ff 会产生的结果。它强制 Git 创建一个 merge 提交以将两个历史放在一起。

git merge --squash 会做一些不同的事情。它会阻止 Git 创建 merge 提交,但仍会提取 CD 所做的更改,因此您的树看起来像这样:

A --> B --> F'

C --> D

F' 包含 CD 所做的更改,但没有迹象表明您在存储库中 merge 了两棵树。

git merge --no-ff

--no-ff 是一个略有不同的操作。它会强制 Git 创建一个 merge 提交,即使它并不是真正必要的。作为引用,以下是手册中关于 --no-ff--ff-only 的相反内容:

--no-ff

Create a merge commit even when the merge resolves as a fast-forward.

--ff-only

Refuse to merge and exit with a non-zero status unless the current HEAD is already up-to-date or the merge can be resolved as a fast-forward.

要理解,最好看一个例子:

A --> B --> C --> D --> E
| |
'master' 'topic'

如果您有这棵树,在主分支上,并运行 git merge,Git 将执行所谓的“快进” merge 。由于这两个历史之间没有分歧,Git 可以将 master 分支向上移动到 topic 所在的位置,而不做任何有趣的事情。它看起来像这样:

A --> B --> C --> D --> E
|
'topic'
'master'

topic 和 master 都指向同一个分支。现在,某些工作流程规则要求您在每次 merge 回 master 时创建 merge 提交。这样可以保留分支机构的历史记录。你会得到关于应该如何完成的任何一种争论,但我不会在这里讨论这些。

如果你在同一棵树上使用 git merge --no-ff,它会强制 git 创建一个 merge 提交,给你一个像这样的树:

                   'master'
|
A --> B -------------> F
\ /
C --> D --> E
|
'topic'

其中 F 是新的 merge 提交 --no-ff 强制 Git 创建。

关于Git 压缩和 merge ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23616706/

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