更改已推送的 git 提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23832812/
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
change a git commit already pushed
提问by nha
I accidently overwrote another developper's changes when doing a merge in git. I know how to undo the last commit, that is, my merge.
在 git 中进行合并时,我不小心覆盖了另一个开发人员的更改。我知道如何撤消最后一次提交,即我的合并。
My problem is that I already pushed those commits into our online repository. So if I roll back, do my merge merge again (with his modifications this time) and try to push it again, there will be a conflict (right?). What is right the way to handle this ?
我的问题是我已经将这些提交推送到我们的在线存储库中。因此,如果我回滚,再次执行我的合并合并(这次是他的修改)并尝试再次推送,则会发生冲突(对吧?)。处理这个问题的正确方法是什么?
EDITTo clarify, here is what the situation looks like :
编辑为了澄清,情况如下:
commit A --- commit B --- merge
But in the merge I accidentally discarded the modifications made in commit A. This isn't really a problem. I know how to make the changes locally (undo the merge). Butmy problem is that the whole thing has been pushed into our shared repository (think github or bitbucket).
但是在合并中我不小心丢弃了在提交 A 中所做的修改。这不是一个真正的问题。我知道如何在本地进行更改(撤消合并)。但我的问题是整个东西都被推到了我们的共享存储库中(想想 github 或 bitbucket)。
采纳答案by poke
By default, remote servers will disallow overwriting already pushed commits. This is because those new commits are different objects which are incompatible to those published before. This means that anyone who has already fetched from the remote since will have major problems fixing it once you overwrite the commit. So you should really reconsider overwriting the commit with something else. Note that git revert
works with merge commits too, so you might want to consider that instead.
默认情况下,远程服务器将不允许覆盖已经推送的提交。这是因为这些新提交是不同的对象,与之前发布的那些不兼容。这意味着一旦你覆盖了提交,任何已经从远程获取的人都会在修复它时遇到重大问题。所以你真的应该重新考虑用其他东西覆盖提交。请注意,它也git revert
适用于合并提交,因此您可能需要考虑这一点。
That being said, you still can push that rewritten commit even if it conflicts with what's on the server. You can do that by force pushing using git push --force
or git push -f
in short.
话虽如此,您仍然可以推送重写的提交,即使它与服务器上的内容冲突。您可以通过强制使用git push --force
或git push -f
简而言之来做到这一点。