如何在“git Push”之后在本地和远程撤消“git commit”
我已经执行了 git commit ,然后执行了 git push 。如何在本地和远程存储库上恢复该更改?
$ git log
commit 364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8
Author: Michael Silver <Michael [email protected]>
Date: Tue Jun 11 12:24:23 2011 -0700
I have performed git commit
followed by a git push
. How can I revert that change on both local and remote repositories?
$ git log
commit 364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8
Author: Michael Silver <Michael [email protected]>
Date: Tue Jun 11 12:24:23 2011 -0700
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
(示例推送:
git push -f origin bugfix/bug123
)这将撤消上次提交并将更新的历史记录推送到远程。您需要传递
-f
因为您要替换远程中的上游历史记录。编辑:
请注意,
--hard
将使您的提交无法访问(即它看起来已被删除,但您仍然可以git show< /code> 或
git log
如果您还记得它的哈希值)。如果您想保留更改,请运行:此时您已经有了未暂存的更改,因为您使用了默认的
--mixed
。您可能首先想先更新远程树(即删除提交): git push -f;
由于您仍然在本地进行更改,因此您可以创建另一个分支并在那里
提交
它们(并根据您的需要推送
)。(Example push:
git push -f origin bugfix/bug123
)This will undo the last commit and push the updated history to the remote. You need to pass the
-f
because you're replacing upstream history in the remote.Edit:
Please note that
--hard
will make your commit unreachable (i.e. it will appear to be deleted, but you can stillgit show <hash>
orgit log <hash>
it if you remember its hash). If you want to keep your changes, run:At this point you have unstaged changes because you used
--mixed
, which is the default.You may first want to update the remote tree first (i.e. remove the commit):
git push -f <remote> <branch>
Since you still have your changes locally you can create another branch and
commit
them there (andpush
as you see fit).一般来说,进行“反向”提交,使用:
然后照常将其发送到远程:
这不会删除该提交:它会进行额外的提交,撤消第一次提交所做的任何操作。其他任何事情都不太安全,尤其是当更改已经传播时。
Generally, make an "inverse" commit, using:
then send it to the remote as usual:
This won't delete the commit: it makes an additional commit that undoes whatever the first commit did. Anything else, not really safe, especially when the changes have already been propagated.
首先,放松。
“没有什么是在我们的控制之下。我们的控制只是幻觉。”,“人都会犯错”
我知道你无意中推动了你的代码到
remote-master
。一切都会好起来的。1. 首先,获取您尝试返回的提交的
SHA-1
值,例如提交到 master 分支。运行这个:你会看到一堆'f650a9e398ad9ca606b25513bd4af9fe...'像字符串以及每个提交。从您想要返回的提交中复制该编号。
2. 现在,输入以下命令:
您应该看到类似“HEAD is now at”的消息。你已经清楚了。它刚刚所做的就是在本地反映这种变化。
3. 现在,输入以下命令:
您应该看到类似
现在,你都清楚了。再次用“git log”检查 master,你的fixed_destination_commit 应该在列表的顶部。
不客气(提前;))
更新:
现在,在这一切开始之前您所做的更改现在都消失了。
如果你想再次恢复那些辛苦工作,这是可能的。感谢 git reflog 和 gitcherry-pick 命令。
为此,我建议请遵循 此博客或此发布。
First of all, Relax.
"Nothing is under our control. Our control is mere illusion.", "To err is human"
I get that you've unintentionally pushed your code to
remote-master
. THIS is going to be alright.1. At first, get the
SHA-1
value of the commit you are trying to return, e.g. commit to master branch. run this:you'll see bunch of 'f650a9e398ad9ca606b25513bd4af9fe...' like strings along with each of the commits. copy that number from the commit that you want to return back.
2. Now, type in below command:
you should see message like "HEAD is now at ". you are on clear. What it just have done is to reflect that change locally.
3. Now, type in below command:
you should see like
Now, you are all clear. Check the master with "git log" again, your fixed_destination_commit should be on top of the list.
You are welcome (in advance ;))
UPDATE:
Now, the changes you had made before all these began, are now gone.
If you want to bring those hard-works back again, it's possible. Thanks to git reflog, and git cherry-pick commands.
For that, i would suggest to please follow this blog or this post.
git reset HEAD~1
如果您不希望更改消失(未暂存的更改)。更改、提交并再次推送git push -f [origin] [branch]
git reset HEAD~1
if you don't want your changes to be gone(unstaged changes). Change, commit and push againgit push -f [origin] [branch]
尝试使用
请注意:这里的提交ID将是您想要转到的提交的ID,而不是您想要重置的ID。这是我也陷入困境的唯一一点。
然后推
Try using
Please Note : Here commit id will the id of the commit you want to go to but not the id you want to reset. this was the only point where i also got stucked.
then push
正如大多数人建议的那样,使用 git reset --hard HEAD~1 会在本地撤消最后一次提交,但您也会丢失这些本地更改。
如果您不想丢失本地更改,而只是撤消上次提交:
则将其强制推送到源以撤消远程中的提交(假设
== origin
)Using
git reset --hard HEAD~1
, as most suggest, will undo the last commit locally, but you will also lose those local changes.If you don't want to lose the local changes, and rather just undo last commit:
Then force push it to the origin to undo the commit in the remote (assuming
<remote> == origin
)您可以进行交互式变基:
这将打开您的默认编辑器。只需删除包含要删除的提交的行即可删除该提交。
当然,您也需要访问远程存储库才能应用此更改。
请参阅此问题:Git:从存储库中删除选定的提交
You can do an interactive rebase:
This will bring up your default editor. Just delete the line containing the commit you want to remove to delete that commit.
You will, of course, need access to the remote repository to apply this change there too.
See this question: Git: removing selected commits from repository
或者:
将原始远程存储库的 master 分支强制到上次提交的父分支
Alternatively:
Force the master branch of the origin remote repository to the parent of last commit