git selective revert local changes from a file

前端 未结 6 900
花落未央
花落未央 2021-01-29 18:55

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

6条回答
  •  深忆病人
    2021-01-29 19:19

    Re-reading the question, it sounds like you want to revert changes that are in your working tree and not changes that have been previously committed but some of the other answers make it sound like my reading may be wrong. Can you clarify?

    If the changes are just in your working copy then the easiest way to do this is to stage the changes you want to keep with:

    git add -i 
    

    Then throw away the changes that you don't want to keep by checking out the index version:

    git checkout -- 
    

    Then unstage the changes if you don't want them staged yet:

    git reset -- 
    

    This recipe only reverts selected changes to the file (or files that you specify) and doesn't create any temporary commit that then needs reverting.

    If you want to selectively apply only some of the changes made in previous commits then you can reset a file to a previous committed state first:

    git reset  -- 
    

    Then you can follow the previous recipe of git add -i to stage those changes that you want to keep, git checkout -- to throw away the unwanted changes and git reset -- to 'unstage' the changes.

提交回复
热议问题