12

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.

2
  • 2
    Are you sure that specified commit didn't include more changes than you thought? Commented Oct 18, 2011 at 5:52
  • Yeah, I tried to see the diff of that commit: git diff revision^ revision. that is what I need. Commented Oct 18, 2011 at 6:59

2 Answers 2

9

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

Sign up to request clarification or add additional context in comments.

1 Comment

sorry for the post necro, but could you ever find a way to avoid that behavior and include only the changes in the specified commit? I am having the same issue and cant seam to find a solution
4

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.

2 Comments

Thanks for your answer. I checked the log. It is exactly what I want. But after I ran git cherry-pick, it added extra codes not in that commit. And FYI, the commit in branch1 was merged from other development branch, does this possibly cause this issue?
the code was in your branch. run git log -p -1 on the branch you cherry-picked to and see what got added. If the extra stuff is not there, it means it already existed in that branch. To check which commit added a piece of code, git log -Ssometextfromafeature will show what added that code.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.