Simplifying Git Workflow: How I Merged Multiple Commits Using --squash

How I used git merge --squash to combine the changes from a feature branch into one commit on another branch.

Git · Workflow

Updated:

Originally published on Medium

Illustration of a developer working with Git branches

As a Frontend Developer with 3+ years of experience working on enterprise-level React applications, I often find myself in situations where I need to manage multiple commits across different branches. Today, I want to share a useful Git trick I learned that saved me hours of work.

The Problem

Recently, while working on a feature branch that had accumulated about 10 commits, I needed to merge these changes into another branch. I could have cherry-picked each commit individually, but that meant replaying all ten.

# The old way (painful with multiple commits)
git cherry-pick commit1
git cherry-pick commit2
git cherry-pick commit3
# ... and so on

The Solution: git merge --squash

I discovered a much more elegant solution using the --squash option:

# Step 1: Switch to your target branch
git checkout target-branch

# Step 2: Perform the squash merge
git merge --squash feature/your-branch-name

# Step 3: Commit the changes
git commit -m "feat: merged all changes from feature branch"

# Step 4: Push the changes
git push --set-upstream origin target-branch

The squash stages the combined changes. The commit you create has one parent, so Git does not record the feature branch as merged.

Why This Works Better

  1. Time Efficient: Instead of managing multiple commits, you handle everything in one go
  2. Clean History: Your main branch stays clean with a single, meaningful commit
  3. Easy to Review: Code reviewers can see all changes in one consolidated commit
  4. Conflict Resolution: Resolve conflicts during the squash instead of replaying each commit.

Real-World Example

In my recent project, I had a feature branch with multiple commits for adding language and country IDs to our system:

git checkout development
git merge --squash feature/adding-language-and-country-ids
git commit -m "feat: implemented language and country ID system"

A Few Tips

  • Always write clear commit messages
  • Test your changes after the squash merge
  • Use this technique mainly for feature branches
  • Keep your main/development branch clean

When to Use This Approach?

  • ✅ When merging completed features
  • ✅ When consolidating experimental work
  • ✅ During code cleanup sprints
  • ✅ When preparing for production releases

When Not to Use?

  • ❌ When commit history details are important
  • ❌ When working on shared feature branches
  • ❌ During collaborative debugging sessions

Conclusion

As a frontend developer, keeping your Git history clean while efficiently managing code changes is crucial. The git merge --squash command has become an essential tool in my daily workflow, especially when working with feature branches that have multiple commits.

If you found this article helpful, feel free to follow me for more frontend development tips and tricks!

References

  1. git merge documentation

Written by:

Full Stack Developer working with TypeScript, React and Node.js. Web development since 2019, across product interfaces, APIs and analytics.

A bit about me