Git rebase interactive squash allows you to combine multiple commits into a single commit, making your commit history cleaner and more manageable.
git rebase -i HEAD~n # Replace 'n' with the number of commits you want to squash
In the interactive interface that appears, change `pick` to `squash` (or `s`) for the commits you want to squash into the previous commit.
Understanding Git Rebase and Squash
What is Git Rebase?
Git rebase is a powerful command used in version control that allows developers to integrate changes from one branch into another. The primary purpose of rebase is to create a linear history by moving or combining a sequence of commits to a new base commit. Unlike a merge, which preserves the commit history of both branches, rebase streamlines the commit log, eliminating complex branch merging points.
When contemplating whether to use rebase or merge, consider that rebase is generally more beneficial for local branches, particularly for feature branches that will eventually be pushed to a shared repository. Using rebase helps maintain a cleaner project history and reduces the likelihood of conflicts later on.
The Basics of Git Squash
Squashing commits refers to the process of combining multiple commits into a single commit. The benefits of squash become particularly apparent when you want to keep your commit history concise and meaningful. A messy commit history can make it difficult for collaborators to understand the evolution of a project, which is why squashing is a popular practice in many teams.
Squashing differs from merging in that it compacts changes, reducing the number of commits associated with a feature or bug fix. This can enhance the clarity of the project’s history, as it groups related changes together under one commit message.

The Importance of `git rebase -i`
What Does `-i` Stand For?
The `-i` in `git rebase -i` stands for interactive mode. This mode allows you to view a list of recent commits and choose how to proceed with them. Interactive rebase is especially useful for squashing commits, as it gives you granular control over how multiple commits can be altered, combined, or modified during the rebase process.
Setting Up for Interactive Rebase
Before you can begin an interactive rebase, ensure you are working within a Git repository. The basic syntax for the command is:
git rebase -i HEAD~<number_of_commits>
In this command, replace `<number_of_commits>` with the number of prior commits you wish to include in the rebase. For example, if you want to squash the last three commits, you would use `HEAD~3`.
Initiating an Interactive Rebase
To start an interactive rebase, execute:
git rebase -i HEAD~3
This will open your default text editor and display the last three commits, listed in chronological order.

Step-by-Step: Squashing Commits
Overview of the Interactive Rebase Process
When you initiate an interactive rebase, a list of your commits will display, allowing you to choose how to handle each one. This is where the real power of interactive rebase comes into play.
Modifying the Commit List
In the editor, you'll see a list that resembles this:
pick 12345a1 First commit
pick 67890bc Second commit
pick 13579de Third commit
To squash the second and third commits into the first, change `pick` to `squash` (or just `s`) for each of those commits, like so:
pick 12345a1 First commit
squash 67890bc Second commit
squash 13579de Third commit
Saving and Exiting the Editor
After making your changes, save the file and exit your editor. Git will then proceed to combine the specified commits. You may be prompted to edit the commit message for the resulting squashed commit.

Resolving Conflicts During Rebase
What Are Merge Conflicts?
While squashing commits, conflicts can arise if changes from the commits you are squashing overlap. Merge conflicts happen when Git cannot automatically resolve differences between two branches.
How to Resolve Conflicts
If you encounter a conflict during the rebase, Git will pause and prompt you to resolve the conflicts. Use the following command to check the status of your rebase:
git status
This will show which files are conflicting. Resolve the conflict in your preferred text editor, stage the resolved files, and then continue with:
git add <resolved-file>
git rebase --continue
Repeat this process until all conflicts are resolved and rebase is complete.

Finalizing Your Squashed Commits
Review the Squashed Commits
After successfully completing the rebase, review the squashed commits using:
git log --oneline
This will display your commit history in a streamlined format, reflecting the changes made during the interactive rebase.
Pushing Changes to a Remote Repository
To synchronize your local changes with the remote repository, a force push is often necessary after a rebase due to the rewritten history. Use the following command to push your changes:
git push origin branch-name --force
Note: Use force-pushing with caution, especially if others are collaborating on the same branch, as it can overwrite shared history.

Best Practices for Using `git rebase -i` and Squashing
When to Rebase and Squash
Understanding when to leverage rebase and squash is crucial. It’s best to use these commands on local branches or feature branches before they are shared with a larger team, ensuring your commits remain organized and easy to follow.
Avoiding Common Pitfalls
Always keep a backup branch before performing an interactive rebase. Mistakes during the rebase can lead to lost commits, and having a backup allows you to recover your work easily.

Conclusion
Recap on the Benefits of Git Rebase and Squash
In summary, `git rebase -i squash` is an invaluable technique for maintaining a clean commit history, enhancing the organization of your Git repository. The ability to squash commits and reshape your project's history promotes clearer collaboration and a more manageable codebase. With practice, these commands will become essential tools in your version control workflow.
Further Reading and Resources
For anyone looking to deepen their understanding of Git, many resources are available, including official documentation, online tutorials, and Git-focused courses. Exploring these materials will enhance your proficiency and confidence in using Git effectively.