Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GIT: how to squash several commits that have been pushed to a remote repo?

Tags:

git

I have a weird setup with Git. Basically I have:

[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 2] is a remote server for building.

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].

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.

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.

like image 744
wei Avatar asked Oct 15 '11 00:10

wei


People also ask

How could you squash multiple commits together without using?

You can do this fairly easily without git rebase or git merge --squash . In this example, we'll squash the last 3 commits. Both of those methods squash the last three commits into a single new commit in the same way. The soft reset just re-points HEAD to the last commit that you do not want to squash.

How do you squash commits after pushing in bitbucket?

Squash your commits in Bitbucket Cloud You could always squash commits via the command line using “git merge –squash”, but this is just another time consuming step, and it would be a lot easier to have it done for you from Bitbucket.


2 Answers

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.

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.

like image 193
manojlds Avatar answered Sep 21 '22 02:09

manojlds


You can squash the commits with git rebase -i or git merge --squash, see Squash my last X commits together using Git

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.

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.

like image 33
CharlesB Avatar answered Sep 20 '22 02:09

CharlesB