如何使用 Git 删除已推送到远程存储库的提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9731215/
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 to remove a commit that has been pushed to the remote repository using Git
提问by shadowfax
I've pushed a couple of commits to the remote repository and found they are creating problems.
我已经向远程存储库推送了几次提交,发现它们正在产生问题。
How can I go back to the previous version? That is, removing the two latest commits?
我怎样才能回到以前的版本?也就是说,删除两个最新的提交?
回答by David M. Syzdek
Since you have already pushed the commits to a remote repository, the best way is probably to revert the two commits so that you do not create any problems for anyone who has already pulled from the remote repository.
由于您已经将提交推送到远程存储库,因此最好的方法可能是还原两次提交,这样您就不会为已经从远程存储库中提取的任何人造成任何问题。
Examples use the following commit history:
示例使用以下提交历史:
e512d38 Adding taunts to management.
bd89039 Adding kill switch in case I'm fired.
da8af4d Adding performance optimizations to master loop.
db0c012 Fixing bug in the doohickey
If you just want to revert the commits without modifying the history, you can do the following:
如果您只想在不修改历史记录的情况下还原提交,您可以执行以下操作:
git revert e512d38
git revert bd89039
Alternatively, if you don't want others to see that you added the kill switch and then removed it, you can roll back the repository using the following (however, this will cause problems for others who have already pulled your changes from the remote):
或者,如果您不希望其他人看到您添加了终止开关然后将其删除,您可以使用以下命令回滚存储库(但是,这会给已经从远程拉取您的更改的其他人造成问题) :
git reset --hard da8af4d
git push origin -f localBranch:remoteBranch
where localBranch
is the name of the local branch and remoteBranch
is the name of the remote branch.
其中localBranch
是本地分支remoteBranch
的名称, 是远程分支的名称。
回答by peter
I think you can rollback locally and push the result:
我认为您可以在本地回滚并推送结果:
$ git reset HEAD^ --hard
$ git push REMOTE -f
Where 'REMOTE' is the remote name.
其中“REMOTE”是远程名称。
回答by Vipul Behl
First of all type this command.
首先输入这个命令。
git log -n 4
This command will display your last 4 commits with their SHA. After this type the following command.
此命令将显示您的最后 4 次提交及其 SHA。在此之后键入以下命令。
git rebase -i SHA_ID
Instead of SHA_ID type the SHA of the commit prior to the commit which you want to delete.
在要删除的提交之前键入提交的 SHA,而不是 SHA_ID。
After this a file will open, on the top of that file you will see the SHA and the message of the commit which you want to delete. Delete this line and save and exit out of the file.
之后将打开一个文件,在该文件的顶部,您将看到 SHA 和要删除的提交的消息。删除此行并保存并退出文件。
When you have done this you have to make a push. So type the following command.
完成此操作后,您必须推动。所以输入以下命令。
git push -f origin master
After you are done with this you can see that your commit is deleted.
完成此操作后,您可以看到您的提交已被删除。