Git Diff – How do I revert selected lines or chunks

Stage the parts you want with git add -p, then discard (git checkout -- filename) the unstaged changes.

Update for Git 1.6.5+

In version 1.6.5, Git learned to checkout with a -p/--patch flag. You can discard chunks in one step with git checkout -p -- filename.

From the docs:

Interactively select hunks in the difference between the <tree-ish> (or the index, if unspecified) and the working tree. The chosen hunks are then applied in reverse to the working tree (and if a <tree-ish> was specified, the index).

This means that you can use git checkout -p to selectively discard edits from your current working tree.

Leave a Comment