跳到主要内容

Squash Merge

Squash merge is a merging strategy that compresses (squashes) all commits in a Pull Request into a single commit, which is then applied to the target branch. This helps keep the project's commit history clean without becoming cluttered with numerous small commits.

Typically, squash merge is used when integrating a feature or fix into the main branch to maintain the coherence of the commit history.

Applicable Scenarios

  1. When a Pull Request contains multiple intermediate commits that do not have independent significance in the target branch.
  2. The team wishes to maintain a tidy commit record for easier code review and subsequent maintenance.
  3. The associated functionality in the Pull Request is small, and all changes can naturally be summarized in a single commit.

Using Squash Merge


The basic steps for using squash merge are as follows:

  1. Ensure Squash Merge is Enabled:

    • On the project details page, click "Project Settings" -> "Pull Requests settings", and enable "Merge via merge commit" under "Pull Requests - Merge Mode".

    image-20241231202754512

  2. Accept Squash Merge:

    • If you require a squash merge, the PR creator must select "Squash commits when merging this pull request" when creating the PR.

    image-20250102102251690

  3. Enable Squash Merge Option:

    • Below the "Discussions" tab in the PR details page, you will see a checkbox for Squash Merge.

    • Provide a clear description for the merge commit so other developers understand the purpose and changes being made.

    image-20250102102822547

  4. Complete the Merge:

    • Click the "Merge" button, and GitCode will combine multiple commits into a new single commit.
  • The squash merge feature better manages commit history, making version control and code review clearer and more organized.

Precautions

  1. Original Commit Records Are Lost

    • After a squash merge, all original commits from the Pull Request will be compressed into a single commit, making it impossible to view them separately.
  2. Not Suitable for Large Feature Development

    • If a Pull Request involves multiple independent features, it is recommended to keep separate commit records rather than use squash.
  3. Consistency in Collaborative Development

    • The team should clearly define when to use squash merge to avoid inconsistent styles.