Git Cherry pick is showing already committed code as part of PR

287 views Asked by At

I am using GitHub. Lets consider we have a two branches dev and qa. At this point both my dev and qa branch are in sync (dev and qa has no difference). I have modified/modified some code in feature_dev (created from dev branch). I have created Pull Request from feature_dev to dev and merged it, the commit id generated for that PR merge is e105xfb. At this point the only difference between dev and qa branch is the new commit e105xfb.

Now I checkout out to qa branch and ran git pull command. Now I have used the below command

git cherry-pick -m 1 -x e105xfb 

This will cherry pick the commit e105xfb. Now I have pushed it to qa. At this point both the qa and dev are in sync again.

Now If I raise PR from dev to qa , My expectation is that it should display the message "There is nothing to compare", but it is showing the files modified/added during the commit e105xfb as files changed.

Can someone explain this behaviour and How to resolve it?

1

There are 1 answers

4
dani-vta On

The answer to your question lies in how merges and cherry-picks work in Git.

The cherry-pick command introduces the changes of a given commit, meaning that it re-applies the same set of modifications included in the commit. It doesn't "copy" the commit, but it rather creates a new one with the same modifications.

As the official documentation states:

https://git-scm.com/docs/git-cherry-pick

apply the change each one introduces, recording a new commit for each.

Instead, when merging two branches, Git looks for their closest common ancestor and marks it as the merge basis. This particular commit (the merge basis) is used as the starting point of the merge, and on top of it, Git applies every subsequent commit defined on the merging branches.

When you attempt to merge dev into qa, Git determines their closest common ancestor and applies every difference introduced after it. In your case, the set of differences corresponds to: the merge commit on the dev branch and the cherry-picked commit on the qa branch. This is why your pull request doesn't display the message "There is nothing to compare", because both branches did introduce some changes that, even though look the same, are identified by two different commits (with different SHA1s) that need to be merged.

This behavior also explains why your pull request's preview looks as if qa hasn't registered the cherry-picked commit. The pull request is actually showing you the set of changes detected from the merge basis; and since both branches introduced a change that looks exactly the same, when they're displayed they appear as a single change.