git
merge
squash
rebase
version control

What is the difference between merge --squash and rebase?

Interview Questions practice on Codemia

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

Browse interview questions

Understanding Git: Differences Between `merge --squash` and `rebase`

When collaborating on code, it’s crucial to maintain a clean and understandable project history. Git provides a plethora of tools to manage the changes made to a repository, two of which are `merge --squash` and `rebase`. While they might seem similar at first glance, they serve different purposes and produce different outcomes. Below, we dive into the technical distinctions between `merge --squash` and `rebase`, complemented with examples for clarity.

Git Basics

Before exploring the differences, it's essential to understand some basic concepts:

  • Branching: In Git, a branch represents an independent line of development. Branches allow you to develop features, fix bugs, or experiment in isolation.
  • Commits: Each commit in Git records a snapshot of all the files in your project at a given point in time.
  • Merging: Merging is the process of integrating changes from one branch into another.

`merge --squash`

The `merge --squash` command in Git is used to combine changes from one branch with another, resulting in a single new commit on the target branch. It does not create a merge commit, and the source branch's history is not retained in the target branch.

Characteristics:

  • Squashed Commits: All commits from the source branch are combined into a single commit in the target branch.
  • No Merge Commit: Unlike a normal merge, it doesn’t create a merge commit.
  • History Alteration: The historical commits from the source branch are not visible after the merge.

Example:

Imagine you have a feature branch called `feature-login` with three commits. To integrate this branch into the `main` branch using `merge --squash`, you’d perform the following steps:

  • Linear History: Incorporates all commits from the source branch in sequence after the current branch’s commits.
  • Interactive Options: Allows editing, reordering, or squashing of commits during the rebase process.
  • History Rewriting: Modifies the commit history by creating new commits.
    • Use `merge --squash` when you want a simple history in the target branch and the branch’s internal history isn’t crucial.
    • Use `rebase` for cleaner, linear history, especially useful when preparing a feature branch for integration after updates to the main branch.
    • When working in a team, it's critical to communicate about when and how to use these commands to prevent potential data loss and ensure a clear history.
    • While `merge --squash` doesn’t alter the originally shared history, `rebase` can rewrite commit history. Ensure shared changes aren’t rewritten without consensus.

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.