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.
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.
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.
git log --oneline
git log --oneline --graph --decorate --all
git log --author="Thinh" --since="2 weeks ago"
git log -- src/auth.jsgit-show
Hiển thị object/commit cụ thể.
git show HEAD
git show abc123
git show HEAD~2:src/app.js2. Staging Area
git-add
Đưa nội dung thay đổi vào index cho commit kế tiếp.
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.
git restore app.js
git restore --staged app.js
git restore --source=HEAD~1 -- app.jsLệ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.
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 case4. Quản lý file tracked
git-rm
Xóa file khỏi working tree và index, hoặc chỉ bỏ tracking.
git rm old.txt
git rm --cached .envgit-mv
Di chuyển/đổi tên file và stage thay đổi tương ứng.
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/*Workflow commit khuyên dùng
git status
git diff
git add -p
git diff --staged
git commit -m "feat: ..."
git status