Git Handbook

12 · Git Cheat Sheet

Bảng tra nhanh những command và quyết định thường gặp sau khi bạn đã hiểu cơ chế phía sau.

Daily 80/20

Mục đíchLệnh
Statusgit status -sb
Diff chưa stagegit diff
Stage chọn lọcgit add -p
Review stagedgit diff --staged
Commitgit commit -m "..."
Graphgit log --oneline --graph --decorate --all
Branch mớigit switch -c feature/x
Fetchgit fetch --prune origin
Push first timegit push -u origin feature/x

Undo selector

Muốn làm gì?Lệnh
Bỏ thay đổi file chưa stagegit restore file
Unstagegit restore --staged file
Sửa commit cuốigit commit --amend
Undo local commit, giữ stagedgit reset --soft HEAD~1
Undo local commit, giữ code unstagedgit reset HEAD~1
Undo shared commitgit revert <sha>
Tìm commit tưởng đã mấtgit reflog

Branch / history

git branch -vv
git switch main
git switch -c feature/x
git merge feature/x
git rebase origin/main
git rebase -i HEAD~5
git cherry-pick <sha>
git push --force-with-lease

Debug

git show <sha>
git blame -L 20,60 file.js
git grep -n "pattern"
git log -S"string" --oneline
git log -G"regex" --oneline
git bisect start

Advanced

git stash push -u -m "WIP"
git stash pop
git tag -a v1.0.0 -m "Release"
git worktree add ../hotfix -b hotfix main
git sparse-checkout init --cone
git clean -nd     # dry run first
git archive --format=zip v1.0.0 > release.zip

Aliases hữu ích

git config --global alias.st "status -sb"
git config --global alias.lg "log --oneline --graph --decorate --all"
git config --global alias.last "show --stat --oneline HEAD"

Sau đó:

git st
git lg
git last

Danger zone

Kiểm tra kỹ trước: reset --hard, clean -fd, push --force, interactive rebase trên public commits, xóa branch chưa merge.