Trung BìnhGit iconGit

Interactive rebase: làm sao dùng squash, fixup, reword, drop để clean up history trước khi merge PR?

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:

bash
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 merge

Trong 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 cleanup

Thay đổ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.

Xem toàn bộ Git cùng filter theo level & chủ đề con.

Mở danh sách Git