简体   繁体   English

如何在不删除最新提交的情况下恢复到上一个提交?

[英]How do I revert to the previous commit without deleting the latest commit?

Say I have 2 commits which I have already pushed to my remote branch.假设我有 2 个提交已经推送到我的远程分支。

  • Commit A (31-May-2021)提交 A(2021 年 5 月 31 日)
  • Commit B (30-May-2021)提交 B(2021 年 5 月 30 日)

How do I revert to Commit B without deleting Commit A?如何在不删除提交 A 的情况下恢复到提交 B? I just want to compare the result between these 2 commits.我只想比较这两个提交之间的结果。

Note: Code comparison is not needed.注意:不需要代码比较。 I just want to compare the output of Commit A vs Commit B我只想比较 Commit A 与 Commit B 的 output

I strongly disagree with the other answers advising you to use git revert .我强烈不同意其他建议您使用git revert的答案。 This would actually really revert the changes introduced by Commit A and produce a commit on its own.这实际上会恢复提交 A引入的更改并自行生成提交。

Since you want to have a look at the state at a point back in time, you could just checkout Commit B directly so you can inspect the contents.由于您想在某个时间点查看 state,您可以直接签出Commit B ,以便检查内容。 Afterwards checkout the original branch to go back to the latest commit.然后将原始分支检出到 go 回到最新的提交。

git checkout $HASH_OF_COMMIT_B   # now you are in a detached head state at commit B
git checkout $BRANCH             # now you are back at the tip of the branch (commit A)

A lot of tools let you see the difference between two references directly without the need to checkout.很多工具可以让你直接看到两个引用之间的区别,而无需检查。 On the command line, this could be done with git diff :在命令行上,这可以通过git diff来完成:

git diff $HASH_OF_COMMIT_A..$HASH_OF_COMMIT_B

How do I revert to Commit B without deleting Commit A?如何在不删除提交 A 的情况下恢复到提交 B?

You can easily use git rebase -i <commit_hash>您可以轻松使用git rebase -i <commit_hash>

  • Select one commit before commit B ie Commit C Select 在提交 B 之前提交一次,即提交 C

HEAD <- Commit A <- Commit B <- Commit C HEAD <- 提交 A <- 提交 B <- 提交 C

  • git rebase -i <commit_hash_commitC>
  • Above command will list commits in your text editor that looks something like this:上面的命令将在您的文本编辑器中列出提交,如下所示:
 pick f7f3f6d Commit B pick a5f4a0d Commit A # Rebase 710f0f8..a5f4a0d onto 710f0f8 # # Commands: # p, pick <commit> = use commit # r, reword <commit> = use commit, but edit the commit message # e, edit <commit> = use commit, but stop for amending # s, squash <commit> = use commit, but meld into previous commit # f, fixup <commit> = like "squash", but discard this commit's log message # x, exec <command> = run command (the rest of the line) using shell # b, break = stop here (continue rebase later with 'git rebase --continue') # d, drop <commit> = remove commit # l, label <label> = label current HEAD with a name # t, reset <label> = reset HEAD to a label # m, merge [-C <commit> | -c <commit>] <label> [# <oneline>] #. create a merge commit using the original merge commit's #. message (or the oneline, if no original merge commit was #. specified). Use -c <commit> to reword the commit message. # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out
  • Remove line that represents Commit B then save and exit the text editor.删除代表提交 B 的行,然后保存并退出文本编辑器。
  • Git will replay all the commits mentioned in the interactive editor and will stop, which will delete Commit B completely. Git 将重播交互式编辑器中提到的所有提交并将停止,这将完全删除提交 B。

You can do an interactive rebase, remove the commits you don't require, and do a hard push on your remove branch.您可以进行交互式变基,删除您不需要的提交,并在您的删除分支上进行硬推送。

git rebase -i     //opens interactive rebasing window

pick #commitID2 My commit 2
pick #commitID1 My commit 1    <- To unpick/remove this commit, using editor remove 
                                  line->

save the window.
git push origin branchname -f 

Reverting Commit B will not delete Commit A. It will simply create commit C which will be undo everything you did in Commit B恢复提交 B不会删除提交A。它只会创建提交 C ,这将撤消您在提交 B中所做的一切

git revert commit-b-hash
git revert <Commit B>

Given one or more existing commits, revert the changes that the related patches introduce, and record some new commits that record them.给定一个或多个现有提交,还原相关补丁引入的更改,并记录一些记录它们的新提交。 This requires your working tree to be clean (no modifications from the HEAD commit).这要求您的工作树是干净的(不修改 HEAD 提交)。 git docs git 文档


However, if you just want to see the difference between the two:但是,如果您只想查看两者之间的区别:

git diff <Commit B>..<Commit A>

Show changes between the working tree and the index or a tree, changes between the index and a tree, changes between two trees, changes resulting from a merge, changes between two blob objects, or changes between two files on disk.显示工作树和索引或树之间的更改、索引和树之间的更改、两棵树之间的更改、合并导致的更改、两个 blob 对象之间的更改或磁盘上两个文件之间的更改。 git docs git 文档

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

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