squash

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Git Squash

Git Squash

Pre-flight Checks

前置检查

Before merging, validate the environment:
  1. Determine source branch — use the argument if provided (
    /squash feature/my-branch
    ), otherwise use the current branch.
  2. Verify not on main — abort if source branch is
    main
    .
  3. Check for uncommitted changes
    git status --porcelain
    . If dirty, abort and suggest committing or stashing.
  4. Verify branch exists
    git rev-parse --verify <branch>
    .
  5. Verify divergence
    git log main..<branch> --oneline
    . If empty, abort — nothing to merge.
合并前,先校验环境:
  1. 确定源分支 —— 如果传入了参数(比如
    /squash feature/my-branch
    )就使用传入的分支,否则使用当前分支。
  2. 校验不在main分支 —— 如果源分支是
    main
    则终止流程。
  3. 检查未提交的变更 —— 执行
    git status --porcelain
    。如果工作区不干净,终止流程并建议提交或暂存变更。
  4. 校验分支存在 —— 执行
    git rev-parse --verify <branch>
  5. 校验分支存在差异 —— 执行
    git log main..<branch> --oneline
    。如果输出为空,说明没有可合并的内容,终止流程。

Switch to Main

切换到Main分支

bash
git checkout main
If remote
origin
exists, pull latest with
git pull --ff-only
. If ff-only fails, abort — main has diverged and needs manual resolution.
bash
git checkout main
如果存在远程
origin
,执行
git pull --ff-only
拉取最新代码。如果ff-only拉取失败,终止流程 —— main分支已出现分叉,需要手动解决。

Squash Merge

Squash合并

bash
git merge --squash <branch>
On conflicts:
git merge --abort
, switch back to source branch, suggest
git rebase main
, stop.
bash
git merge --squash <branch>
出现冲突时:执行
git merge --abort
,切回源分支,建议执行
git rebase main
,停止流程。

Delegate to commit

提交委托

Invoke
/commit
to handle the commit. Do not write commit messages directly.
调用
/commit
处理提交操作,不要直接编写提交信息。

Post-merge Verification

合并后校验

bash
git log --oneline -5
git status
git diff
Confirm: clean working tree, squash commit at HEAD, no leftover staged changes.
bash
git log --oneline -5
git status
git diff
校验确认:工作区干净,HEAD处是squash合并后的提交,没有剩余的暂存变更。

Cleanup

清理

Try safe delete first:
bash
git branch -d <branch>
If
-d
fails (expected — squash merges don't preserve ancestry), verify zero diff with
git diff main <branch>
. If empty, force-delete with
git branch -D <branch>
. If there IS a diff, stop — something was lost.
If a remote tracking branch exists (
git ls-remote --heads origin <branch>
):
bash
git push origin --delete <branch>
首先尝试安全删除:
bash
git branch -d <branch>
如果
-d
删除失败(属于预期情况 —— squash合并不会保留分支祖先关系),执行
git diff main <branch>
校验两个分支无差异。如果输出为空,执行
git branch -D <branch>
强制删除。如果确实存在差异,停止流程 —— 说明有内容丢失。
如果存在对应的远程跟踪分支(执行
git ls-remote --heads origin <branch>
校验):
bash
git push origin --delete <branch>

Rules

规则

  • Proceed without confirmation — pre-flight checks are the safety gate.
  • Only merge into
    main
    .
  • Always use
    --squash
    — never fast-forward or regular merge.
  • Always delegate the commit to
    /commit
    .
  • Abort on merge conflicts — never auto-resolve.
  • Never force-push.
  • Prefer
    git branch -d
    — use
    -D
    only after verifying zero diff.
  • If any step fails, stop and report the error.
  • 无需确认即可执行后续步骤 —— 前置检查已经是安全闸门。
  • 仅允许合并到
    main
    分支。
  • 始终使用
    --squash
    参数 —— 禁止快进合并或普通合并。
  • 始终将提交操作委托给
    /commit
    处理。
  • 出现合并冲突时终止流程 —— 禁止自动解决冲突。
  • 禁止强制推送。
  • 优先使用
    git branch -d
    删除分支 —— 仅在校验无差异后才使用
    -D
  • 任何步骤失败时,停止流程并上报错误。

Quick Reference

快速参考

Pre-flight → checkout main →
git merge --squash
/commit
→ verify → cleanup (
-d
, fall back to
-D
after zero diff check, delete remote if exists).
前置检查 → 切到main分支 →
git merge --squash
/commit
→ 校验 → 清理(先尝试
-d
,校验无差异后 fallback 到
-D
,如果存在远程分支则删除远程分支)。