Sổ tay kỹ thuật

Git – lệnh hay dùng

cheatsheet#git· cập nhật 2026-11-06

Tra nhanh lệnh Git hằng ngày, gồm branch, commit, diff, log, stash, remote, undo và tag, theo cú pháp hiện đại (git switch/restore).

Sửa trang này
Mục lục

Lệnh dùng cú pháp Git ≥ 2.23 (switch/restore thay cho checkout đa năng). Rebase và sửa lịch sử xem Rebase workflow, cứu sự cố xem Git troubleshooting.

Cấu hình một lần

Mục đích Lệnh
Tên, email git config --global user.name "Nguyen Van A" / git config --global user.email a@example.com
Branch mặc định khi init git config --global init.defaultBranch main
pull luôn rebase thay vì merge git config --global pull.rebase true
Tự xoá ref remote đã bị xoá khi fetch git config --global fetch.prune true
Nhớ cách giải conflict đã làm git config --global rerere.enabled true
Push branch mới không cần -u git config --global push.autoSetupRemote true
Xem config và file nguồn git config --list --show-origin

Branch

Mục đích Lệnh
Tạo và chuyển sang branch mới git switch -c feature/bgp-policy
Tạo branch từ remote branch git switch -c fix-x origin/fix-x (hoặc git switch fix-x nếu tên trùng)
Quay lại branch trước git switch -
Liệt kê branch kèm upstream, ahead/behind git branch -vv
Branch đã merge vào main git branch --merged main
Xoá branch local (đã merge / ép) git branch -d name / git branch -D name
Xoá branch trên remote git push origin --delete name
Đổi tên branch hiện tại git branch -m new-name
Làm việc song song 2 branch, không stash git worktree add ../repo-hotfix hotfix/1.2

Stage, commit, khôi phục file

Mục đích Lệnh
Stage từng đoạn git add -p
Bỏ stage (giữ thay đổi) git restore --staged file
Huỷ thay đổi chưa stage git restore file
Lấy file từ commit/branch khác git restore --source=main -- path/file
Sửa commit cuối (message hoặc thêm file) git commit --amend / git commit --amend --no-edit
Commit kèm chữ ký git commit -S -m "msg" (xem ký GPG)
Xem file sẽ bị clean xoá (dry-run) git clean -nd → thật: git clean -fd

Xem thay đổi và lịch sử

Mục đích Lệnh
Diff chưa stage / đã stage git diff / git diff --staged
Diff giữa 2 branch (từ điểm rẽ nhánh) git diff main...feature
Chỉ tên file thay đổi git diff --name-status main...HEAD
Log gọn dạng đồ thị git log --oneline --graph --decorate --all
Commit có ở feature mà chưa có ở main git log main..feature --oneline
Lịch sử 1 file (kể cả khi đổi tên) git log --follow -p -- path/file
Tìm commit thêm/xoá một chuỗi git log -S 'route-policy PASS' --oneline
Ai sửa dòng 10-30 git blame -L 10,30 path/file
Xem file ở commit cũ git show HEAD~3:path/file
Thống kê commit theo người git shortlog -sn
Tìm commit gây lỗi (nhị phân) git bisect start; git bisect bad; git bisect good v1.0 → git bisect reset

Stash

Mục đích Lệnh
Cất thay đổi kèm ghi chú (cả file untracked) git stash push -u -m "wip bgp"
Liệt kê / xem nội dung git stash list / git stash show -p stash@{0}
Lấy lại và xoá khỏi stash git stash pop
Lấy lại nhưng giữ trong stash git stash apply stash@{1}
Tạo branch từ stash git stash branch wip-branch stash@{0}

Remote, đồng bộ

Mục đích Lệnh
Xem / thêm remote git remote -v / git remote add upstream git@github.com:org/repo.git
Fetch tất cả và dọn ref cũ git fetch --all --prune
Pull bằng rebase git pull --rebase
Push branch mới và đặt upstream git push -u origin HEAD
Force push an toàn git push --force-with-lease
Clone nhẹ (chỉ commit mới nhất) git clone --depth 1 URL

Undo

Mục đích Lệnh
Bỏ commit cuối, giữ thay đổi ở stage git reset --soft HEAD~1
Bỏ commit cuối, giữ thay đổi ở working tree git reset HEAD~1
Bỏ hẳn commit và thay đổi (nguy hiểm) git reset --hard HEAD~1
Tạo commit đảo ngược (đã push) git revert <sha>
Revert một merge commit git revert -m 1 <merge-sha>
Lấy 1 commit từ branch khác git cherry-pick -x <sha>
Xem mọi vị trí HEAD từng trỏ tới git reflog

reset --hard, clean -fd, push --force* làm mất dữ liệu. Chạy git status và git stash trước nếu không chắc.

Tag

Mục đích Lệnh
Tạo annotated tag git tag -a v1.2.0 -m "release 1.2.0"
Push một tag / mọi tag annotated đi kèm commit git push origin v1.2.0 / git push --follow-tags
Xoá tag local và remote git tag -d v1.2.0 && git push origin --delete v1.2.0
Tag gần nhất git describe --tags --abbrev=0