กลับไปหน้าสูตร
#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 main | default 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 --prune | sync refs และลบ ref เก่า |
git pull --rebase | update branch แบบ linear |
git push -u origin <branch> | push และ set upstream |
Status / Diff / Log
| Command | คำอธิบาย |
|---|---|
git status -sb | ดูสถานะสั้น |
git diff | diff unstaged |
git diff --staged | diff 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/x | merge branch |
git branch -d feature/x | ลบ branch ที่ merge แล้ว |
git branch -D feature/x | force delete branch |
Rebase / History Rewrite
| Command | คำอธิบาย |
|---|---|
git rebase main | ย้ายฐาน branch |
git rebase -i HEAD~10 | interactive rebase |
git rebase --continue | ทำต่อหลังแก้ conflict |
git rebase --abort | ยกเลิก rebase |
git commit --amend | แก้ commit ล่าสุด |
git push --force-with-lease | push หลัง 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 pop | apply แล้วลบ |
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 good | mark good/bad |
git bisect reset | จบ bisect |
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-commit | lint/format ก่อน commit |
| commit-msg | enforce Conventional Commit |
Conflict Resolution Tips
| เทคนิค | วิธีใช้ |
|---|---|
| ดูไฟล์ conflict ทั้งหมด | git diff --name-only --diff-filter=U |
| รับฝั่ง ours/theirs | git checkout --ours <file> / --theirs |
| เปิด tool | git mergetool |
| จบ conflict | git add . && git rebase --continue |
Team Workflow (Recommended)
| Step | Command |
|---|---|
| sync main | git switch main && git pull --rebase |
| new branch | git switch -c feat/<name> |
| commit | git add . && git commit -m "feat: ..." |
| update branch | git fetch origin && git rebase origin/main |
| push | git push -u origin HEAD |