Tình huống: bạn có commits ABC trên branch, teammate force push → remote history khác, commits của bạn "mất" trên remote.
Recovery (nếu bạn chưa pull):
bash
# Commits của bạn vẫn còn local
git log --oneline # thấy commits của bạn
# Remote đã overwrite, nhưng local chưa bị xóa
git push --force-with-lease origin feature/shared
# EXPECTED: sẽ fail vì remote có commits mới (của teammate) — đây là safety behavior đúng đắn
# Không phải lỗi của recovery process mà là --force-with-lease bảo vệ bạn khỏi overwriteĐúng quy trình (sau khi --force-with-lease fail):
bash
git fetch origin
git rebase origin/feature/shared # replay your commits on top of teammate's
# Resolve conflicts nếu có
git push origin feature/sharedNếu bạn đã pull (local bị overwrite):
bash
git reflog # tìm commit trước khi pull
# Ví dụ: abc123 HEAD@{3}: commit: feat: my work
git cherry-pick abc123 # recover commitNếu teammate force push tới main: không recover bằng force push ngược lại (sẽ mất work của teammate). Coordinate với team, dùng git cherry-pick để apply lại commits bị mất lên main mới.
Phòng tránh: enable --force-with-lease và branch protection cho shared branches.