Merge: tạo merge commit, preserve history đầy đủ. git log --graph thấy nhánh. Non-destructive — không thay đổi existing commits.
Rebase: replay commits lên đỉnh của branch khác, tạo linear history. Rewrite commit hashes.
Golden Rule of Rebasing: KHÔNG BAO GIỜ rebase branch đã được share/push lên remote mà người khác đang dùng.
Vì sao: rebase tạo commits MỚI với hash khác. Nếu teammate đã pull branch của bạn, họ có commits với hash cũ. Khi bạn force push sau rebase, history của họ và remote diverge → họ phải resolve "fake conflicts" khi pull.
Khi dùng rebase (an toàn):
- Trên local feature branch chưa push
- git pull --rebase để sync với remote (thay vì tạo merge commit)
- Clean up commits trước khi tạo PR: git rebase -i origin/main
Khi dùng merge:
- Merge feature branch vào main (qua PR)
- Tích hợp upstream changes vào long-lived branch của team
- Khi branch đã được share
Thực tế: rebase locally, merge publicly (via PR).
Merge: creates a merge commit, preserves full history. git log --graph shows branches. Non-destructive — does not change existing commits.
Rebase: replays commits on top of another branch, creating linear history. Rewrites commit hashes.
Golden Rule of Rebasing: NEVER rebase a branch that has been shared/pushed to a remote that others are using.
Why: rebase creates NEW commits with different hashes. If a teammate has already pulled your branch, they have commits with the old hashes. When you force push after rebasing, their history and the remote diverge → they must resolve "fake conflicts" when pulling.
Safe times to rebase:
- On a local feature branch not yet pushed
- git pull --rebase to sync with remote (instead of creating a merge commit)
- Cleaning up commits before opening a PR: git rebase -i origin/main
Use merge:
- Merging a feature branch into main (via PR)
- Integrating upstream changes into a long-lived team branch
- When the branch has already been shared
In practice: rebase locally, merge publicly (via PR).