Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why cherry-pick pick change more than one commit?

I have 2 branches here, say branch1 and branch2. There are lots of new feature added in branch1, and the branch2 is stable. Today, I want to merge just 1 feature from branch1 to branch2. So, I just run git cherry-pick <commit-for-feature1-in-branch1. I suppose there should be only the change in <commit-for-featur1-in-branch1 will be merged into branch2. But I found there are more changes for other features are included.

I thought it will get the diff just for only that specified commit, right?

FYI, the commit in branch1 was merged from other development branch, does this possibly cause this issue?

Anything wrong I did?

Thanks.

like image 701
Rocky Avatar asked Oct 18 '11 03:10

Rocky


People also ask

Do cherry pick multiple commits?

It is possible to cherry pick multiple commits using the command line. Git 1.7. 2 introduced the ability to cherry pick a range of commits.

Does git cherry pick change commit?

When you are performing a regular cherry-pick, you will get a new commit hash but the commit message will be the same. However, there is a way to append the origin of a cherry-pick to the commit message : by using the cherry-pick with the “-x” option.

Does cherry pick Create a new commit hash?

There's no need to redo the same changes in a different branch when you can just copy the same commits to the other branch. Please note that cherry-picking commits will create a fresh commit with a new hash in the other branch, so please don't be confused if you see a different commit hash.

How does cherry pick commit work?

Cherry picking in Git means to choose a commit from one branch and apply it onto another. This is in contrast with other ways such as merge and rebase which normally apply many commits onto another branch. Make sure you are on the branch you want to apply the commit to.


2 Answers

I have also come across this behavior ... I've tracked it down to the following explanation, but perhaps someone clarify this a it more:

  • You cherry pick a commit, the commit contains 1 change in 1 file
  • You notice that not only the change contained in the commit but also more changes (mostly around that change are included)

This is because the change in the commit depends on a previous change. So this area of code was changed multiple time after the target branch you want to cherry pick was created.

Git goes back in the history until the cherry pick source matches the target and creates the patch based on this revision. That's why more changes might appear ...

I find this behavior a bit scary, because one would expect that only the changes from the given commit hash are picked

like image 171
Wirtsi Avatar answered Oct 17 '22 03:10

Wirtsi


What git cherry-pick does is it takes the commit that you specify and reads the difference between it and it's parent. This effectively makes a patch. It then applies this patch to your currently checked out branch.

In your case, the commit contained the addition of other features. You can double check that the commit message corresponds to what you thought the feature was by looking at the patch that this commit would generate with git log:

git log -p -1 <sha1-of-your-commit>

The -p tells log to not only show the commit information like author, date and commit message, but to also include the patch (or difference) that the commit introduces. The -1 option tells git log to stop listing history after 1 commit.

like image 22
Adam Dymitruk Avatar answered Oct 17 '22 04:10

Adam Dymitruk