首页 > 解决方案 > Can you cherry-pick a hunk or hunks from within a git commit?

问题描述

When I do stage commits in my workspace I usually use the -p option to go through each hunk of the changes. I find this useful to confirm my changes and as a check on typos and other silly errors.

I usually rebase in my open source development work when a dev branch gets stale. However, sometimes I want to test out something on a throw away branch in my workspace and I don't want the entire branch because it is long and contains many commits that are not yet merged. I could git cherry-pick but that appears to grab the entire commit. Sometimes these commits are large and I don't want the entire thing.

Is there any way to grab the commit via cherry-pick but inspect the commit hunk by hunk as it is applied to a staging area before committing?

Presumably the user then stages only the components of the commit that they want.

标签: gitgit-rebasecherry-pickgit-cherry-pick

解决方案


不确定这是最简单的方法,但原子上它可能看起来像

# first you cherry-pick without committing
git cherry-pick -n <commit-hash>

# then you unstage everything
git reset

# and stage hunk by hunk as usual
git add -p

# finally you discard everything else
git stash --keep-index

推荐阅读