Git cherry pick vs rebase

前端 未结 6 455
故里飘歌
故里飘歌 2020-12-12 10:12

I have recently started working with Git.

Going over the Git book online I found the following under the \"Git Rebase\" section:

With the reba

6条回答
  •  被撕碎了的回忆
    2020-12-12 10:23

    Both do very similar things; the main conceptual difference is (in simplified terms) that:

    • rebase moves commits from the current branch to another branch.

    • cherry-pick copies commits from another branch to the current branch.

    Using diagrams similar to @Kenny Ho's answer:

    Given this initial state:

    A---B---C---D master
         \
          E---F---G topic
    

    ...and assuming that you want get the commits from the topic branch replayed on top of the current master branch, you have two options:

    1. Using rebase: You'd first go to topic by doing git checkout topic, and then move the branch by running git rebase master, producing:

      A---B---C---D master
                   \
                    E'---F'---G' topic
      

      Result: your current branch topic was rebased (moved) onto master.
      The topic branch was updated, while the master branch remained in place.

    2. Using cherry-pick: you'd first go to master by doing git checkout master, and then copy the branch by running git cherry-pick topic~3..topic (or, equivalently, git cherry-pick B..G), producing:

      A---B---C---D---E'---F'---G' master
           \
            E---F---G topic
      

      Result: the commits from topic were copied into master.
      The master branch was updated, while the topic branch remained in place.


    Of course, here you had to explicitly tell cherry-pick to pick a sequence of commits, using the range notation foo..bar. If you had simply passed the branch name, as in git cherry-pick topic, it would have picked up only the commit at the tip of the branch, resulting in:

    A---B---C---D---G' master
         \
          E---F---G topic
    

提交回复
热议问题