git push --force: overwrite remote không kiểm tra gì — nếu teammate đã push commits mới lên remote mà bạn chưa fetch, bạn sẽ overwrite và mất work của họ.
git push --force-with-lease: kiểm tra remote ref trước khi overwrite — chỉ force push nếu remote ref khớp với lần fetch cuối của bạn. Nếu ai đó đã push thêm → lệnh fail, bạn được nhắc fetch trước.
# An toàn:
git push --force-with-lease origin feature/my-branch
# Error nếu remote đã thay đổi:
# ! [rejected] feature/my-branch -> feature/my-branch (stale info)
# Sau đó:
git fetch origin
git rebase origin/feature/my-branch
git push --force-with-lease origin feature/my-branchEnforce trong team:
# .gitconfig alias:
git config --global alias.pushf "push --force-with-lease"
# Sau đó dùng: git pushfHoặc dùng git alias để enforce convention:
# .gitconfig alias (thực tế và đơn giản hơn hook):
git config --global alias.pushf "push --force-with-lease"
# Dùng: git pushf origin feature/branchLưu ý: không thể dùng git push --dry-run --force để detect --force trong pre-push hook vì git không output chuỗi "Would force push" — muốn block phải dùng git alias wrapper thay vì hook parsing.
Cẩn thận: --force-with-lease vẫn không an toàn nếu bạn vừa git fetch (remote ref match nhưng có thể còn work của người khác chưa pushed).