Skip to content

Git merge & squash

To merge a branch to a parent and squash all the commits into a single commit in Git, you can use the git merge --squash command. This approach is beneficial for maintaining a clean and linear project history.

Using git merge --squash

This is often the most straightforward method for this task. It takes all the changes from a feature branch, squashes them into a single set of changes, and stages them in your working directory. You then commit these changes as a single commit.

Here are the steps:

  1. Checkout the parent branch: Make sure you are on the branch you want to merge into (e.g., main or develop).
    bash git checkout main

  2. Run the squash merge command: Use git merge --squash followed by the name of the branch you want to merge.
    bash git merge --squash feature-branch
    This command takes all the commits from feature-branch and applies their changes to your current working directory and index. It does not create a merge commit automatically.

  3. Review the changes (optional): You can check the staged changes to ensure everything is correct.
    bash git status

  4. Commit the changes: Create a single commit that consolidates all the changes from the feature branch. It’s good practice to write a clear and descriptive commit message that summarizes the changes.
    bash git commit -m "feat: Implement the new feature"

Page last modified: 2026-01-26 09:31:10