简体   繁体   English

如何在git中还原为旧版本

[英]How to revert to older version in git

On github we are at commit 57d7cb274622f86e586c2595317264f23626e6c5 I want to revert to commit 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c which is several commits before the latest one. 在github上,我们的提交为57d7cb274622f86e586c2595317264f23626e6c5,我想还原为提交5798ee7c56efd7e898d3115da6efe3d5e7cddb6c,这是最近一次提交之前的几次提交。 Here is what I am doing: 这是我在做什么:

$ git fetch origin
$ git reset --hard origin/master
Head is now at  57d7cb274
$ git status
nothing to commit (working directory clean)
$git revert 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c
#On branch master
nothing to commit (working directory clean)
$ git status
#On branch master
nothing to commit (working directory clean)

But the resulting code is not really at the commit at 5798ee7c56efd7e898d3115da6efe3d5e7cddb6c What am I missing? 但是生成的代码不是真正在5798ee7c56efd7e898d3115da6efe3d5e7cddb6c处提交的内容吗?

According to the documentation for git at https://www.kernel.org/pub/software/scm/git/docs/git-revert.html , git revert creates commits which reverse the specified commit. 根据https://www.kernel.org/pub/software/scm/git/docs/git-revert.html上 git的文档,git revert创建了可反转指定提交的提交。

Therefore, the reason git status says there is nothing to commit after running git revert is that the changes you are looking for were already committed. 因此,在运行git revert后git status表示没有任何要提交的原因是,您要查找的更改已被提交。

I think what you are trying to achieve is done, but your test to see if it worked is flawed. 我认为您想要实现的目标已经完成,但是您进行测试以查看其是否有效是有缺陷的。

You are mixing up git revert and git reset . 您正在混合git revertgit reset

With git reset you really throw away all those commits and you result at exactly the given commit. 使用git reset您实际上丢弃了所有这些提交,并且得到的结果恰好是给定的提交。

But git revert will actually add another commit which does exactly the opposite as the original commit, and therefore reverts that commit. 但是git revert实际上会添加另一个提交,该提交与原始提交完全相反,因此会还原该提交。

Using git reset is fine for your local repository, but once you pushed your commits other people might already started using those commits for their own work. 对于本地存储库,使用git reset很合适,但是一旦您提交了提交,其他人可能已经开始将这些提交用于自己的工作。 Therefore you want an explicit revert commit in this case. 因此,在这种情况下,您需要显式还原提交。

Be aware that you need to revert each unneeded commit if you want to go the revert way. 请注意,如果要使用还原方式,则需要还原每个不需要的提交。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM