Git Squashとは?コミットをスクワッシュするための初心者ガイド

2026年1月7日

In the world of software development, as projects expand and evolve over time, the history of changes recorded in Git can rapidly accumulate a multitude of minor, exploratory, or corrective commits. These can include everything from quick fixes to experimental tweaks, leading to a cluttered and sometimes overwhelming timeline. This is precisely where the concept of Git squash becomes invaluable. By squashing commits, you can streamline and tidy up your Git history, merging several individual commits into one cohesive and meaningful entry. This not only enhances the readability of your project’s timeline but also simplifies maintenance, collaboration, and future reference for both you and your team members.

What Is Git Squash?

At its core, Git squash refers to the methodical process of consolidating multiple separate commits into a single, unified commit. Rather than maintaining a lengthy trail of incremental changes—such as “fix a minor typo in the documentation,” “update the core logic to handle edge cases,” or “debug a persistent issue in the code”—squashing empowers you to blend these together into one polished commit that encapsulates the entire scope of a feature implementation or bug resolution.

This approach is particularly prevalent in modern development workflows, especially right before integrating a feature branch back into the primary or main branch. It helps ensure that the mainline history remains focused on significant milestones rather than every small step along the way, promoting a more professional and organized repository.

Why Should You Squash Commits?

The practice of squashing commits brings a host of advantages that can significantly improve your development process. Let’s delve deeper into some of the key benefits:

  • Cleaner commit history: A streamlined history is much easier for developers to navigate, understand, and review. Instead of sifting through dozens of fragmented entries, you get a concise overview that highlights major achievements and changes.
  • Better code reviews: During pull requests or merge requests, reviewers can concentrate on the comprehensive, final version of the changes rather than piecing together the narrative from numerous tiny commits. This reduces cognitive load and speeds up the approval process.
  • Simpler debugging: Tools like Git bisect, which help pinpoint the introduction of bugs by binary-searching through commit history, become far more efficient with fewer commits to evaluate. A condensed history means quicker identification of problematic changes.
  • Professional workflow: In team environments and open-source contributions, squashing is a standard practice. It demonstrates attention to detail and respect for collaborators by presenting work in a polished form, aligning with best practices advocated by platforms like GitHub and GitLab.

By incorporating squashing into your routine, you not only elevate the quality of your repository but also foster better team dynamics and long-term project sustainability.

When Should You Use Git Squash?

While Git squash is a versatile tool, it’s most effective in specific scenarios to avoid unintended complications. Consider employing it when:

  • You have accumulated many small commits for a single feature: If your branch is filled with iterative refinements that don’t each warrant their own entry in the history, squashing them consolidates the work into a logical unit.
  • You’re preparing a pull request: Before submitting your changes for review, squashing ensures that the proposed merge is clean and focused, making it easier for maintainers to assess and integrate.
  • You aim for a clean, logical commit history: In projects where clarity is paramount, such as educational repositories or high-stakes enterprise codebases, squashing helps maintain an intelligible narrative.

However, exercise caution: Refrain from squashing commits that have already been pushed to shared branches or repositories, as this rewrites history and can disrupt others’ work. If squashing is necessary in such cases, ensure all team members are informed and aligned to prevent conflicts or lost work.

How Git Squash Works

Git squash operates primarily through the mechanism of interactive rebase, a powerful Git command that allows you to interactively rewrite the commit history of a branch in a controlled manner. This process is done locally before any merges or pushes, ensuring safety and reversibility if needed.

The interactive rebase provides a way to edit, reorder, or combine commits without altering the shared remote history prematurely. It’s like editing a draft of your story before publishing it, allowing you to refine the plot points into a more compelling whole.

Basic Git Squash Command

To initiate a squash, you can use a command like:

git rebase -i HEAD~3

This launches an interactive session for the last three commits (adjust the number as needed), where you can specify which ones to squash.

Squashing Commits Step by Step

Let’s break down the squashing process into detailed, actionable steps to make it accessible even for newcomers:

1. Run interactive rebase:

git rebase -i HEAD~N

Replace N with the number of commits you wish to review and potentially squash. For instance, HEAD~5 targets the last five commits.

2. Edit the rebase instructions: Your default text editor (like Vim or Nano) will open, displaying a list similar to:

pick a1b2c3d First commit message here
pick e4f5g6h Second commit message
pick i7j8k9l Third commit message

pick keyword means to keep the commit as is.

Change picksquash (or simply s) for the commits you want to merge into the previous one.

3. Example edited version:

pick a1b2c3d First commit message here
squash e4f5g6h Second commit message
squash i7j8k9l Third commit message

4. Save and close the editor: Git will then proceed to combine the specified commits.

5. Edit the final commit message: Another editor window will appear, allowing you to craft a new, descriptive message that summarizes all the squashed changes. This is your opportunity to provide context, such as “Implemented user authentication feature with error handling.”

6. Complete the rebase: Save and exit again. Git will finalize the squash, and you’ll have a single commit representing the group.

If conflicts arise during this process (e.g., overlapping changes), Git will pause and prompt you to resolve them manually before continuing.

Squash vs Fixup

Within the interactive rebase, you have options beyond basic squash:

  • Squash: This merges the commit into the previous one and opens an editor to combine and edit the commit messages, preserving valuable details if needed.
  • Fixup: Similar to squash, but it automatically discards the commit message of the fixup commit, using only the message from the base commit. It’s ideal for minor corrections where the extra message adds no value, like simple typo fixes.

Choose based on whether the additional commit details are worth retaining for historical or explanatory purposes.

Git Squash vs Merge

Understanding the differences between squashing and traditional merging is crucial for selecting the right tool:

特徴SquashMerge
Commit HistoryResults in a clean, linear history with consolidated commitsPreserves the full sequence of all individual commits
最適Short-lived feature branches where the process details are irrelevantLong-lived branches or when maintaining a detailed development log
Rewrites HistoryYes, alters the commit timeline locally before pushNo, adds a new merge commit without changing existing ones

Squash is preferred for ephemeral work, while merge suits ongoing collaborations.

Common Mistakes to Avoid

Even experienced users can stumble, so watch out for these pitfalls:

  • Squashing commits already pushed to shared branches: This can cause synchronization issues for collaborators; always squash locally first.
  • Forgetting to resolve conflicts during rebase: Ignoring prompts can lead to incomplete or broken history—address them promptly.
  • Losing important commit messages: When squashing, take time to incorporate key details into the final message to avoid erasing context.
  • Over-squashing: Don’t combine unrelated changes; keep squashes thematic for better traceability.

Proactive communication with your team can mitigate many of these risks.

Git Squash Best Practices for Beginners

To get the most out of Git squash without frustration:

  • Squash commits before opening a pull request: This presents your work in its best light to reviewers.
  • Keep commit messages clear and descriptive: Use the 50/72 rule—50 characters for the summary line, wrap body at 72—for readability.
  • Use squash for features, not shared release branches: Reserve it for personal or feature-specific branches to avoid disrupting stable lines.
  • Practice on local branches first: Experiment in a safe environment, perhaps with a test repository, to build confidence.
  • Backup your branch: Before rebasing, create a backup branch (e.g., git branch backup-branch) in case something goes wrong.

Adopting these habits will help you integrate squashing seamlessly into your workflow.

結論

In summary, Git squash is a powerful and indispensable technique for maintaining a clean, professional, and efficient commit history across modern software projects. At カーマテック, we encourage development teams to adopt best practices like commit squashing to improve collaboration, simplify code reviews, and ensure long-term maintainability of repositories. By understanding when to apply Git squash, how to execute it correctly, and which common pitfalls to avoid, developers can significantly enhance their Git workflows and overall productivity. For those new to Git, mastering commit squashing marks an important step toward becoming a confident, disciplined, and team-ready developer—an essential skill in today’s fast-paced and quality-driven development environment.