【问题标题】:Is a new branch necessary before git cherry pick?在 git cherry pick 之前是否需要一个新分支?
【发布时间】:2015-12-11 00:20:15
【问题描述】:

我一直在关注这个示例here,它表明您在使用git cherry-pick 之前创建了一个临时分支,然后您选择了该临时分支。

在示例中,临时分支称为newbar,它从foo 分支出来。

临时分店有必要吗?或者你可以从foo 中挑选樱桃吗?该示例没有明确说明此临时分支的目的。

【问题讨论】:

  • 如果您使用foo 进行挑选,那么 foo 将指向 E'。使用新分支以便 foo 在说了这么多之后仍然指向 H。
  • 这个例子的重点是展示如何使用cherry-pick来模拟rebase。如果您只想挑选提交到一个分支,您可以查看该分支并使用git cherry-pick AA 是提交哈希...不需要临时分支。

标签: git github git-branch git-commit git-cherry-pick


【解决方案1】:

不,您不需要 拥有单独的分支。这是完全合法的:

$ git log --decorate --oneline --graph --all
* f1188b1 (HEAD -> master) commit3
* 90a233e commit2
* f167481 commit1

然后在仍然在单个且唯一的分支master

$ git cherry-pick 90a233e

这里的问题是很有可能发生冲突(以下是挑选后的示例输出):

error: could not apply 90a233e... commit2
hint: after resolving the conflicts, mark the corrected paths
hint: with 'git add <paths>' or 'git rm <paths>'
hint: and commit the result with 'git commit'

按照git-status简单处理和解决:

$ git status
On branch master
You are currently cherry-picking commit 90a233e.
  (fix conflicts and run "git cherry-pick --continue")
  (use "git cherry-pick --abort" to cancel the cherry-pick operation)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   file1.txt

no changes added to commit (use "git add" and/or "git commit -a")

基本上你会修复你的冲突,然后将更改添加到索引并运行$ git cherry-pick --continue 以完成樱桃挑选操作。

【讨论】:

    【解决方案2】:

    没有必要为采摘樱桃创建一个新的分支。示例之所以这样做,是因为是在模拟rebase 命令。

    This example 更清晰

    如果您在此图中的节点 H,并且您输入了 git cherry-pick E (是的,您实际上会为提交键入部分或全部 SHA,但是 为简单起见,我将只使用已经存在的标签 在这里),你会得到一份提交 E 的副本——让我们称之为“E prime” 或 E'——指向 H 作为其父级,如下所示:

    这里要注意的重要一点是 Git 复制了所做的更改 在一个地方,然后在其他地方重播。

    【讨论】:

      猜你喜欢
      • 2012-12-02
      • 2018-07-27
      • 1970-01-01
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      相关资源
      最近更新 更多