如何将文件重置或还原到特定版本? [英] How can I reset or revert a file to a specific revision?

查看:118
本文介绍了如何将文件重置或还原到特定版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对一个文件进行了一些更改,该文件已作为一组文件的一部分提交了几次,但现在想将其更改重置/还原为以前的版本.

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 version.

我已经完成了git loggit diff来查找所需的修订,但是却不知道如何使文件恢复到过去的状态.

I have done a git log along with a git diff to find the revision I need, but just have no idea how to get the file back to its former state in the past.

推荐答案

假定所需提交的哈希值为c5f567:

Assuming the hash of the commit you want is c5f567:

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

git checkout 手册页提供了更多信息.

The git checkout man page gives more information.

如果要还原到c5f567之前的提交,请附加~1(其中1是要返回的提交数,可以是任意值):

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).

这篇关于如何将文件重置或还原到特定版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆