Interactive rebase (git rebase -i) cho phép edit lịch sử commit local trước khi share.
Workflow chuẩn trước merge PR:
git rebase -i HEAD~5 # edit 5 commits gần nhất
# hoặc
git rebase -i origin/main # tất cả commits chưa mergeTrong editor hiện ra:
pick abc123 feat: add user model
pick def456 fix typo
pick ghi789 wip: half done
pick jkl012 fix tests
pick mno345 final cleanupThay đổi:
- squash (s): merge commit này vào trước, giữ message
- fixup (f): merge vào trước, BỎ message (dùng cho "fix typo", "wip")
- reword (r): giữ commit, chỉ edit message
- drop (d): xóa commit hoàn toàn
- edit (e): dừng lại tại commit để amend
Ví dụ kết quả tốt: 5 WIP commits → 1 clean commit feat(auth): implement JWT login with refresh tokens
Lưu ý quan trọng: CHỈ rebase commits chưa push lên remote (hoặc trên branch riêng của bạn). Rebase shared commits = disaster cho teammates.
Interactive rebase (git rebase -i) lets you edit local commit history before sharing it.
Standard workflow before merging a PR:
git rebase -i HEAD~5 # edit last 5 commits
# or
git rebase -i origin/main # all unmerged commitsThe editor shows:
pick abc123 feat: add user model
pick def456 fix typo
pick ghi789 wip: half done
pick jkl012 fix tests
pick mno345 final cleanupCommands:
- squash (s): merge into previous commit, keep message
- fixup (f): merge into previous commit, DISCARD message (for "fix typo", "wip")
- reword (r): keep commit, only edit the message
- drop (d): delete commit entirely
- edit (e): pause at this commit to amend it
Good outcome example: 5 WIP commits → 1 clean commit feat(auth): implement JWT login with refresh tokens
Critical warning: ONLY rebase commits that have NOT been pushed to remote (or are on your own branch). Rebasing shared commits is a disaster for teammates.