git-rebase
Rebase branch อย่างปลอดภัย — onto main, autosquash fixups, conflict flow ครบ
SKILL.mdgit-rebase/SKILL.md
GitHubGoal
Replay commits onto a new base or tidy history with a rebase, without breaking shared history
Scope
git rebase <base>(linearize),git rebase --onto, autosquash (--autosquashwithfixup!/squash!commits)- conflict resolution flow: continue/abort/skip
- does not cover interactive rebase (
-i) — use non-interactive equivalents: autosquash,--onto,git reset+ recommit
Execute
1. Safety Check
Goal: confirm the rebase is safe before starting
git status— the working tree must be clean (or/git-stashfirst)- check whether the branch has been pushed/shared — rebasing a pushed branch = rewriting public history → requires confirmation + force-push only, and notify collaborators
- record a backup ref:
git branch backup/<name>-<date>always before rebasing - run
/check-merge-conflictsto assess conflict risk if any
2. Rebase
Goal: commits are replayed onto the new base
git fetch origin
git rebase origin/main # linearize onto the latest main
git rebase --onto main <old-base> <branch> # move commits across bases
git rebase --autosquash origin/main # fold fixup!/squash! commits (requires rebase.autoSquash)
- always fetch the latest base first — rebasing onto a stale base = redoing work
- if unsure → use
--ontoin smaller segments rather than rebasing the whole branch
3. Handle Conflicts
Goal: conflicts resolved correctly, commit by commit
- conflict → fix the files,
git add,git rebase --continue - that commit is no longer needed →
git rebase --skip - everything goes wrong →
git rebase --abortalways returns to the pre-rebase state - a long rebase/many conflicts → consider merge instead (conflicts resolved once)
4. Verify
Goal: history is clean and code is not broken
git log --oneline origin/main..HEAD— commits are in order with no merge commits mixed in- run
/run-check— semantic conflicts do not show up ingit status - if the branch was pushed →
git push --force-with-leaseonly (never--force)
Rules
- never rebase commits others may have based work on (public history) — the golden rule of rebasing
- always create a backup branch before rebasing — reflog exists but an explicit backup is clearer
--force-with-leaseonly when pushing after a rebase — prevents overwriting others' work- rebase = rewrite — if a PR is under review and reviewers look at history → notify them first
- Use /resolve-errors if needed
Expected Outcome
- branch is linear onto the latest base with clean history
- no lost commits (backup ref + reflog) and it passes
/run-check