使用Git将特定文件重置或恢复到特定修订版本? [英] Reset or revert a specific file to a specific revision using Git?

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

问题描述

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



我已经完成了一个 git log 以及一个 git diff 找到我需要的修订版本,但是不知道如何将文件恢复到以前的状态。

假设你想要的提交的散列是 c5f567

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

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



如果你想恢复到 c5f567 之前的提交,追加〜1 (可以使用任何数字):

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

作为一个方面说明,我一直对这个命令感到不舒服,因为它用于普通的东西(在分支之间转换)和不寻常的破坏性的东西(丢弃的变化工作目录)。


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.

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.

解决方案

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 (works with any number):

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将特定文件重置或恢复到特定修订版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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