Git Handbook

02 · Các lệnh Git hằng ngày

Nhóm command bạn sẽ sử dụng gần như mỗi ngày: status, diff, add, commit, log, show, restore, rm và mv.

1. Kiểm tra trạng thái và thay đổi

git-status

Cho biết branch hiện tại và trạng thái tracked/untracked/staged.
Official docs ↗
Cú pháp
git status [-s|-sb]
Hiểu đơn giản
Đây là “dashboard” an toàn nhất trước và sau hầu hết thao tác.
git status
git status -sb
-s = short; -b thêm branch/tracking info.

git-diff

So sánh nội dung giữa working tree, index, commits hoặc branches.
Official docs ↗
Cú pháp
git diff [<commit> [<commit>]] [-- <path>]
Hiểu đơn giản
Không đối số: xem thay đổi chưa stage. --staged: xem snapshot đã stage so với HEAD.
git diff
git diff --staged
git diff main..feature/login
git diff HEAD~1 HEAD -- src/

git-log

Hiển thị lịch sử commit.
Official docs ↗
Cú pháp
git log [options]
Hiểu đơn giản
Có thể filter, format, graph và giới hạn phạm vi commit.
git log --oneline
git log --oneline --graph --decorate --all
git log --author="Thinh" --since="2 weeks ago"
git log -- src/auth.js

git-show

Hiển thị object/commit cụ thể.
Official docs ↗
Cú pháp
git show [<object>]
Hiểu đơn giản
Thường dùng để đọc metadata + patch của một commit hoặc lấy nội dung file tại commit.
git show HEAD
git show abc123
git show HEAD~2:src/app.js

2. Staging Area

git-add

Đưa nội dung thay đổi vào index cho commit kế tiếp.
Official docs ↗
Cú pháp
git add <path> | git add -p
Hiểu đơn giản
git add stage nội dung, không chỉ “đánh dấu filename”. Nếu file thay đổi tiếp sau khi add, phần mới chưa tự được stage.
git add README.md
git add src/
git add .
git add -p src/app.js
Ưu tiên git add -p khi một file chứa nhiều thay đổi thuộc các mục đích khác nhau.

git-restore

Khôi phục file hoặc unstage thay đổi.
Official docs ↗
Cú pháp
git restore [--staged] <path>
Hiểu đơn giản
Không --staged: cập nhật working tree từ source mặc định. Có --staged: đưa index về HEAD nhưng giữ working changes.
git restore app.js
git restore --staged app.js
git restore --source=HEAD~1 -- app.js
Lệnh git restore app.js có thể làm mất thay đổi chưa commit trong file.

3. Tạo commit

git-commit

Tạo commit mới từ nội dung trong index.
Official docs ↗
Cú pháp
git commit [-m <message>]
Hiểu đơn giản
Commit ghi snapshot đã stage cùng metadata và parent reference.
git commit -m "feat(auth): add login validation"
git commit -am "fix: handle null user"
git commit --amend --no-edit
-a chỉ tự stage file đã tracked; file mới vẫn cần git add.

Commit message gợi ý

feat(auth): add password reset
fix(order): prevent duplicate checkout
docs: explain local setup
refactor(payment): extract gateway adapter
test(user): cover duplicate email case

4. Quản lý file tracked

git-rm

Xóa file khỏi working tree và index, hoặc chỉ bỏ tracking.
Official docs ↗
Cú pháp
git rm [--cached] <path>
Hiểu đơn giản
--cached rất hữu ích khi file như .env đã bị track nhưng bạn muốn giữ local.
git rm old.txt
git rm --cached .env

git-mv

Di chuyển/đổi tên file và stage thay đổi tương ứng.
Official docs ↗
Cú pháp
git mv <old> <new>
Hiểu đơn giản
Convenience command cho rename/move.
git mv docs/old.md docs/new.md

.gitignore

.gitignore chỉ ảnh hưởng file chưa tracked. Nếu file đã được commit, hãy bỏ tracking trước.

# Node
node_modules/
dist/
.env
*.log

# PHP
/vendor/
/storage/logs/*

Official gitignore docs ↗

Workflow commit khuyên dùng

git status
git diff
git add -p
git diff --staged
git commit -m "feat: ..."
git status