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):
# 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):
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):
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.
Scenario: you have commits ABC on a branch, teammate force pushes → remote history diverged, your commits are "lost" on remote.
Recovery (if you have not pulled yet):
# Your commits are still local
git log --oneline # you can see your commits
# Remote was overwritten, but local is untouched
git push --force-with-lease origin feature/shared
# EXPECTED: will fail because remote has new commits (teammate's) — this is correct safety behavior
# The failure is NOT a problem with the recovery; --force-with-lease is protecting you from overwritingCorrect process (after --force-with-lease fails):
git fetch origin
git rebase origin/feature/shared # replay your commits on top of teammate's
# Resolve conflicts if any
git push origin feature/sharedIf you already pulled (local overwritten):
git reflog # find the commit before the pull
# Example: abc123 HEAD@{3}: commit: feat: my work
git cherry-pick abc123 # recover the commitIf teammate force pushed to main: do not force push back (will lose their work). Coordinate with team, use git cherry-pick to re-apply lost commits onto the new main.
Prevention: enforce --force-with-lease usage and branch protection rules for shared branches.