git-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 (
    /git-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. 确定源分支 —— 如果传入了参数(如
    /git-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
拉取最新代码。如果快进拉取失败,终止操作——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 git-commit

委托git-commit处理提交

Invoke
/git-commit
to handle the commit. Do not write commit messages directly.
调用
/git-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
    /git-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
    参数 —— 禁止使用快进合并或普通合并。
  • 始终将提交逻辑委托给
    /git-commit
    处理。
  • 出现合并冲突时终止操作 —— 禁止自动解决冲突。
  • 禁止强制推送。
  • 优先使用
    git branch -d
    删除分支 —— 只有验证无差异后才能使用
    -D
    参数。
  • 任何步骤失败都终止流程并上报错误。

Quick Reference

快速参考

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