如何从 Github 和 Bitbucket 上的 git 中删除提交

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

How to delete commits from git on Github and Bitbucket

gitgithubrepositorybitbucketcommit

提问by TJB

I accidentally pushed up files from my .idea directory in my Django project which I had in my .gitignore file. I am trying to completely delete the commit from my bitbucket repository since there is someone else Im working with on the project and he can't pull my changes without affecting his own .idea files. I have seen other SO questions where they say to use git revert, however I remember there was another command where you pushed the last good commit you made, and everything after that was deleted from the master branch. For example

我不小心从我的 .gitignore 文件中的 Django 项目中的 .idea 目录中推送了文件。我正在尝试从我的 bitbucket 存储库中完全删除提交,因为我正在与其他人一起处理该项目,并且他无法在不影响他自己的 .idea 文件的情况下提取我的更改。我看过其他 SO 问题,他们说要使用 git revert,但是我记得还有另一个命令,您在其中推送了您所做的最后一次提交,之后的所有内容都从 master 分支中删除。例如

Commit History:

提交历史:

94ca48e

94ca48e

55fab05

55fab05

3813803

3813803

I want to delete 94ca48e, and 55fab05. There was a command I found once where you could make 3813803 the most current commit, and everything in the remote repository after that commit would be deleted, but I can't find it anywhere.

我想删除 94ca48e 和 55fab05。我曾经找到一个命令,您可以在其中将 3813803 设为最新的提交,并且在该提交之后远程存储库中的所有内容都将被删除,但我在任何地方都找不到它。

回答by harmonica141

Use git reset --hard 3813803. This can not be undoneand works locally as well as remote.

使用git reset --hard 3813803. 这无法撤消,并且在本地和远程都可以使用。

For remote push using git push --force origin master

对于远程推送使用 git push --force origin master

Have a look at the git docu by Atlassian here.

此处查看Atlassian 的 git docu 。

Let me also quote from there:

让我也从那里引用:

Whereas reverting is designed to safely undo a public commit, git reset is designed to undo local changes. Because of their distinct goals, the two commands are implemented differently: resetting completely removes a changeset, whereas reverting maintains the original changeset and uses a new commit to apply the undo.

恢复旨在安全地撤消公共提交,而 git reset 旨在撤消本地更改。由于它们不同的目标,这两个命令的实现方式不同:重置完全删除更改集,而还原保留原始更改集并使用新提交来应用撤消。

git resetis the one to be used here, though, because you are asking for complete deletion.

git reset是这里要使用的那个,因为您要求完全删除。