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

前端 未结 30 2421
不思量自难忘°
不思量自难忘° 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:56

    You have to be careful when you say "rollback". If you used to have one version of a file in commit $A, and then later made two changes in two separate commits $B and $C (so what you are seeing is the third iteration of the file), and if you say "I want to roll back to the first one", do you really mean it?

    If you want to get rid of the changes both the second and the third iteration, it is very simple:

    $ git checkout $A file
    

    and then you commit the result. The command asks "I want to check out the file from the state recorded by the commit $A".

    On the other hand, what you meant is to get rid of the change the second iteration (i.e. commit $B) brought in, while keeping what commit $C did to the file, you would want to revert $B

    $ git revert $B
    

    Note that whoever created commit $B may not have been very disciplined and may have committed totally unrelated change in the same commit, and this revert may touch files other than file you see offending changes, so you may want to check the result carefully after doing so.

提交回复
热议问题