Trung BìnhGit iconGit

Khi nào nên squash merge PR thay vì merge commit? Trade-offs là gì?

3 merge strategies trên GitHub:

Merge commit (--no-ff): tạo merge commit, giữ toàn bộ history của branch. git log --graph thấy branch structure. Tốt cho: feature branches quan trọng muốn preserve full context.

Squash merge: tất cả commits của PR → 1 commit trên main. History linear và clean. Xấu: mất toàn bộ individual commit history của feature.

Rebase merge: replay từng commit của PR lên main, không có merge commit. Linear history nhưng giữ commit granularity.

Nên dùng squash khi:
- PR có nhiều WIP/fixup commits không meaningful
- Team muốn main branch history = one-line-per-feature
- Semantic release dựa trên commit messages trên main

Nên dùng merge commit khi:
- Branch history có value (nhiều meaningful commits)
- Muốn git bisect hoạt động trên từng small commit
- Audit trail quan trọng

Thực tế: squash merge phổ biến nhất cho application code, merge commit cho library/SDK với semantic versioning chi tiết.

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

Mở danh sách Git