Git Command Cheatsheet

Ultimate Git Cheatsheet for Dev

Git Command Cheatsheet

📘 Git Cheat Sheet Toàn Tập (PowerShell / Windows)

Tổng hợp đầy đủ các lệnh Git hữu ích cho workflow hàng ngày, bao gồm cả rebase, cherry-pick, và stash. Chia theo nhóm chức năng để dễ tra cứu.


🧭 Trạng thái & Danh sách file

CommandInputOutputExampleTác dụng
git status—Danh sách file + hướng dẫngit statusXem file nào đã sửa, chưa track, đã stage
git status -s—M file.js / ?? new.txtgit status -sList file sạch, dễ copy để git add
git status -sb—## main...origin/main + list filegit status -sbBranch tracking + danh sách file cùng lúc
git status -s -uall—?? dir/file1.mdgit status -s -uallHiện từng file trong thư mục mới, không chỉ tên thư mục

📜 Lịch sử & Xem commit

CommandInputOutputExampleTác dụng
git log --oneline—a1b2c3d Fix buggit log --oneline -1010 commit gần nhất, mỗi commit 1 dòng
git log --graph --oneline --all—Đồ thị ASCIIgit log --graph --oneline --allXem nhánh rẽ, merge
git log -p—Nội dung thay đổigit log -p -3Chi tiết thay đổi của 3 commit gần nhất
git log --author="Name"Tên tác giảCommit của người đógit log --author="nhan"Tìm commit của một người
git log --since="2 weeks ago"Mốc thời gianCommit trong khoảnggit log --since="2 weeks ago"Lọc commit theo thời gian
git log -S "keyword"Chuỗi cần tìmCommit thay đổi chuỗi đógit log -S "apiKey"Tìm commit đã thêm/xóa chuỗi (pickaxe)
git show <hash>Commit hashDiff + metadatagit show HEADXem commit gần nhất đã thay đổi gì

✏️ Thêm & Commit

CommandInputOutputExampleTác dụng
git add <file>Đường dẫn fileStaged filegit add docs/ai/*.mdStage file cụ thể
git add -p—Interactivegit add -pChọn từng khối thay đổi để stage, tránh add cả file
git commit -m "msg"MessageCommit hashgit commit -m "fix typo"Commit với message
git commit -am "msg"MessageCommit hashgit commit -am "update"Add tất cả file đã track + commit (bỏ qua untracked)
git commit --amend -m "new"Message mớiCommit hash mớigit commit --amend -m "better msg"Sửa commit gần nhất (message hoặc thêm file quên)
git commit --amend --no-edit—Commit hash mớigit commit --amend --no-editThêm file vào commit gần nhất, giữ nguyên message

🌿 Nhánh (Branch)

CommandInputOutputExampleTác dụng
git branch—* main / featuregit branchXem đang ở nhánh nào
git branch -a—Local + remotegit branch -aXem cả nhánh remote
git branch -v—Branch + commit cuốigit branch -vXem commit gần nhất của mỗi nhánh
git switch <branch>Tên branchĐổi nhánhgit switch mainChuyển nhánh (cú pháp mới)
git switch -c <branch>Tên branch mớiTạo + chuyểngit switch -c feature/loginTạo nhánh mới và chuyển sang
git switch -c <branch> origin/<branch>Branch remoteTạo local từ remotegit switch -c fix origin/fixTạo local branch tracking remote branch
git branch -d <branch>Tên branchXóa nhánhgit branch -d feature/oldXóa nhánh an toàn (chặn nếu chưa merge)
git branch -D <branch>Tên branchXóa cưỡng chếgit branch -D feature/junkXóa nhánh kể cả chưa merge
git branch -m <new>Tên mớiĐổi têngit branch -m mainĐổi tên nhánh hiện tại

🔄 Làm việc với Remote

CommandInputOutputExampleTác dụng
git remote -v—origin https://...git remote -vXem remote đang trỏ đâu
git fetch—Thông báo cập nhậtgit fetch originTải thay đổi từ remote, chưa merge
git fetch --prune—Dọn ref đã xóagit fetch --pruneXóa tracking branch không còn trên remote
git pull—Merge outputgit pull origin mainFetch + merge từ remote
git pull --rebase—Rebase outputgit pull --rebaseFetch + rebase, tránh merge commit thừa
git push—Push outputgit push origin mainĐẩy commit lên remote
git push -u origin <branch>Tên branchSet upstreamgit push -u origin feature/loginPush lần đầu + set tracking branch
git push --force-with-lease—Force push an toàngit push --force-with-leaseForce push nhưng an toàn hơn --force (kiểm tra remote chưa đổi)
git push origin --delete <branch>Tên branchXóa remote branchgit push origin --delete old-featureXóa branch trên remote

↩️ Hoàn tác & Sửa sai

CommandInputOutputExampleTác dụng
git restore <file>Đường dẫnFile về trạng thái cũgit restore docs/ai/_meta.jsonBỏ thay đổi chưa stage của file
git restore --staged <file>Đường dẫnUnstage filegit restore --staged file.jsBỏ file khỏi staging (giữ thay đổi)
git reset --soft HEAD~1—Undo commit, giữ stagedgit reset --soft HEAD~1Hủy commit gần nhất nhưng giữ file đã stage
git reset HEAD~1—Undo commit, giữ unstagedgit reset HEAD~1Hủy commit gần nhất, file về unstaged
git reset --hard HEAD~1—Mất thay đổigit reset --hard HEAD~1Hủy commit + xóa thay đổi (nguy hiểm)
git revert <hash>Commit hashCommit mớigit revert a1b2c3dTạo commit mới đảo ngược commit cũ (an toàn cho remote)
git reflog—Danh sách HEAD di chuyểngit reflogXem lịch sử mọi thay đổi HEAD, cứu commit bị mất

🔍 Xem thay đổi (Diff)

CommandInputOutputExampleTác dụng
git diff—+/- linesgit diffXem thay đổi chưa stage
git diff --staged—+/- linesgit diff --stagedXem thay đổi đã stage (sắp commit)
git diff <file>Đường dẫn+/- linesgit diff docs/ai/_meta.jsonXem thay đổi của file cụ thể
git diff HEAD~1—+/- linesgit diff HEAD~1So sánh working tree với commit trước
git diff --stat—Tóm tắt số dònggit diff --statXem nhanh file nào thay đổi bao nhiêu dòng
git diff --word-diff—Diff theo từgit diff --word-diffSo sánh theo từng từ thay vì từng dòng

🧹 Dọn dẹp & Xóa

CommandInputOutputExampleTác dụng
git rm <file>Đường dẫnXóa file + stagegit rm old.txtXóa file khỏi repo và filesystem
git rm --cached <file>Đường dẫnXóa khỏi repogit rm --cached .envXóa khỏi Git nhưng giữ file trên máy
git clean -n—Dry rungit clean -nXem file untracked nào sẽ bị xóa (không xóa thật)
git clean -fd—Xóa file + thư mụcgit clean -fdXóa untracked file và thư mục (nguy hiểm)
git clean -fdx—Xóa cả ignoredgit clean -fdxXóa cả file bị ignore (nguy hiểm hơn)

🔄 Rebase — Viết lại lịch sử

CommandInputOutputExampleTác dụng
git rebase <branch>Branch đíchÁp dụng lại commitgit rebase mainDi chuyển toàn bộ commit của branch hiện tại lên trên main, tạo lịch sử tuyến tính
git rebase -i HEAD~nn = số commitMở editor tương tácgit rebase -i HEAD~3Mở editor để pick/reword/edit/squash/fixup/drop
git rebase -i <branch>Branch đíchMở editor tương tácgit rebase -i mainRebase tương tác tất cả commit giữa branch hiện tại và main
git rebase --continue—Tiếp tụcgit rebase --continueSau khi giải quyết conflict, stage file rồi chạy lệnh này
git rebase --skip—Bỏ qua commitgit rebase --skipBỏ qua commit đang gây conflict
git rebase --abort—Hủy rebasegit rebase --abortQuay lại trạng thái trước khi rebase
git rebase --autostash—Tự động stash/unstashgit rebase main --autostashTự động stash thay đổi chưa commit trước khi rebase
git rebase --onto <newbase> <upstream> <branch>3 branchRebase branch lên newbasegit rebase --onto master server clientDi chuyển commit của client (từ sau server) lên master

Các lệnh trong interactive rebase (thay thế pick):

  • pick — giữ nguyên commit
  • reword — giữ commit, sửa message
  • edit — dừng lại để sửa nội dung commit
  • squash — gộp vào commit trước, gộp cả message
  • fixup — gộp vào commit trước, bỏ message của commit này
  • drop — xóa commit

🍒 Cherry-pick — Chọn commit cụ thể

CommandInputOutputExampleTác dụng
git cherry-pick <hash>Commit hashCommit mớigit cherry-pick a1b2c3dSao chép một commit cụ thể vào branch hiện tại
git cherry-pick <h1> <h2>Nhiều hashNhiều commit mớigit cherry-pick a1b2c3d d4e5f6aCherry-pick nhiều commit không liên tiếp
git cherry-pick <start>^..<end>Range hashNhiều commit mớigit cherry-pick d4e5f6a^..a1b2c3dCherry-pick một dải commit liên tiếp
git cherry-pick -n <hash>Commit hashStage, không commitgit cherry-pick -n a1b2c3dÁp dụng thay đổi nhưng không tự động commit
git cherry-pick -e <hash>Commit hashMở editor messagegit cherry-pick -e a1b2c3dCherry-pick và sửa commit message
git cherry-pick --continue—Tiếp tụcgit cherry-pick --continueSau khi giải quyết conflict
git cherry-pick --skip—Bỏ quagit cherry-pick --skipBỏ qua commit đang conflict
git cherry-pick --abort—Hủygit cherry-pick --abortHủy toàn bộ, quay về ban đầu

📦 Stash — Cất tạm thay đổi

CommandInputOutputExampleTác dụng
git stash—Saved working directory...git stashCất tất cả thay đổi (staged + unstaged)
git stash push -m "msg"MessageStash có têngit stash push -m "WIP auth"Cất thay đổi kèm message
git stash -u—Bao gồm untrackedgit stash -uCất cả file untracked
git stash -a—Bao gồm ignoredgit stash -aCất cả file untracked và ignored
git stash --staged—Chỉ stagedgit stash --stagedChỉ cất thay đổi đã stage
git stash --keep-index—Giữ staged lạigit stash --keep-indexCất unstaged, giữ staged để test
git stash -p—Interactivegit stash -pChọn từng hunk để cất (partial stash)
git stash list—Danh sách stashgit stash listLiệt kê tất cả stash
git stash show—File thay đổigit stash showTóm tắt file trong stash gần nhất
git stash show -p—Full diffgit stash show -p stash@{1}Diff đầy đủ của stash
git stash apply—Khôi phụcgit stash applyÁp dụng stash gần nhất, giữ trong list
git stash apply --index—Khôi phục + stagedgit stash apply --indexKhôi phục cả trạng thái staged ban đầu
git stash apply stash@{n}Stash indexKhôi phục cụ thểgit stash apply stash@{2}Áp dụng stash cụ thể
git stash pop—Khôi phục + xóagit stash popÁp dụng stash gần nhất rồi xóa khỏi list
git stash pop stash@{n}Stash indexKhôi phục + xóagit stash pop stash@{1}Pop stash cụ thể
git stash drop stash@{n}Stash indexXóa 1 stashgit stash drop stash@{0}Xóa một stash cụ thể
git stash clear—Xóa tất cảgit stash clearXóa toàn bộ stash (không thể phục hồi)
git stash branch <name>Tên branchTạo branch từ stashgit stash branch fix-auth stash@{1}Tạo branch mới tại commit gốc của stash

🔎 Tìm bug & Cứu commit

CommandInputOutputExampleTác dụng
git bisect start—Bắt đầu tìm buggit bisect startBắt đầu quá trình binary search tìm commit gây bug
git bisect bad—Đánh dấu commit lỗigit bisect badĐánh dấu commit hiện tại là có bug
git bisect good <hash>Commit hashĐánh dấu commit tốtgit bisect good v1.0Đánh dấu commit cũ là không có bug
git bisect reset—Kết thúcgit bisect resetThoát chế độ bisect
git reflog—Lịch sử HEADgit reflogXem mọi thay đổi HEAD, cứu commit bị mất
git reset --hard HEAD@{n}Reflog indexQuay vềgit reset --hard HEAD@{3}Quay về trạng thái cách đây 3 HEAD move

⚙️ Cấu hình & Tiện ích

CommandInputOutputExampleTác dụng
git config --list—Key=valuegit config --listXem toàn bộ cấu hình
git config --global user.name "X"TênĐặt têngit config --global user.name "nhan"Cấu hình tên tác giả
git config --global user.email "X"EmailĐặt emailgit config --global user.email "[email protected]"Cấu hình email
git config --global alias.last "log -1 HEAD"AliasTạo shortcutgit config --global alias.last "log -1 HEAD"Tạo alias git last
git config --global core.editor "code --wait"EditorĐặt editorgit config --global core.editor "code --wait"VS Code làm editor mặc định
git config --global core.autocrlf true—Xử lý line endinggit config --global core.autocrlf trueTự động chuyển LF ↔ CRLF trên Windows
git config --global pull.rebase true—Pull = rebasegit config --global pull.rebase truegit pull mặc định dùng rebase

💡 Workflow thực tế

Rebase để cập nhật branch feature với main:

git switch feature
git fetch origin
git rebase origin/main
# giải quyết conflict nếu có, rồi:
git rebase --continue

Squash 3 commit gần nhất thành 1:

git rebase -i HEAD~3
# đổi 'pick' thành 'squash' cho 2 commit sau
# lưu, editor mở ra để gộp message

Cherry-pick hotfix từ main sang release branch:

git switch release/v2
git cherry-pick a1b2c3d

Stash để chuyển branch gấp:

git stash push -m "WIP: đang sửa auth"
git switch hotfix-branch
# làm việc...
git switch main
git stash pop

Cứu commit bị mất sau reset --hard:

git reflog
# tìm hash commit trước khi reset
git reset --hard a1b2c3d

💡 Mẹo cho PowerShell

Posh-Git — module hiển thị nhánh Git trên prompt + tab-completion thông minh:

Install-Module Posh-Git -Scope CurrentUser
Import-Module Posh-Git

Thêm Import-Module Posh-Git vào $PROFILE để tự động load mỗi phiên.

Copy file list để git add hàng loạt (cẩn thận: add cả untracked):

git status -s | ForEach-Object { $_.Substring(3) } | ForEach-Object { git add $_ }

Add tất cả file đã sửa (không add untracked):

git status -s | Where-Object { $_ -match '^ M' } | ForEach-Object { git add $_.Substring(3) }

Add tất cả file untracked:

git status -s | Where-Object { $_ -match '^\?\?' } | ForEach-Object { git add $_.Substring(3) }

🔗 Tài liệu tham khảo nhanh

  • Working tree — vùng làm việc hiện tại
  • Index / Staging — vùng chờ commit
  • HEAD — commit hiện tại đang trỏ tới
  • ^ — viết tắt của HEAD~1 (commit trước)
  • ~n — lùi n commit
  • @{n} — tham chiếu reflog/stash thứ n