squash
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGit Squash
Git Squash
Pre-flight Checks
前置检查
Before merging, validate the environment:
- Determine source branch — use the argument if provided (), otherwise use the current branch.
/squash feature/my-branch - Verify not on main — abort if source branch is .
main - Check for uncommitted changes — . If dirty, abort and suggest committing or stashing.
git status --porcelain - Verify branch exists — .
git rev-parse --verify <branch> - Verify divergence — . If empty, abort — nothing to merge.
git log main..<branch> --oneline
合并前,先校验环境:
- 确定源分支 —— 如果传入了参数(比如)就使用传入的分支,否则使用当前分支。
/squash feature/my-branch - 校验不在main分支 —— 如果源分支是则终止流程。
main - 检查未提交的变更 —— 执行。如果工作区不干净,终止流程并建议提交或暂存变更。
git status --porcelain - 校验分支存在 —— 执行。
git rev-parse --verify <branch> - 校验分支存在差异 —— 执行。如果输出为空,说明没有可合并的内容,终止流程。
git log main..<branch> --oneline
Switch to Main
切换到Main分支
bash
git checkout mainIf remote exists, pull latest with . If ff-only fails, abort — main has diverged and needs manual resolution.
origingit pull --ff-onlybash
git checkout main如果存在远程,执行拉取最新代码。如果ff-only拉取失败,终止流程 —— main分支已出现分叉,需要手动解决。
origingit pull --ff-onlySquash Merge
Squash合并
bash
git merge --squash <branch>On conflicts: , switch back to source branch, suggest , stop.
git merge --abortgit rebase mainbash
git merge --squash <branch>出现冲突时:执行,切回源分支,建议执行,停止流程。
git merge --abortgit rebase mainDelegate to commit
提交委托
Invoke to handle the commit. Do not write commit messages directly.
/commit调用处理提交操作,不要直接编写提交信息。
/commitPost-merge Verification
合并后校验
bash
git log --oneline -5
git status
git diffConfirm: 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 fails (expected — squash merges don't preserve ancestry), verify zero diff with . If empty, force-delete with . If there IS a diff, stop — something was lost.
-dgit diff main <branch>git branch -D <branch>If a remote tracking branch exists ():
git ls-remote --heads origin <branch>bash
git push origin --delete <branch>首先尝试安全删除:
bash
git branch -d <branch>如果删除失败(属于预期情况 —— squash合并不会保留分支祖先关系),执行校验两个分支无差异。如果输出为空,执行强制删除。如果确实存在差异,停止流程 —— 说明有内容丢失。
-dgit 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 — never fast-forward or regular merge.
--squash - Always delegate the commit to .
/commit - Abort on merge conflicts — never auto-resolve.
- Never force-push.
- Prefer — use
git branch -donly after verifying zero diff.-D - If any step fails, stop and report the error.
- 无需确认即可执行后续步骤 —— 前置检查已经是安全闸门。
- 仅允许合并到分支。
main - 始终使用参数 —— 禁止快进合并或普通合并。
--squash - 始终将提交操作委托给处理。
/commit - 出现合并冲突时终止流程 —— 禁止自动解决冲突。
- 禁止强制推送。
- 优先使用删除分支 —— 仅在校验无差异后才使用
git branch -d。-D - 任何步骤失败时,停止流程并上报错误。
Quick Reference
快速参考
Pre-flight → checkout main → → → verify → cleanup (, fall back to after zero diff check, delete remote if exists).
git merge --squash/commit-d-D前置检查 → 切到main分支 → → → 校验 → 清理(先尝试,校验无差异后 fallback 到,如果存在远程分支则删除远程分支)。
git merge --squash/commit-d-D