Selectively revert or checkout changes to a file in Git?

后端 未结 4 1179
情歌与酒
情歌与酒 2021-01-30 06:09

Is there a command that allows you to partially undo the changes to a file (or files) in the working directory?

Suppose you have edited a file a lot but you realize that

4条回答
  •  时光取名叫无心
    2021-01-30 06:49

    You could use

    git add -p 
    

    to stage the chunks that you want to keep in a particular file, then

    git checkout -- 
    

    to discard the working tree changes that you didn't want to keep, by checking out the staged version of the file.

    Finally, you can use

    git reset -- 
    

    to revert the staged version of the file to the most recent committed version of the file to leave you with your changes unstaged.

提交回复
热议问题