git - 为什么 cherry-pick 之后的 rebase 不会两次应用相同的提交?

标签 git

当您从一个分支(比如“主题”)挑选一个提交到另一个分支(我们称之为“主”)时,该提交的历史记录被重写,它的哈希值发生变化,它实际上变成了一个新的、独立的提交。

然而,当您随后根据主 git 对主题进行 rebase 时,git 足够聪明,知道不要对提交应用两次。

例子:

A --- B <- master
      \
       \---- C ---- D <- topic

$ git checkout master
$ git cherrypick D

A --- B --- D' <- master
      \
       \---- C ---- D <- topic

$ git checkout topic
$ git rebase master
First, rewinding head to replay your work on top of it...
Applying 'C'

A --- B --- D' <- master
            \
             \---- C' <- topic

这个魔法是如何运作的? IE。 git 如何知道它应该将 C 应用到 D',而不是 D 应用到 D'?

最佳答案

答案在 git-rebase 的手册页中:

Note that any commits in HEAD which introduce the same textual changes as a commit in HEAD.. are omitted (i.e., a patch already accepted upstream with a different commit message or timestamp will be skipped).

Rebase 查看文本更改,如果该提交已经存在于您要进行 rebase 的分支上,则拒绝重放该提交。

关于git - 为什么 cherry-pick 之后的 rebase 不会两次应用相同的提交?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14509878/

相关文章:

bash - Homebrew 软件无法安装-致命: not in a git directory

git - 推送除特定分支之外的所有分支

git-ftp resmo git-ftp.log

git - 为什么不推荐使用 git flow 功能 checkout ?我应该改用什么?

macos - 使用 gcloud init 时遇到问题

git - 使用 Atlassian SourceTree,存储应用期间出错。不知道该怎么办

Git 远程分支列表

java - 如何将公共(public)存储库 merge 到我自己的存储库中,但继续接收来自公共(public)存储库的更新?

php - 如何使用 composer 安装 git 仓库的一部分?

git - 撤消 git 存储