(Git) Cherry-pick only parts of a commit

  1. Use -n flag to cherry-pick changes in a commit without committing them. Then reset the changes and re-stage only the desired parts before committing them. E.g.:
    git cherry-pick -n <COMMIT_HASH>
    git reset
    git add -p      # Stage patches of current changes
    git commit
    
  2. Use git checkout -p <COMMIT_HASH> which diffs the current commit against the specified commit, and allows to select patches of the diff to be staged.

References:

Written on November 16, 2021