问题描述
我不小心"向 GitHub 推送了一个提交.
I "accidentally" pushed a commit to GitHub.
是否可以删除此提交?
我想恢复我的 GitHub 存储库,就像这次提交之前一样.
I want to revert my GitHub repository as it was before this commit.
推荐答案
git reset --soft HEAD^
首先,删除本地存储库上的提交.你可以使用 git rebase -i
来做到这一点.例如,如果这是您的最后一次提交,您可以执行 git rebase -i HEAD~2
并删除弹出的编辑器窗口中的第二行.
First, remove the commit on your local repository. You can do this using git rebase -i
. For example, if it's your last commit, you can do git rebase -i HEAD~2
and delete the second line within the editor window that pops up.
然后,使用 git push origin +branchName --force
参见 Git Magic 第 5 章:历史教训 - 以及然后是一些以获取更多信息(即,如果您想删除较旧的提交).
See Git Magic Chapter 5: Lessons of History - And Then Some for more information (i.e. if you want to remove older commits).
哦,如果你的工作树很脏,你必须先做一个git stash
,然后再做一个git stash apply
.
Oh, and if your working tree is dirty, you have to do a git stash
first, and then a git stash apply
after.
这篇关于如何删除 GitHub 上的提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!