Bỏ qua để đến nội dung

Commit

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)
Working Directory → Staging Area → Repository
(add) (commit)
Terminal window
git status
Terminal window
# 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 *.js
Terminal window
git commit -m "Mô tả thay đổi"
<type>: <subject>
<body> (tùy chọ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…)
Terminal window
# 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"
Terminal window
# Chỉ với file đã tracked
git commit -am "message"
Terminal window
# 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-edit
Terminal window
# 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 -5
* a1b2c3d (HEAD -> main) feat: add user profile
* e4f5g6h fix: resolve login bug
* i7j8k9l docs: update README
* m0n1o2p Initial commit
Terminal window
# Xem thay đổi trong commit
git show a1b2c3d
# Xem diff giữa 2 commits
git diff commit1 commit2

Học về Branch để làm việc song song trên nhiều tính năng.