GIT:如何压缩已推送到远程存储库的多个提交?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7774801/
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
GIT: how to squash several commits that have been pushed to a remote repo?
提问by wei
I have a weird setup with Git. Basically I have:
我对 Git 有一个奇怪的设置。基本上我有:
[client 1] <---> [remote repo] ----> [client 2]
[Client 1] is essentially the local repo I am working with, because I can't compile/build the project on my local machine.
[Client 1] 本质上是我正在使用的本地存储库,因为我无法在本地机器上编译/构建项目。
[Client 2] is a remote server for building.
[客户端 2] 是用于构建的远程服务器。
In the middle, I have another repo, [remote repo], basically for synchronizing with a cvs central repo in my company, and also synchronizing between my [client 1] and [client 2].
在中间,我有另一个 repo,[remote repo],主要用于与我公司的 cvs 中央 repo 同步,以及在我的 [client 1] 和 [client 2] 之间同步。
Since all the compiling/building is done on [client 2], I have many trivial commits on [client 1] just for fixing the compilation or building errors.
由于所有编译/构建都是在 [client 2] 上完成的,我在 [client 1] 上有许多琐碎的提交只是为了修复编译或构建错误。
So by the time I find out there are errors in the last commit, it's already too late because the commit has already been pushed to and pulled from the remote repo.
因此,当我发现上次提交中存在错误时,已经太晚了,因为该提交已被推送到远程存储库并从远程存储库中拉出。
How can I squash these (many) trivial commits into one? Thanks.
我怎样才能将这些(许多)琐碎的提交压缩成一个?谢谢。
回答by manojlds
First of all, avoid squashing and in general rewriting history unless you absolutely have to. Having "trivial" commits is not reason to squash pushed commits. If they can stay, let them stay. And rewriting history is not straightforward in cvs at all, so since these commits would have made their way into the cvs repo, you should probably live with it.
首先,除非绝对必要,否则避免挤压和重写历史。拥有“微不足道”的提交并不是压制推送提交的理由。如果他们能留下,就让他们留下。在 cvs 中重写历史根本不是一件简单的事情,所以既然这些提交会进入 cvs 存储库,你可能应该接受它。
For the git remote repo, if you do wish to proceed - I assume you know to squash the commits on your local repo ( git rebase -i
is straightforward). After the squash, push with a -f
- a force push.
对于 git 远程存储库,如果您确实希望继续 - 我假设您知道压缩本地存储库上的提交(git rebase -i
很简单)。壁球后,-f
用力推动。
回答by CharlesB
You can squash the commits with git rebase -i
or git merge --squash
, see Squash my last X commits together using Git
您可以使用git rebase -i
或压缩提交git merge --squash
,请参阅使用 Git 压缩我最近的 X 次提交
But since you have already published them to another repository you have to fix it on others. Quite cumbersome, but git push --force
is the command you need, though.
但是由于您已经将它们发布到另一个存储库,因此您必须在其他存储库上修复它。相当麻烦,但这git push --force
是您需要的命令。
It is not recommended, however: if the remote repo has already synced with CVS you'll have to fix it too... Same thing if other devs have already pulled from it.
但是,不建议这样做:如果远程存储库已经与 CVS 同步,您也必须修复它……如果其他开发人员已经从中提取,也是如此。