简体   繁体   English

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

[英]How to delete commits from git on Github and Bitbucket

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

Commit History:提交历史:

94ca48e 94ca48e

55fab05 55fab05

3813803 3813803

I want to delete 94ca48e, and 55fab05.我想删除 94ca48e 和 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.我曾经找到一个命令,您可以将 3813803 设为最新的提交,并且在该提交之后远程存储库中的所有内容都将被删除,但我在任何地方都找不到它。

Use git reset --hard 3813803 .使用git reset --hard 3813803 This can not be undone and works locally as well as remote.无法撤消,并且在本地和远程都可以使用。

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.恢复旨在安全地撤消公共提交,而 git reset 旨在撤消本地更改。 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 is the one to be used here, though, because you are asking for complete deletion. git reset是这里使用的一个,因为您要求完全删除。

Even if the commits are HARD reset, unwanted commits would still show up in the history.即使提交被硬重置,不需要的提交仍然会出现在历史记录中。 If the history also needs to be reset, then I am afraid only way to do that is to delete and recreate the branch.如果历史也需要重置,那么恐怕唯一的方法就是删除并重新创建分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM