git
commit squashing
version control
git push
git rebase

How to squash commits in git after they have been pushed?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Overview

When working with Git, there are times when you may need to squash commits that have already been pushed to a remote repository. Squashing commits helps you to combine multiple commit entries into a single cohesive commit. This is particularly useful for cleaning up your commit history before merging branches or when you realize that a sequence of commits should have logically been a single change.

Understanding the Implications

Squashing commits that have been pushed to a remote repository requires rewriting the commit history. This involves the use of Git's interactive rebase feature. Before proceeding, it's crucial to understand that rewriting history can affect other collaborators. For branches that are shared, you should communicate with your team, as they will need to synchronize their local repositories.

Pre-requisites

  • Git Installed: Ensure that Git is installed and properly configured on your machine.
  • Back up Current State: It's a good idea to back up or create a clone of your branch as a precaution.

Squashing Commits Using Interactive Rebase

Step-by-Step Guide

  1. Identify the Range of Commits to Squash
    Determine how many commits back you want to squash. For instance, if you wish to squash the last three commits:
bash
   git log --oneline

This command displays the commit history. Note the hash of the commit you want to rebase onto.

  1. Start an Interactive Rebase Session
    Use the HEAD~n notation if squashing the last few commits. Replace n with the number of commits:
bash
   git rebase -i HEAD~3
  1. Interactive Rebase Interface
    On running the above command, an editor will open displaying a list of commits:
 
   pick <commit_hash_1> Commit message 1
   pick <commit_hash_2> Commit message 2
   pick <commit_hash_3> Commit message 3
  1. Modify the Commands
    Change the word pick to squash (or s) for the commits that you want to combine:
 
   pick <commit_hash_1> Commit message 1
   squash <commit_hash_2> Commit message 2
   squash <commit_hash_3> Commit message 3

Only the commit with the pick label will remain unchanged; the others will be combined with it.

  1. Modify Commit Message
    After closing the editor, Git will open another editor where you can write your combined commit message. Modify it as appropriate and save it.
  2. Force Push Changes
    Since you've rewritten history, a force push is required to update the remote repository:
bash
   git push origin branch-name --force

Ensure you replace branch-name with your actual branch name.

Key Considerations

  • Communication: Once done, inform other developers working on the branch so they can pull the updated history.
  • Conflict Handling: If conflicts arise during the rebase, Git will pause allowing you to resolve them.
  • Non-Master Branches: It's less risky to perform these actions on non-master branches that are not frequently shared.

Pros and Cons of Squashing

ProsCons
Simplifies commit history by combining trivial commitsCan disrupt team workflow if not communicated
Easier to review code changes in a single commitForce-pushing may inadvertently overwrite others' work
Better historical record of logical changesHistory rewriting can lead to complications

Alternative Approaches

  1. Merge Commits: If squashing isn't suitable, consider using Git's merge functionality, which preserves all commits but groups them into a single merge commit.
  2. Use git reset: If not yet pushed, git reset followed by a regular commit can entirely avoid rewriting history.
  3. Temporary Branching: Create a new branch from where you want a clean history, apply changes, and push. Then reintegrate with the main branch.

Conclusion

Squashing commits in Git after they've been pushed involves rewriting history and thus requires careful consideration, particularly when working in a team environment. By following the steps in this guide and taking the necessary precautions, you can effectively manage your commit history to maintain clarity and coherence in your project. Always remember to communicate with team members when making changes that involve force-pushing.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.