git cherry-pick不只是选择提交的差异

tam*_*lok 11 git cherry-pick

我有两个分支:AB.

  • A的承诺历史:a <- b <- c;
  • B的承诺历史:a <- h <- i;

假设这里只有一个文件.

  1. 在提交中b,我添加了一些像"foo"这样的文本.
  2. 在提交中c,我添加了一些像"bar"这样的文本.
  3. 然后我git cherry-pick cB分支上.我以为cherry-pick只会选择c分支的变化B.但是,它会添加foobar分支B.这显然不是我想要的.

因此,cherry-pick将从c祖先提交后选择提交中触及的那些文件的所有更改a.是对的吗?如果我只想要什么,从挑DIFF bc并应用到i

更新确切的步骤

  1. 创建一个git repo;
  2. 添加文件test.txt并发出第一个提交init commit.test.txt就是现在:

    first line  
    second line
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建一个新分支dev但留在分支机构master;

  4. 添加added in commit b到文件并发出提交b.test.txt就是现在:

    first line
    added in commit b
    second line
    
    Run Code Online (Sandbox Code Playgroud)
  5. 添加added in commit c到文件并发出提交c.test.txt就是现在:

    first line
    added in commit b
    added in commit c
    second line
    
    Run Code Online (Sandbox Code Playgroud)
  6. 签出dev分支并发出提交h.test.txt就是现在:

    first line
    second line
    
    adding by commit h
    
    Run Code Online (Sandbox Code Playgroud)
  7. git cherry-pick <commit c SHA1 ID>樱桃挑选承诺c提交h.

  8. 冲突消息:

    index 6a8dc57,594c6ec..0000000
    @@@ -1,4 -1,4 +1,9 @@@
      first line
    ++<<<<<<< HEAD
    ++=======
    + added in commit b
    + added in commit c
    ++>>>>>>> 06ce9b1... commit c adding another line
      second line
     +
     +adding by commit h
    
    Run Code Online (Sandbox Code Playgroud)
  9. 看到?cherry-pick也会带来改变的提交b.

谢谢!

ara*_*aer 5

git cherry-pick尝试只提交一次提交。但这是通过应用需要一些上下文的补丁来实现的。提交C中所做的更改与提交b中所做的更改非常接近,因此您会遇到冲突-它不能仅仅找到必须应用更改的正确位置。并且当您有冲突时,您还会得到一些冲突的上下文,这至少是提交B的一部分。

这是没有冲突的工作方式:

$ git init
$ cat > f
line1
line2
line3
$ git add f
$ git commit -a -m "initial"
# Edited to add a line in the beginning of f
$ cat f
Commit b
line1
line2
line3
$ git commit f -m "B"
# Edited to add a line in the end of f
$ cat f
Commit b
line1
line2
line3
Commit c
$ git commit f -m "C"
$ git checkout HEAD^^
$ git cherry-pick master
$ cat f
line1
line2
line3
Commit c
Run Code Online (Sandbox Code Playgroud)