git-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.
/git-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
合并前先验证环境:
- 确定源分支 —— 如果传入了参数(如 )则使用参数指定的分支,否则使用当前分支。
/git-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如果存在远程,执行拉取最新代码。如果快进拉取失败,终止操作——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 git-commit
委托git-commit处理提交
Invoke to handle the commit. Do not write commit messages directly.
/git-commit调用来处理提交逻辑,不要直接编写提交信息。
/git-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 .
/git-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 - 始终将提交逻辑委托给处理。
/git-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/git-commit-d-D前置检查 → 切换到main分支 → → → 验证 → 清理(先试,验证无差异后使用,如果有远程分支则删除远程分支)。
git merge --squash/git-commit-d-D