jvinhit//lab

Search posts

Type to search across journal entries.

navigate open esc close

Command Line cho Developer · Part 5 - Git Commands Daily Workflow

Git CLI hằng ngày cho developer: status, diff, add -p, commit, log, branch/switch, stash, restore, rebase/pull, review trước khi push và rollback an toàn.

Đây là Phần 5. Git là command line bạn sẽ dùng nhiều nhất ngoài package manager. Biết Git CLI giúp bạn review thay đổi kỹ hơn, hiểu mình sắp commit gì, và tránh thao tác phá working tree.

Mục tiêu bài này không phải học mọi thứ về Git. Mục tiêu là có workflow hằng ngày an toàn.


Bắt đầu mọi phiên bằng status

git status --short
git status

--short cho ảnh nhanh:

 M src/lib/series.ts
?? src/content/posts/dev-cli-01-terminal-files-navigation.mdx

Ký hiệu phổ biến:

Ký hiệuNghĩa
??file mới chưa track
Mfile đã sửa, chưa stage
M file đã stage
MMfile vừa stage vừa còn sửa thêm
Dfile bị xóa

Trước khi sửa code, chạy git status --short để biết working tree đã có thay đổi của ai chưa.


Xem diff trước khi stage

git diff
git diff -- src/lib/series.ts

Xem diff đã stage:

git diff --staged

Workflow tối thiểu trước commit:

git status --short
git diff
git add -p
git diff --staged
git commit -m "Add developer command line series"

git diff giúp bạn bắt typo, file thừa, debug log, secret, hoặc thay đổi ngoài ý muốn.


Stage có chọn lọc với git add -p

git add -p

Git sẽ hỏi từng hunk:

PhímTác dụng
ystage hunk này
nbỏ qua hunk này
ssplit hunk nhỏ hơn
eedit hunk thủ công
qthoát
?help

git add -p là thói quen cực đáng tiền: một file có thể chứa cả bugfix thật và thử nghiệm tạm. Stage từng hunk giúp commit sạch hơn.


Commit tốt là commit review được

git commit -m "Add command line cookbook series"

Một commit message tốt trả lời: thay đổi gì và vì sao.

Ví dụ ổn:

Add developer command line cookbook series
Fix stale series ordering by using seriesOrder
Guard API route against missing auth token

Ví dụ yếu:

update
fix bug
changes

Nếu commit có nhiều ý không liên quan, tách ra. Git history tốt là công cụ debugging tương lai.


Log và tìm commit

git log --oneline --decorate --graph -20
git log --stat -5
git show HEAD
git show HEAD -- src/lib/series.ts

Tìm commit đụng một file:

git log --follow -- src/lib/series.ts

Tìm commit có text trong diff:

git log -S "seriesOrder" -- src

-S rất mạnh khi bạn muốn biết “dòng này được thêm/xóa từ commit nào”.


Branch và switch

git branch
git switch main
git switch -c codex/dev-cli-series

Tên branch tốt:

codex/dev-cli-series
feature/search-modal
fix/auth-token-refresh
docs/bash-series

Trước khi đổi branch:

git status --short

Nếu working tree bẩn, Git có thể từ chối switch hoặc mang thay đổi theo branch mới. Cả hai đều có thể gây nhầm.


Stash khi cần đổi ngữ cảnh

git stash push -m "wip command line series"
git stash list
git stash show -p stash@{0}
git stash pop

Stash hợp khi bạn đang làm dở nhưng cần đổi branch để hotfix.

Đừng dùng stash như kho lưu trữ lâu dài. Nếu ý tưởng đã đáng giữ, commit WIP trên branch riêng thường rõ hơn.


Restore an toàn

Hủy thay đổi chưa stage của một file:

git restore src/lib/series.ts

Bỏ stage nhưng giữ nội dung file:

git restore --staged src/lib/series.ts

Khôi phục file từ commit trước:

git restore --source=HEAD~1 -- src/lib/series.ts

Trước khi restore, chạy:

git diff -- src/lib/series.ts

Đọc diff một lần. Nếu đó là thay đổi của người khác hoặc bạn chưa chắc, đừng xóa.


Pull, rebase, push

Update branch hiện tại:

git fetch origin
git status
git pull --rebase

Push branch mới:

git push -u origin codex/dev-cli-series

Xem branch đang tracking gì:

git branch -vv

Nếu rebase conflict, nhịp xử lý là:

git status
# sửa file conflict
git add path/to/file
git rebase --continue

Không cố đoán. git status luôn nói bước tiếp theo.


Checklist trước khi push

git status --short
git diff --staged
npm run check
npm run build

Tùy repo có thể là:

npm test
pnpm lint
cargo test
go test ./...

Sau đó:

git log --oneline --decorate -5
git push

Bài tập

  1. Tạo branch mới codex/cli-lab.
  2. Sửa một file bất kỳ, xem git diff.
  3. Stage bằng git add -p.
  4. Xem git diff --staged, rồi bỏ stage bằng git restore --staged.
  5. Dùng git log -S tìm commit từng thêm một từ khóa trong repo.
Lời giải tham khảo
git switch -c codex/cli-lab
git diff
git add -p
git diff --staged
git restore --staged path/to/file
git log -S "seriesOrder" -- src

Điều cốt lõi

Git CLI tốt xoay quanh một thói quen: status, diff, stage có chọn lọc, diff staged, commit. Khi bạn đọc được mình sắp đưa gì vào history, Git bớt đáng sợ và code review sạch hơn nhiều.