How to squash commits in git after they have been pushed?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Squashing commits in Git is a process used to combine multiple commit entries into a single commit. This can be useful for cleaning up your commit history, simplifying complicated commit chains, or making a series of experimental changes more concise before merging them into a main branch. However, squashing commits that have already been pushed to a public or shared repository requires careful handling to avoid disrupting other collaborators. Here, we provide a detailed guide on how to safely squash commits in Git after they have been pushed.
Step-by-Step Guide to Squash Commits After Pushing
To squash commits that have been pushed, follow these steps. Note that this process involves rewriting history, which can cause issues for anyone else who has pulled the original commits. Therefore, it's crucial to coordinate with your team.
1. Ensure Your Branch is up to Date
Make sure your branch is synced with the remote repository.
2. Use git rebase
You can start an interactive rebase for the last n commits on your current branch. Replace n with the number of commits you want to squash.
This command opens an editor displaying a list of the last n commits in your current branch, looking something like this:
3. Squash Commits
Within the editor, you can choose which commits to squash into the first one by replacing the word pick with squash or s beside the commits you intend to combine. It should look something like:
Save and close the editor. Git will then give you the opportunity to combine the commit messages or replace them.
4. Force Push Your Changes
Since your local branch now has a history that diverges from the remote branch, you'll need to force push your changes.
Best Practices and Considerations
- Notify Your Team: Before rewriting the public history, inform your teammates. They'll need to handle the changes on their local clones.
- Backup Before Rebasing: It’s a good idea to create a backup branch before you start the rebase, in case something goes wrong.
- Use Force Push With Caution: Avoid using
git push --forcein a collaborative environment unless necessary. It can overwrite others' work.
Summary Table
| Step | Command | Description |
| Update branch | git pull origin your-branch-name | Syncs local branch with remote repository. |
| Start interactive rebase | git rebase -i HEAD~n | Opens rebase editor for last n commits. |
| Select commits to squash | Edit to squash instead of pick | Marks commits to be combined. |
| Force push | git push origin your-branch-name --force | Updates remote repository with new history. |
Closing Thoughts
Squashing pushed commits should be done with caution, especially in a shared repository. Always communicate with your team when performing operations that alter the history of your repository. By following the steps outlined in this guide and adhering to best practices, you can maintain a clean and comprehensible history in your Git projects.

