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.
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.
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.
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.
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.
I have also come across this behavior ... I've tracked it down to the following explanation, but perhaps someone clarify this a it more:
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With