How can I reset or revert a file to a specific revision?

前端 未结 30 1946
不思量自难忘°
不思量自难忘° 2020-11-21 11:23

I have made some changes to a file which has been committed a few times as part of a group of files, but now want to reset/revert the changes on it back to a previous versio

30条回答
  •  名媛妹妹
    2020-11-21 11:46

    Assuming the hash of the commit you want is c5f567:

    git checkout c5f567 -- file1/to/restore file2/to/restore
    

    The git checkout man page gives more information.

    If you want to revert to the commit before c5f567, append ~1 (where 1 is the number of commits you want to go back, it can be anything):

    git checkout c5f567~1 -- file1/to/restore file2/to/restore
    

    As a side note, I've always been uncomfortable with this command because it's used for both ordinary things (changing between branches) and unusual, destructive things (discarding changes in the working directory).

提交回复
热议问题