กลับไปหน้าสูตร
#git#command#vcs#cheatsheet

Git Command Cheatsheet

รวมคำสั่ง Git แบบจัดเต็ม: setup, branching, rebase, stash, undo, bisect, hooks และ workflow ทีม

11 มีนาคม 2569อ่านประมาณ 3 นาที

Global Setup

Commandคำอธิบาย
git config --global user.name "Your Name"ตั้งชื่อ
git config --global user.email "you@example.com"ตั้งอีเมล
git config --global init.defaultBranch maindefault branch
git config --global pull.rebase trueให้ pull เป็น rebase
git config --global core.editor "code --wait"ตั้ง editor
git config --global alias.st "status -sb"alias ยอดนิยม

Init / Clone / Remote

Commandคำอธิบาย
git initสร้าง repo
git clone <url>clone repo
git remote -vดู remotes
git remote add origin <url>เพิ่ม remote
git fetch --all --prunesync refs และลบ ref เก่า
git pull --rebaseupdate branch แบบ linear
git push -u origin <branch>push และ set upstream

Status / Diff / Log

Commandคำอธิบาย
git status -sbดูสถานะสั้น
git diffdiff unstaged
git diff --stageddiff staged
git log --oneline --graph --decorate --allดูกราฟประวัติ
git show <commit>ดู commit detail
git blame <file>ดูเจ้าของแต่ละบรรทัด

Branching

Commandคำอธิบาย
git switch -c feature/xสร้างและสลับ branch
git branch -vvดู branch + upstream
git switch mainกลับ main
git merge feature/xmerge branch
git branch -d feature/xลบ branch ที่ merge แล้ว
git branch -D feature/xforce delete branch

Rebase / History Rewrite

Commandคำอธิบาย
git rebase mainย้ายฐาน branch
git rebase -i HEAD~10interactive rebase
git rebase --continueทำต่อหลังแก้ conflict
git rebase --abortยกเลิก rebase
git commit --amendแก้ commit ล่าสุด
git push --force-with-leasepush หลัง rewrite แบบปลอดภัยกว่า

Stash

Commandคำอธิบาย
git stash push -u -m "wip: form page"stash รวม untracked
git stash listดูรายการ stash
git stash show -p stash@{0}ดู patch stash
git stash apply stash@{1}apply stash
git stash popapply แล้วลบ
git stash drop stash@{1}ลบ stash เดี่ยว

Undo / Recover

Commandคำอธิบาย
git restore <file>คืนไฟล์ที่ยังไม่ stage
git restore --staged <file>เอาออกจาก stage
git reset --soft HEAD~1ย้อน commit เก็บ staged
git reset --mixed HEAD~1ย้อน commit เก็บไฟล์
git revert <commit>สร้าง commit ย้อนกลับ
git reflogกู้ commit ที่หายจาก history

Cherry-pick / Bisect

Commandคำอธิบาย
git cherry-pick <hash>ดึง commit เดี่ยว
git cherry-pick A..Bดึงช่วง commit
git bisect startเริ่มหา bad commit
git bisect bad / git bisect goodmark good/bad
git bisect resetจบ bisect

Tags / Release

Commandคำอธิบาย
git tagดู tags
git tag -a v1.0.0 -m "release"สร้าง annotated tag
git push origin v1.0.0push tag เดี่ยว
git push origin --tagspush tags ทั้งหมด
git tag -d v1.0.0ลบ local tag

Worktree (หลาย branch พร้อมกัน)

Commandคำอธิบาย
git worktree add ../repo-hotfix hotfix/xเปิด branch ใหม่อีกโฟลเดอร์
git worktree listดู worktrees
git worktree remove ../repo-hotfixลบ worktree

Hooks & Quality

Commandคำอธิบาย
git config core.hooksPath .githooksตั้ง hook path
pre-commitlint/format ก่อน commit
commit-msgenforce Conventional Commit

Conflict Resolution Tips

เทคนิควิธีใช้
ดูไฟล์ conflict ทั้งหมดgit diff --name-only --diff-filter=U
รับฝั่ง ours/theirsgit checkout --ours <file> / --theirs
เปิด toolgit mergetool
จบ conflictgit add . && git rebase --continue