I want to merge a change in a commit of development branch to another branch using git
. I tried git cherry-pick
but it results change in the whole file, not the specific change. Can anyone help with that?

- 2,268
- 1
- 15
- 27

- 1,006
- 14
- 14
-
Honestly the way I would do this would be to just compare your source file against the other commit in an IDE (such as IntelliJ), and then pull in whatever change you want. – Tim Biegeleisen Jun 08 '18 at 11:03
-
This question has been answered here: https://stackoverflow.com/questions/1085162/commit-only-part-of-a-file-in-git?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Tawanda Diza Jun 08 '18 at 11:09
-
Except @TawandaDiza 's proposed alternate question doesn't address how to take the change from another branch via merge. (And in fact that question may not be applicable at all, as the question doesn't specify whether the desired change is in a file that contains other changes.) I'm not sure why people have so much trouble understanding the meaning of EXACT DUPLICATE. – Mark Adelsberger Jun 08 '18 at 13:00
1 Answers
One thing to keep in mind is that the commit is the smallest unit considered in deciding what's "already merged". So there's a trade-off, in that if you don't create a merge relationship you may well get a spurious merge conflict down the line, but if you do create a merge relationship between the branches for just the one change, it would likely cause some future merge to fail to pick up the other changes.
In 99% of cases, not creating a merge relationship is the lesser evil here, and so I'd say you're on the right track with cherry-pick
.
The next step is to get only the change you want. The easiest way is to use the --no-commit
(or -n
) option:
git cherry-pick -n <commit-id>
Then the changes from the target commit will be staged, but not committed. You can remove the changes to other files with
git reset HEAD -- path/to/other/file
git checkout -- path/to/other/file
If the change you want is in the same file with other changes, you can unstage the file and then re-stage it in "interactive" mode with
git reset HEAD -- path/to/file
git add -i path/to/file
(See https://git-scm.com/docs/git-add for details about the "interactive add" interface. It has multiple ways to select what changes to include.
Once you've done your selective staging, you can commit, then clear any remaining unwanted changes from your working tree.

- 42,148
- 4
- 35
- 52