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

塔姆洛克

我有两个分支:AB

  • A的提交历史:a <- b <- c;
  • B的提交历史:a <- h <- i;

假设这里只有一个文件。

  1. 在commit中b,我添加了一些文本,例如“ foo”。
  2. 在commit中c,我添加了一些文本,例如“ bar”。
  3. 然后我git cherry-pick cB树枝上。我以为cherry-pick只会选择c分支中的更改B但是,它将同时添加foobar到branch B这显然不是我想要的。

因此,cherry-pick将选择c自祖先commit以来提交中涉及的那些文件的所有更改a那正确吗?如果我只想从挑差异bc并把它应用到i

更新确切步骤

  1. 初始化一个git repo;
  2. 添加文件test.txt并发出第一次提交init committest.txt就是现在:

    first line  
    second line
    
  3. 创建一个新分支dev但留在分支master;

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

    first line
    added in commit b
    second line
    
  5. 添加added in commit c到文件中并发出commit ctest.txt就是现在:

    first line
    added in commit b
    added in commit c
    second line
    
  6. 签出dev分支并发出提交htest.txt就是现在:

    first line
    second line
    
    adding by commit h
    
  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
    
  9. 看到?cherry-pick也带来了提交的变化b

谢谢!

阿拉加尔

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章