Git 删除推送提交

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13577283/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 07:48:50  来源:igfitidea点击:

Git delete pushed commits

gitgithub

提问by Lolly

I am using git repository in my project. I have accidentally pushed 2 commits which I shouldn't have. And in between some one has committed on top of it. Is it possible to remove my pushed commits or I have to remove my code changes and push it as new commit, since some one has committed on top of it.

我在我的项目中使用 git 存储库。我不小心推送了 2 个我不应该提交的提交。在这两者之间,有人已经承诺了。是否可以删除我推送的提交,或者我必须删除我的代码更改并将其作为新提交推送,因为有人已经在它上面提交了。

Git Master branch :

Git 主分支:

Commit A // by me

提交 A // 由我

Commit B // by me

提交 B // 由我

Commit C // by some one

由某人提交 C //

Now I have to delete Commit A and B leaving Commit C. Any help will be really appreciated.

现在我必须删除提交 A 和 B 离开提交 C。任何帮助将不胜感激。

采纳答案by eis

You'll have to revertthose commits.

您必须还原这些提交。

Technically what it does is that it removes those changes and makes a new commit, undoing them.

从技术上讲,它的作用是删除这些更改并进行新的提交,撤消它们。

Now, reverting them will leave them on the history stil, but usually that is ok. If that's totally unacceptable only then look at the solutions like filter-branch and force pushes.

现在,恢复它们将使它们留在历史记录中,但通常没关系。如果那是完全不可接受的,则仅查看过滤器分支和强制推动之类的解决方案。

回答by Konst

git reset --hard HEAD~1

Where HEAD~1 means the commit before head.

其中 HEAD~1 表示 head 之前的提交。

Alternatively find the commit id of the commit you want (look at the output of git log), and then do this:

或者找到您想要的提交的提交 ID(查看 git log 的输出),然后执行以下操作:

git reset --hard <sha1-commit-id>

回答by Toby Zheng

git rebase --onto master~3 master~1 master

You can see help by command git help rebase, and then --ontooption, or you see it here. The doc has an example as some as your situation.

您可以通过 commandgit help rebase--onto选项查看帮助,或者您可以在此处查看。该文档有一个与您的情况一样的示例。