How to copy commits from one branch to another?

前端 未结 9 1404
猫巷女王i
猫巷女王i 2020-11-27 09:08

I\'ve got two branches from my master:

  • v2.1: (version 2) I\'ve been working on for several months
  • wss: that I create
9条回答
  •  有刺的猬
    2020-11-27 09:23

    git cherry-pick : Apply the changes introduced by some existing commits

    Assume we have branch A with (X, Y, Z) commits. We need to add these commits to branch B. We are going to use the cherry-pick operations.

    When we use cherry-pick, we should add commits on branch B in the same chronological order that the commits appear in Branch A.

    cherry-pick does support a range of commits, but if you have merge commits in that range, it gets really complicated

    git checkout B
    git cherry-pick SHA-COMMIT-X
    git cherry-pick SHA-COMMIT-Y
    git cherry-pick SHA-COMMIT-Z
    

    Example of workflow :

    We can use cherry-pick with options

    -e or --edit : With this option, git cherry-pick will let you edit the commit message prior to committing.

    -n or --no-commit : Usually the command automatically creates a sequence of commits. This flag applies the changes necessary to cherry-pick each named commit to your working tree and the index, without making any commit. In addition, when this option is used, your index does not have to match the HEAD commit. The cherry-pick is done against the beginning state of your index.

    Here an interesting article concerning cherry-pick.

提交回复
热议问题