删除在远程分支上推送的 git commit
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44441116/
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
Delete a git commit pushed on a remote branch
提问by Adrien BARRAL
We use git and GitFlow as versioning tools, and I'm facing a problem that I am quite sure some people have already encountered.
我们使用 git 和 GitFlow 作为版本控制工具,我正面临一个问题,我很确定有些人已经遇到过。
In a feature branch, a colleague did a commit in which a lot of useless files were inserted (all binary and IDE metadata files). This commit was pushed. Now this colleague submits a pull request, and I don't want to integrate all these useless files into develop
.
在一个功能分支中,一位同事做了一个提交,其中插入了大量无用的文件(所有二进制文件和 IDE 元数据文件)。这个提交被推送了。现在这个同事提交了一个pull request,我不想把这些没用的文件都整合到develop
.
Now, my git repository is quite huge, and a git clone becomes a long and boring process...
现在,我的 git 存储库相当庞大,一个 git clone 变成了一个漫长而无聊的过程......
The first (and easy) solution is to remove these files from the feature branch, and commit without these files (or do a revert commit
). But if I do that, my git repository will remain quite big because objects (files) will not be removed from the history (because git still knows about them!).
第一个(也是简单的)解决方案是从功能分支中删除这些文件,并在没有这些文件的情况下提交(或执行revert commit
)。但是如果我这样做,我的 git 存储库将保持相当大,因为对象(文件)不会从历史记录中删除(因为 git 仍然知道它们!)。
What I would like is to "cancel" this commit.
我想要的是“取消”这个提交。
The following picture shows my problem (the topmost commit is the oldest):
下图显示了我的问题(最上面的提交是最旧的):
How can I remove this commit and make my git clone quick again ?
如何删除此提交并使我的 git clone 再次快速?
PS: Thanks to GitGraphJS for their useful tools to draw git graphs!
PS:感谢 GitGraphJS 提供有用的工具来绘制 git 图!
回答by danglingpointer
There are several methods to delete or undo the commit. In that case, you can use git revert
or git rebase
or git reset
.
有多种方法可以删除或撤消提交。在这种情况下,您可以使用git revert
或git rebase
或git reset
。
One information, git rebase
don't add one extra commit compared to using git revert. git revert adds one extra commit during the merge. If you're not familiar with using git rebase, I suggest you use git revert
then.
一个信息,git rebase
与使用 git revert 相比,不要添加一个额外的提交。git revert 在合并期间添加一个额外的提交。如果您不熟悉使用 git rebase,我建议您使用git revert
then。
Revert
恢复
git revert <commit-ID>
after that you commit the changes and push that commit to your origin.
之后,您提交更改并将该提交推送到您的源。
Reset
重启
You can delete the last pushed commit, after doing this you have to push your branch. You can decide either to merge or rebase the feature branch to development branch.
您可以删除最后一次推送的提交,这样做之后您必须推送您的分支。您可以决定将功能分支合并或重新设置为开发分支。
git reset –-hard HEAD~1
or
或者
git reset HEAD^ --hard
Rebase
变基
This command will lead to interactive section of editor, from there you can pick
此命令将导致编辑器的交互式部分,您可以从那里选择
git rebase -i HEAD~
In the interactive section, it lists all the commits. Delete the one you want to get rid off. Finish the rebase and push force to the repo.
在交互部分,它列出了所有提交。删除你想摆脱的那个。完成 rebase 并将力推到 repo。
git rebase --continue
then push your branch.
git rebase --continue
然后推动你的分支。