📘 Git Command Cheat Sheet
🔹 Initial Setup
| Command |
Description |
git init |
สร้าง repo ใหม่ใน directory ปัจจุบัน |
git clone <url> |
โคลน repo จาก remote |
git config --global user.name "Your Name" |
ตั้งชื่อผู้ใช้ |
git config --global user.email "you@email.com" |
ตั้งอีเมลผู้ใช้ |
🔹 Basic Workflow
| Command |
Description |
git status |
ดูสถานะไฟล์ (modified, staged, untracked) |
git add <file> |
เพิ่มไฟล์เข้า staging area |
git add . |
เพิ่มไฟล์ทั้งหมดเข้า staging area |
git commit -m "message" |
บันทึกการเปลี่ยนแปลงพร้อมข้อความ |
git log |
ดู history commit |
git log --oneline --graph --decorate |
ดู log แบบสั้น มีกราฟ branch |
🔹 Branching
| Command |
Description |
git branch |
ดู branch ทั้งหมด (local) |
git branch <name> |
สร้าง branch ใหม่ |
git checkout <branch> |
สลับ branch |
git switch <branch> |
สลับ branch (คำสั่งใหม่กว่า) |
git checkout -b <branch> |
สร้าง branch ใหม่และสลับไปทันที |
git branch -d <branch> |
ลบ branch (ถ้า merge แล้ว) |
🔹 Merge & Rebase
| Command |
Description |
git merge <branch> |
รวม branch เข้ามา → สร้าง merge commit |
git rebase <branch> |
ยก commit ของ branch ปัจจุบันมาต่อท้าย branch อื่น → history เรียบ |
🔹 Remote
| Command |
Description |
git remote -v |
ดู remote repo ที่เชื่อมอยู่ |
git remote add origin <url> |
เพิ่ม remote repo |
git push origin <branch> |
ส่ง branch ไปที่ remote |
git pull origin <branch> |
ดึงและ merge branch จาก remote |
git fetch |
ดึงข้อมูล commit/branch จาก remote (ไม่ merge) |
🔹 Diff & Log
| Command |
Description |
git diff |
ดูความต่างระหว่าง working dir ↔ staging |
git diff --cached |
ดูความต่างระหว่าง staging ↔ commit ล่าสุด |
git diff <commit1> <commit2> |
ดูความต่างระหว่าง 2 commit |
🔹 Reset & Revert
| Command |
Description |
git reset <file> |
ยกเลิกไฟล์ออกจาก staging |
git reset --hard <commit> |
ย้อนกลับไป commit ที่กำหนด (ล้างการเปลี่ยนแปลงทั้งหมด) |
git revert <commit> |
สร้าง commit ใหม่เพื่อ undo commit ที่เลือก (ปลอดภัยกว่า reset) |
🔹 Stash
| Command |
Description |
git stash |
เก็บงานที่ยังไม่ commit ออกไปพัก |
git stash list |
ดู stash ที่เก็บไว้ |
git stash pop |
นำงานที่ stash ไว้กลับมาใช้งานและลบออก |
🔹 Tagging
| Command |
Description |
git tag <tagname> |
สร้าง tag แบบ lightweight |
git tag -a <tagname> -m "message" |
สร้าง tag แบบ annotated |
git push origin <tagname> |
ส่ง tag ไป remote |
git push origin --tags |
ส่ง tag ทั้งหมดไป remote |
✅ สรุป Workflow ง่าย ๆ ใน 5 ขั้นตอน
- แตก branch ใหม่ →
git checkout -b feature-x
- ทำงาน แก้ไฟล์ →
git add .
- commit →
git commit -m "message"
- push →
git push origin feature-x
- เปิด Merge Request /Pull Request กลับเข้า main/master