Amend last commit — chỉ dùng khi commit CHƯA push:
bash
# Chỉ sửa message:
git commit --amend -m "feat: correct message here"
# Thêm file bị quên:
git add forgotten-file.ts
git commit --amend --no-edit # giữ nguyên message
# Sửa cả file lẫn message:
git add forgotten-file.ts
git commit --amend -m "feat: complete implementation with tests"Nếu đã push (chỉ branch của riêng bạn):
bash
git commit --amend -m "corrected message"
git push --force-with-lease origin feature/my-branchLưu ý quan trọng:
- --amend tạo commit MỚI với hash khác — không phải edit in-place
- KHÔNG amend commits trên main/develop hoặc bất kỳ shared branch
- --force-with-lease an toàn hơn --force — sẽ fail nếu remote có commits mới mà local chưa có (tránh overwrite người khác)
- Nếu cần sửa commit cũ hơn (không phải last): dùng git rebase -i