在“git push”之后如何在本地和远程撤消“git commit”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6459080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I undo a `git commit` locally and on a remote after `git push`
提问by michael
I have performed git commit
followed by a git push
. How can I revert that change on both local and remote repositories?
我已经执行git commit
了一个git push
. 如何在本地和远程存储库上恢复该更改?
$ git log
commit 364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8
Author: Michael Silver <Michael [email protected]>
Date: Tue Jun 11 12:24:23 2011 -0700
回答by Alexander Gro?
git reset --hard HEAD~1
git push -f <remote> <branch>
(Example push:git push -f origin bugfix/bug123
)
(实施例推: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.
这将撤消最后一次提交并将更新的历史记录推送到远程。您需要传递 ,-f
因为您要替换遥控器中的上游历史记录。
回答by Amadan
Generally, make an "inverse" commit, using:
通常,使用以下方法进行“反向”提交:
git revert 364705c
then send it to the remote as usual:
然后像往常一样将其发送到遥控器:
git push
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.
这不会删除提交:它会进行额外的提交,撤消第一次提交所做的任何事情。其他任何东西,都不是真正安全的,尤其是当更改已经传播时。
回答by kmonsoor
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.
我知道您无意中将代码推送到remote-master
. 这会没事的。
1.At first, get the SHA-1
value of the commit you are trying to return, e.g. commit to master branch. run this:
1.首先,获取SHA-1
您尝试返回的提交的值,例如提交到主分支。运行这个:
git log
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.
你会看到一堆 'f650a9e398ad9ca606b25513bd4af9fe...' 像字符串一样伴随着每个提交。从要返回的提交中复制该编号。
2.Now, type in below command:
2.现在,输入以下命令:
git reset --hard your_that_copied_string_but_without_quote_mark
you should see message like "HEAD is now at ". you are on clear. What it just have done is to reflect that change locally.
您应该会看到“HEAD 现在在”之类的消息。你很清楚。它刚刚所做的就是在本地反映这种变化。
3.Now, type in below command:
3.现在,输入以下命令:
git push -f
you should see like
你应该看到像
"warning: push.default is unset; its implicit value has changed in..... ... Total 0 (delta 0), reused 0 (delta 0) ... ...your_branch_name -> master (forced update)."
“警告:push.default 未设置;其隐含值已更改..... 总计 0(delta 0),重用 0(delta 0)... ...your_branch_name -> master(强制更新) .”
Now, you are all clear. Check the master with "git log" again, your fixed_destination_commit should be on top of the list.
现在,你们都清楚了。再次使用“git log”检查 master,您的 fixed_destination_commit 应该在列表的顶部。
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-pickcommands.
现在,您在所有这些开始之前所做的更改现在都消失了。如果你想把那些辛苦的工作带回来,那是可能的。感谢git reflog和git cherry-pick命令。
For that, i would suggest to please follow this blogor this post.
回答by softvar
git reset HEAD~1
if you don't want your changes to be gone(unstaged changes). Change, commit and push again git push -f [origin] [branch]
git reset HEAD~1
如果您不希望更改消失(未暂存的更改)。更改、提交并再次推送git push -f [origin] [branch]
回答by Hyman Edmonds
You can do an interactive rebase:
您可以进行交互式变基:
git rebase -i <commit>
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
请参阅此问题:Git:从存储库中删除选定的提交
回答by Mohit Dhawan
Try using
尝试使用
git reset --hard <commit id>
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.
请注意:这里的提交 id 将是您要访问的提交的 id,而不是您要重置的 id。这是我也被卡住的唯一一点。
then push
然后推
git push -f <remote> <branch>
回答by MicRum
Alternatively:
或者:
git push origin +364705c23011b0fc6a7ca2d80c86cef4a7c4db7ac8^:master
Force the master branch of the origin remote repository to the parent of last commit
强制原远程仓库的 master 分支到上次提交的父级