Question

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)?

Was it helpful?

Solution

git checkout -p $REF -- path/to/file

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^.

OTHER TIPS

git difftool $REF -- /path/to/file

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^.

This question was already answered by @cdhowie, but I find it somewhat nicer to use an interactive difftool like meld to selectively restore old hunks/lines of code, especially if there is a newly-introduced, hard-to-find bug in the code.

To recover a deleted file from a previous commit I used the answer here:

Find and restore a deleted file in a Git repository

git checkout <deleting_commit>^ -- <file_path>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top