git selective revert local changes from a file

Pradeep picture Pradeep · Jul 10, 2009 · Viewed 34k times · Source

In my git repo which is tracking a svn repo I have made a number of edits to a single file.

Now I want to revert those changes(like svn revert), but only portions of the file.

I want to be able to view the diffs on the file, discard(revert) the changes that I don't want and retain the changes I want.

the

git add -i 

command seems to have an option to do that but I don't want to stage this yet.

Answer

Daniel Stutzbach picture Daniel Stutzbach · Mar 4, 2010

I believe you can do it most simply with:

git checkout -p <optional filename(s)>

From the manpage:

   −p, −−patch
       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.