简体   繁体   English

将所有更改还原为先前提交的状态

[英]Revert all changes to state of a previous commit

I am working in Git and want to know the best way to revert all of my changes to this Repo back to a specific commit. 我在Git工作,想知道将我对Repo的所有更改还原为特定提交的最佳方法。 For example, here is an example of what my log looks like: 例如,以下是我的日志的示例:

commit hash#1

commit hash#2

commit hash#3

commit hash#4

I am in my master branch and want to revert all changes made to this repo back to commit hash#4. 我在我的master分支中,并希望将对此repo所做的所有更改恢复为提交#4哈希。 So that means I want to undo the changes that were committed in hash#1,hash#2, and hash#3. 这意味着我想撤消在散列#1,散列#2和散列#3中提交的更改。

I do NOT want to rewrite/delete history, as this is a public repo. 我不想重写/删除历史记录,因为这是一个公共回购。 And I do want to (re)commit hash#4 once it has been reverted to. 而且我确实希望(重新)提交哈希#4一旦它被还原为。

Thanks! 谢谢!

Try the following 请尝试以下方法

git reset --hard commit_hash_4
git reset --soft commit_hash_1

The first command will reset the working directory and head back to commit_hash_4. 第一个命令将重置工作目录并返回commit_hash_4。 The second command will move the head back to commit_hash_1. 第二个命令将头部移回commit_hash_1。 Comitting at this point will preserve linear history but will give you the state of commit_hash_4 此时进行操作将保留线性历史记录,但会为您提供commit_hash_4的状态

A great reset overview per ( http://git-scm.com/2011/07/11/reset.html ): 一个很棒的重置概述( http://git-scm.com/2011/07/11/reset.html ):

The reset command overwrites these three trees in a specific order, stopping when you tell it to. reset命令按特定顺序覆盖这三棵树,当你告诉它时停止。

1) Move whatever branch HEAD points to (stop if --soft) 1)移动任何分支HEAD指向(停止,如果--soft)

2) THEN, make the Index look like that (stop here unless --hard) 2)然后,使索引看起来像那样(除非--hard,否则停在这里)

3) THEN, make the Working Directory look like that 3)然后,使工作目录看起来像那样

Just wanted to share for any other users that come across this question. 只想分享给遇到此问题的任何其他用户。

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

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