How to partially cherry-pick a commit in git

There are times when you need to cherry-pick a commit from another branch. Then there are times when you only need parts of that commit.

Following is how you can partly cherry-pick a commit to get only the changes that you need.

Get the patch for the entire commit. Include the -n for no commit so that it does not add it as a commit to your branch

git cherry-pick -n <commit>

Then unstage the the changes from the cherry-picked commit so that you can choose which lines/hunks that you want to include

git reset

From here you can either git add -p to specify specific files or use git gui or some other tool to selectively stage and commit lines or hunks of the current set of changes

Then just use git commit to commit your changes and reset the rest that you do not want and you are done.

Leave a Reply