问题
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.
回答1:
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:
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
来源:https://stackoverflow.com/questions/7802252/why-cherry-pick-pick-change-more-than-one-commit