gpt4 book ai didi

Git cherry pick 和数据模型完整性

转载 作者:IT王子 更新时间:2023-10-29 01:28:07 25 4
gpt4 key购买 nike

鉴于两个分支已经 fork ,并且需要将一个分支(而不是所有分支)的特定提交引入另一个分支,git cherry pick 恰好实现了这一点。

一段时间后,需要完全 merge 两个分支。 git 将如何知道它已经拥有过去精心挑选的提交,这样它就不会重新引入它?

最佳答案

avoiding duplicate commit中提到的“tonio's answer”文章说:

Imagine we have the master branch and a branch b:

  o---X   <-- master
\
b1---b2---b3---b4 <-- b

Now we urgently need the commits b1 and b3 in master, but not the remaining commits in b. So what we do is checkout the master branch and cherry-pick commits b1 and b3:

$ git checkout master
$ git cherry-pick “b1’s SHA”
$ git cherry-pick “b3’s SHA”

The result would be:

  o---X---b1'---b3'   <-- master
\
b1---b2---b3---b4 <-- b

Let’s say we do another commit on master and we get:

  o---X---b1'---b3'---Y   <-- master
\
b1---b2---b3---b4 <-- b

If we would now merge branch b into master:

$ git merge b

We would get the following:

  o---X---b1'---b3'---Y--- M  <-- master
\ /
b1----b2----b3----b4 <-- b

That means the changes introduced by b1 and b3 would appear twice in the history. To avoid that we can rebase instead of merge:

$ git rebase master b

Which would yield:

  o---X---b1'---b3'---Y   <-- master
\
b2---b4 <-- b

Finally:

$ git checkout master
$ git merge b

gives us:

  o---X---b1'---b3'---Y---b2---b4   <-- master, b

(在 this thread 之后)


OP 在评论中添加:

But still it seems that I dont quite understand how rebase works.. I mean even after rebasing shouldn't the cherry picked commits still appear?

没有。 git commit man page明确提到:

If the upstream branch already contains a change you have made (e.g., because you mailed a patch which was applied upstream), then that commit will be skipped.
For example, running git rebase master on the following history (in which A' and A introduce the same set of changes, but have different committer information):

      A---B---C topic
/
D---E---A'---F master

will result in:

               B'---C' topic
/
D---E---A'---F master

您可以使用 git cherry master 检测 master 上是否已经存在提交(如果您在 topic 分支上)。

关于Git cherry pick 和数据模型完整性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2627953/

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