DEV Community

Discussion on: Squashing commits in Git!

Collapse
 
yeganathan profile image
Yeganathan S • Edited

First of all thanks for sharing your thoughts!
I would like to add a few tops of it. When we squash commits, our local version would probably be changed so git won't allow us to do push those since both remote and local are different versions. Only force push works in this case, also when we work with others only if the person pulls, the changes come to his local, and if he force pushes directly then his changes would be reflected as before squashing.

Collapse
 
recursivefaults profile image
Ryan Latta

You only need to force push when you have to overwrite a remote's history of commits. If you only rebase commits that you've not pushed, you'll find you never have to force push.

Give it a try. Make a bunch of commits without pushing them. Do your interactive rebase on ONLY those commits. You'll have no problem pushing because you'd be pushing 1 squash commit. The remote repo never knew or cared you have others before the rebase. When you rebased you rewrote that history to never exist. When you push it looks like you only ever had 1 commit.

For fun clone the repo somewhere else. and do it again only this time do a few commits, and push them one at a time. Somewhere in the middle of pushing pull to your 2nd repo. Add a few commits in that 2nd repo. Go back to your original and squash those commits and force push. Go to your 2nd repo and do a pull. See what happens.

Knowing how this works will be a pretty important thing. Someone you work with will mess up a repo at some point. Good to know how it looks and how to fix it. Its a super power.