Reset (git reset --hard HEAD~1): di chuyển HEAD pointer về commit cũ, xóa commits khỏi history. KHÔNG dùng trên main/shared branch — sẽ cần force push, gây disaster cho mọi người đã pull.
Revert (git revert <commit>): tạo commit MỚI undo changes của commit cũ. An toàn cho shared branches vì không rewrite history.
# Revert 1 commit:
git revert abc123
# Revert merge commit (phải chỉ định mainline parent):
git revert -m 1 abc123 # -m 1 = giữ lại parent thứ 1 (main branch)
# Revert range commits:
git revert abc123..def456
# Revert nhiều commits nhưng chỉ 1 commit revert:
git revert --no-commit abc123 def456
git commit -m "revert: remove broken payment feature"Ví dụ thực tế: deploy feature lên production, users báo lỗi nghiêm trọng → git revert -m 1 <merge-commit> → push → deploy → feature bị undo ngay mà không ảnh hưởng history.
Khi nào dùng reset: trên local branch chưa share, hoặc clean up local staging area.
Lưu ý revert merge commit: nếu sau đó muốn re-merge feature đó, phải revert lại revert commit trước — không thể merge thẳng vì git sẽ "thấy" commits đã được merge rồi.
Reset (git reset --hard HEAD~1): moves the HEAD pointer back, removing commits from history. NEVER use on main/shared branches — it requires a force push and causes disasters for everyone who has already pulled.
Revert (git revert <commit>): creates a NEW commit that undoes the changes of the old commit. Safe for shared branches because it does not rewrite history.
# Revert 1 commit:
git revert abc123
# Revert a merge commit (must specify the mainline parent):
git revert -m 1 abc123 # -m 1 = keep first parent (the main branch)
# Revert a range of commits:
git revert abc123..def456
# Revert multiple commits but create only 1 revert commit:
git revert --no-commit abc123 def456
git commit -m "revert: remove broken payment feature"Real-world example: deploy a feature to production, users report a critical bug → git revert -m 1 <merge-commit> → push → deploy → feature is undone immediately without affecting history.
When to use reset: on a local branch you have not shared yet, or to clean up local staging.
Revert merge commit warning: if you later want to re-merge the same feature, you must first revert the revert commit — you cannot merge it directly because git will think those commits were already merged.