How do I reverse a specific hunk of a commit in git?

dkinzer picture dkinzer · Nov 22, 2010 · Viewed 10.6k times · Source

I'm wondering if there is a way to quickly reverse specific hunk of a commit.

I can generate a diff between two commits or HEAD to see the difference.

How do I reverse just one of those hunks (or better yet, a set of specific hunks)?

Answer

cdhowie picture cdhowie · Nov 22, 2010
git checkout -p $REF -- path/to/file

e.g.,

git checkout -p HEAD^ myfile

Where $REF is a ref name or commit ID that specifies the commit you want to take the file state from. For example, to selectively revert changes made in the last commit, use HEAD^.