如何在不丢失Git的最后提交的情况下返回上一次提交? [英] How to go back to previous commit without losing last commit in Git?

查看:171
本文介绍了如何在不丢失Git的最后提交的情况下返回上一次提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的.我想回到2次提交之前,也许可以重新提交在该提交中更改过的文件.但是我不想丢失我的上一次提交.我的上一次提交在代码中有一些错误,但我现在想保留该错误.

Here is what I want to do. I want to go back to 2 commits before, bring back the files that changed in that commit as a new commit maybe. But I do not want to lose my last commit. My last commit has some mistakes in the code but I would like to keep that one for now.

我阅读了一些文档,但没有一个文件清楚说明重置头部时会发生什么.例如,您是否丢失了所有提交,直到您重置为(向后)提交为止?

I read some documentation but none made it clear about what happens when you reset your head. Do you lose all the commits up until the one that you are resetting to (going backward) for example?

我试图了解所有这些工作原理,但是我对git revertresetcheckout命令感到困惑.

I am trying to understand how all this works but I am rather confused about git revert, reset and checkout commands.

我意识到我应该隐瞒上一次提交而不是提交,但这是另一回事了.

I realize that I should have stashed the last commit instead of committing, but that is another story for now.

推荐答案

revert进行新的提交,以还原由较早提交所做的更改. reset --hard将当前分支的HEAD更改为指定的提交. checkout将工作副本切换到指定的分支或提交.

revert makes a new commit that reverts changes made by an older commit. reset --hard changes the HEAD of the current branch to the specified commit. checkout switches the working copy to the specified branch or commit.

当您将分支重置为较旧的提交时,如果新的提交不是其他分支或标记祖先的一部分,则新的提交将丢失(尽管仍然可以通过reflog访问它们).

When you reset a branch to an older commit the newer commits are lost if they are not parts of other branches or ancestors of tags (they are still accessible via reflog though).

目前尚不清楚您需要做什么,最可能的解决方案是revert(完全还原旧的提交或一系列提交)和rebase -i(更改旧的提交或从历史记录中删除) ).

It is not clear what do you need to do, the most probable solutions are revert (to fully revert an older commit or series of commits) and rebase -i (to change an older commit or delete it from the history).

这篇关于如何在不丢失Git的最后提交的情况下返回上一次提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆