Git-将指定文件回退到指定版本

121 阅读1分钟

场景1:修改了文件/path/to/file,没有提交,但是觉得改的不好,想还原。

解法

git checkout -- /path/to/file

场景2:修改了文件/path/to/file,已经提交,但是觉得改的不好,想还原到上衣版本。

解法

  1. 首先查看文件的历史版本。git log /path/to/file
  2. 找到你想要还原的版本。如
commit 5d5513249ae92b0d8003023a185b3159459a098f
Author: panww <test@qq.com>
Date: Wed Nov 8 11:48:31 2017 +0800
commit modify/path/to/file
  1. 将文件还原到你想要还原的版本。$ git checkout ${commit} /path/to/file。即git checkout 5d5513249ae92b0d8003023a185b3159459a098f /path/to/file
  2. 这时查看文件,会发现文件已经还原了。