简体   繁体   中英

Git cherry-pick vs merge branches

I understand that cherry-pick was invented for apply some intermediate commits but does exist any difference between:

git cherry-pick last_commit_from_branch

and

git merge some_branch

As i understand, git will apply not only this one commit but also all previous commits starting with common commit? If yes, the only difference in these lines will be that in case of merge the new commit will have two parent commits from these branches.

These both commands are different in a very substantial way.

Lets start with git cherry-pick :

  1. A cherry pick literaly takes the diff the commit introduced
  2. It literaly applies this diff on the HEAD you are currently on. This means it doesn't take any parent commits into account to accomplish a smooth and correct merge.
  3. A cherry pick does not create any connection to the branch you are picking from. There is a reference to the commit you took it from, but this reference is only in the commit message.

Now lets take a look for what a merge does.(For the sake of simplicity we look a common 3-Way merge)

         A
         |
1--2--3--4
   |
   5--6--7
         |
         B

Now lets say you want to merge both of them. What will git now do?

  1. First it will try to find the most common ancestor of A and B which is 2 in this case.
  2. It will now determine the changes brought in by 4 and 7 and match them agains 2 to get the difference for the merging. More in detail: If you have changes that are in 2 and in 7 it will take the changes from A as the merge changes and vice versa.

So a merge has the possibility to take more information into account while doing the actual merge compared to a simple cherry-pick.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM