Cơ BảnGit iconGit

Vừa commit nhầm (message sai hoặc quên file), làm sao sửa mà không tạo commit mới?

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-branch

Lư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

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

Mở danh sách Git