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

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

问题描述

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

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 log 和一个 git 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).

还有一个新的 git restore 命令专为恢复已修改的工作副本文件而设计.如果您的 git 足够新,您可以使用此命令,但文档附带警告:

There is also a new git restore command that is specifically designed for restoring working copy files that have been modified. If your git is new enough you can use this command, but the documentation comes with a warning:

此命令是实验性的.行为可能会改变.

THIS COMMAND IS EXPERIMENTAL. THE BEHAVIOR MAY CHANGE.

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

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