Can I interactively pick hunks from another git commit?

Using cherry-pick -n is not really satisfactory, since it leaves me with the dirty job of removing all the unrequired code. I'd really just like to pick the selected changes I want to test.

The job may have been a dirty one before, but with the advent of git checkout --patch, you can now selectively discard changes, similar to git add -p for adding.


You could use git reset --mixed HEAD^1 to revert the index, then pick the hunks you want with git add -i.

The reset will roll back the index to the previous commit (essentially un-committing whatever was the HEAD), but it won't touch the working tree. You can now stage the hunks you want, commit them and throw away the rest with a git reset --hard HEAD.


Building on the great answer from Cameron Skinner, I will expand this a bit to include the case when you have a branch with multiple commits, perhaps merge commits, perhaps a lot of "noise" and you want to cherry-pick only very specific hunks from it. (which was exactly what happened to me today)

Follow these steps:

git checkout source-branch
git checkout -b cherry-pick-branch # Gives you a new branch based on source-branch
git reset --mixed master

This will leave you with a branch where all changes are in the filesystem, but nothing has been commited/staged. Then use git add -p to selectively add which parts you want to stage, and then commit & push as normal.

Tags:

Git