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 đích | Lệnh |
|---|---|
| Status | git status -sb |
| Diff chưa stage | git diff |
| Stage chọn lọc | git add -p |
| Review staged | git diff --staged |
| Commit | git commit -m "..." |
| Graph | git log --oneline --graph --decorate --all |
| Branch mới | git switch -c feature/x |
| Fetch | git fetch --prune origin |
| Push first time | git push -u origin feature/x |
Undo selector
| Muốn làm gì? | Lệnh |
|---|---|
| Bỏ thay đổi file chưa stage | git restore file |
| Unstage | git restore --staged file |
| Sửa commit cuối | git commit --amend |
| Undo local commit, giữ staged | git reset --soft HEAD~1 |
| Undo local commit, giữ code unstaged | git reset HEAD~1 |
| Undo shared commit | git revert <sha> |
| Tìm commit tưởng đã mất | git 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-leaseDebug
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 startAdvanced
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.zipAliases 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 lastDanger 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.