![](/img/trans.png)
[英]How to revert files to a previous commit but keep other files to latest commit
[英]How do I revert to the previous commit without deleting the latest commit?
假设我有 2 个提交已经推送到我的远程分支。
如何在不删除提交 A 的情况下恢复到提交 B? 我只想比较这两个提交之间的结果。
注意:不需要代码比较。 我只想比较 Commit A 与 Commit B 的 output
我强烈不同意其他建议您使用git revert
的答案。 这实际上会恢复提交 A引入的更改并自行生成提交。
由于您想在某个时间点查看 state,您可以直接签出Commit B ,以便检查内容。 然后将原始分支检出到 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)
很多工具可以让你直接看到两个引用之间的区别,而无需检查。 在命令行上,这可以通过git diff
来完成:
git diff $HASH_OF_COMMIT_A..$HASH_OF_COMMIT_B
如何在不删除提交 A 的情况下恢复到提交 B?
您可以轻松使用git rebase -i <commit_hash>
HEAD <- 提交 A <- 提交 B <- 提交 C
git rebase -i <commit_hash_commitC>
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
您可以进行交互式变基,删除您不需要的提交,并在您的删除分支上进行硬推送。
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
恢复提交 B不会删除提交A。它只会创建提交 C ,这将撤消您在提交 B中所做的一切
git revert commit-b-hash
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.