# This will detach your HEAD,that is,leave you with no branch checked out:git checkout 0d1d7fc32 ' T% S" M- x+ \) I( Y2 p/ M: n
或者,如果您想在那里提交,请继续创建一个新的分支: 2 u0 N" Q7 g& e* O$ W( Ggit checkout -b old-state 0d1d7fc32回到原来的位置,只要再次检查你的分支机构。(如果你改变了它,你必须像往常一样处理它们。你可以把它们重置扔掉;你可以把它们藏起来,结账,藏起来很受欢迎;如果你想在那里开设分支机构,请把它们转移到那里。' x4 I, Q3 S3 U: r. @1 E
硬删除未发布的提交另一方面,如果你真的想摆脱从那时起所做的一切,有两种可能性。1、如果您没有发布任何这些提交,只需重置:- d2 ^/ y/ E/ s
# This will destroy any local modifications.# Don't do it if you have uncommitted work you want to keep.git reset --hard 0d1d7fc32# Alternatively,if there's work to keep:git stashgit reset --hard 0d1d7fc32git stash pop# This saves the modifications,then reapplies that patch after resetting.# You could get merge conflicts,if you've modified things which were# changed since the commit you reset to. $ f8 p/ b& y! G: U. a
如果你搞砸了,你已经扔掉了当地的变化,但至少你可以通过重置回到以前的位置。2 q8 }( R3 k4 E) e n8 f" Z
使用新提交撤销已发布的提交另一方面,如果你已经发表了作品,你可能不想重置分支,因为它实际上是在重写历史。在这种情况下,您确实可以恢复提交。Git,revert 有一个非常特殊的含义:使用反向补丁创建并提交以取消它。这样你就不会重写任何历史了。 0 R5 E$ h, t: w8 y4 ]* @0 L
# This will create three separate revert commits:git revert a867b4af 25eee4ca 0766c053# It also takes ranges. This will revert the last two commits:git revert HEAD~2..HEAD#Similarly,you can revert a range of commits using commit hashes (non inclusive of first hash):git revert 0d1d7fc..a867b4a# Reverting a merge commitgit revert -m 1 # To get just one,you could use `rebase -i` to squash them afterwards# Or,you could do it manually (be sure to do this at top level of the repo)# get your index and work tree into the desired state,without changing HEAD:git checkout 0d1d7fc32 .# Then commit. Be sure and write a good message describing what you just didgit commit 7 C1 T& Y9 U+ X% r2 y! j