Search code examples
gitversion-controlgit-svn

git selective revert local changes from a file


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.


Solution

  • You can do that directly with git checkout -p. See Daniel Stutzbach's answer below.


    Old answer (before checkout -p was introduced):

    You can do it like this:

    git add -i
    

    (select the hunks you want to keep)

    git commit -m "tmp"
    

    Now you have a commit with only the changes you want to keep, and the rest is unstaged.

    git reset --hard HEAD
    

    At this point, uncommitted changes have been discarded, so you have a clean working directory, with the changes you want to keep committed on top.

    git reset --mixed HEAD^
    

    This removes the last commit ('tmp'), but keeps the modifications in your working directory, unstaged.

    EDIT: replaced --soft with --mixed, to clean up the staging area.