Commit
Commit là gì?
Commit là một “snapshot” (ảnh chụp) của code tại một thời điểm. Mỗi commit bao gồm:
- Các thay đổi được lưu
- Thông điệp mô tả
- Tác giả và thời gian
- Mã định danh duy nhất (hash)
Quy trình Commit
Working Directory → Staging Area → Repository
(add) (commit)Bước 1: Xem thay đổi
git statusBước 2: Thêm vào Staging
# Thêm một file
git add filename.txt
# Thêm nhiều file
git add file1.txt file2.txt
# Thêm tất cả file thay đổi
git add .
# Thêm theo pattern
git add *.jsBước 3: Commit
git commit -m "Mô tả thay đổi"Viết Commit Message tốt
Format chuẩn
<type>: <subject>
<body> (tùy chọn)Các type phổ biến
| Type | Ý nghĩa |
|---|---|
feat | Tính năng mới |
fix | Sửa bug |
docs | Cập nhật tài liệu |
style | Format code (không đổi logic) |
refactor | Refactor code |
test | Thêm/sửa test |
chore | Công việc khác (build, config…) |
Ví dụ
# Tốt ✅
git commit -m "feat: add user login feature"
git commit -m "fix: resolve null pointer in payment"
git commit -m "docs: update API documentation"
# Không tốt ❌
git commit -m "fix"
git commit -m "update"
git commit -m "asdfgh"Commit nhanh
Add + Commit cùng lúc
# Chỉ với file đã tracked
git commit -am "message"Sửa commit cuối
# Sửa message
git commit --amend -m "message mới"
# Thêm file vào commit cuối
git add forgotten-file.txt
git commit --amend --no-editXem lịch sử Commit
# Xem tất cả commits
git log
# Xem ngắn gọn
git log --oneline
# Xem với graph
git log --oneline --graph --all
# Xem n commits gần nhất
git log -5Ví dụ output
* a1b2c3d (HEAD -> main) feat: add user profile
* e4f5g6h fix: resolve login bug
* i7j8k9l docs: update README
* m0n1o2p Initial commitXem chi tiết Commit
# Xem thay đổi trong commit
git show a1b2c3d
# Xem diff giữa 2 commits
git diff commit1 commit2Tiếp theo
Học về Branch để làm việc song song trên nhiều tính năng.
Last updated on