Nâng CaoGit iconGit

Feature branch của bạn đã sống được 2 tuần và bây giờ cách main 50 commits. Chiến lược nào để merge mà không gây thảm họa?

Branch sống lâu là nợ kỹ thuật về integration. Xử lý từng bước:

1. Trước khi làm bất cứ điều gì — tạo backup:

bash
git branch backup/feature-before-rebase

**2.

Rebase từng phần (incremental rebase) thay vì một lần:**

bash
git fetch origin
git rebase origin/main

Nếu conflict quá nhiều, rebase tới mid-point commit của main trước. (Lưu ý: --onto yêu cầu 3 tham số: git rebase --onto <newbase> <upstream> [<branch>] — không dùng redundant ở đây.)

3. Resolve conflicts theo từng commitgit rebase --continue sau mỗi conflict. Dùng git mergetool (vimdiff, VSCode, IntelliJ) để visual diff.

4. Sau rebase — chạy full test suite trước khi push.

5. Force push (với lease) lên feature branch:

bash
git push --force-with-lease origin feature/my-branch

Phòng tránh lần sau: rebase lên main mỗi ngày (hoặc dùng git fetch && git rebase origin/main), chia feature lớn thành nhiều PR nhỏ, dùng feature flags để merge code chưa hoàn thiện vào main sớm.

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

Mở danh sách Git