| การตั้งค่าและกำหนดค่า |
|---|
| ตั้งชื่อ อีเมล หรือ editor ของคุณครั้งเดียวใช้ได้กับทุก repo | git config --global user.name "Ada" |
| กำหนดชื่อลัดให้คำสั่งที่คุณพิมพ์บ่อย | git config --global alias.co checkout |
| สร้าง repository ใหม่ในโฟลเดอร์ปัจจุบัน | git init |
| คัดลอก remote repository มาไว้บนเครื่องของคุณ | git clone git@github.com:org/repo.git |
| คัดลอกเฉพาะ commit ล่าสุดของ branch เดียว | git clone --depth 1 --single-branch git@github.com:org/repo.git |
| พื้นฐาน snapshot |
|---|
| แสดงสิ่งที่ถูก stage, แก้ไขแล้ว และยังไม่ถูกติดตาม | git status -sb |
| stage การเปลี่ยนแปลงเพื่อให้เข้าสู่ commit ถัดไป | git add src/app.ts |
| ทบทวนการเปลี่ยนแปลงทีละส่วน และ stage เฉพาะส่วนที่คุณยอมรับ | git add -p src/app.ts |
| stage การเปลี่ยนแปลงของทุกไฟล์ที่ติดตามอยู่ โดยไม่เพิ่มไฟล์ใหม่ | git add -u |
| บันทึกการเปลี่ยนแปลงที่ stage แล้วเป็น commit ใหม่ | git commit -m "fix: null check" |
| แทนที่ commit ล่าสุดด้วยอันที่แก้ไขแล้ว | git commit --amend -m "fix: handle empty input" |
| แสดงการเปลี่ยนแปลงที่ยังไม่ stage หรือที่ stage แล้วด้วย --staged | git diff --staged |
| แสดงทุกการเปลี่ยนแปลงตั้งแต่ commit ล่าสุด ไม่ว่าจะ stage หรือไม่ | git diff HEAD |
| การแตก branch และ merge |
|---|
| แสดงรายการ, สร้าง หรือลบ branch | git branch feature-x |
| ลบ branch ที่ merge แล้ว (-D บังคับลบ, -a แสดงทุก branch) | git branch -d feature-x |
| สลับ branch (คำสั่งสมัยใหม่ที่แทน checkout) | git switch feature-x |
| สร้าง branch ใหม่และสลับไปในคำสั่งเดียว | git switch -c feature-x |
| สลับ branch หรือกู้คืนไฟล์ (คำสั่งเก่าที่ครอบคลุมกว่า) | git checkout feature-x |
| นำการเปลี่ยนแปลงจาก branch อื่นเข้ามาใน branch ปัจจุบัน | git merge feature-x |
| ยกเลิก merge ที่เกิด conflict และคืน branch กลับสู่สถานะเดิม | git merge --abort |
| เล่น commit ของ branch ปัจจุบันซ้ำบน branch อื่น | git rebase main |
| รวม, จัดลำดับ หรือแก้ไข commit ขณะเล่นซ้ำ | git rebase -i main |
| คัดลอก commit เดี่ยวจาก branch อื่นมาไว้บน branch นี้ | git cherry-pick a1b2c3d |
| ติดป้ายชื่อที่อ่านง่าย เช่น เวอร์ชัน ให้กับ commit | git tag -a v1.2.0 |
| เช็คเอาต์ branch อื่นในโฟลเดอร์แยกไปพร้อมกัน | git worktree add ../repo-feature feature-x |
| การตรวจดูประวัติ |
|---|
| เดินดูประวัติ commit | git log --oneline --graph --decorate |
| เดินดูประวัติจากทุก branch ไม่ใช่แค่ branch ปัจจุบัน | git log --oneline --graph --all |
| แสดง diff และข้อความเต็มของ commit เดียว | git show HEAD |
| แสดงว่าใครแก้แต่ละบรรทัดของไฟล์ล่าสุด | git blame src/app.ts |
| แสดงทุก commit ที่ HEAD เคยชี้ รวมถึงที่ถูกทิ้งไป | git reflog |
| ค้นหาในประวัติแบบครึ่ง ๆ เพื่อหา commit ที่ทำให้พัง | git bisect start |
| สรุป commit ตามผู้เขียน | git shortlog -sn |
| ตั้งชื่อ commit ปัจจุบันโดยนับจาก tag ที่ใกล้ที่สุด | git describe --tags |
| การทำงานร่วมกับ remote |
|---|
| แสดงรายการหรือเพิ่ม remote repository | git remote add origin <url> |
| แสดง remote พร้อม URL สำหรับ fetch และ push | git remote -v |
| ดาวน์โหลดการเปลี่ยนแปลงจาก remote โดยไม่แตะ branch ของคุณ | git fetch origin |
| ดาวน์โหลดการเปลี่ยนแปลง และตัด ref ของ branch บน remote ที่ถูกลบ | git fetch --prune |
| fetch และ merge การเปลี่ยนแปลงจาก remote ของ branch ปัจจุบัน | git pull --rebase |
| ส่ง commit ในเครื่องไปยัง remote | git push -u origin feature-x |
| เขียนทับ branch บน remote แต่เฉพาะเมื่อไม่มีใคร push มาก่อน | git push --force-with-lease origin feature-x |
| การย้อนการเปลี่ยนแปลง |
|---|
| ทิ้งการเปลี่ยนแปลงใน working tree หรือถอนออกจาก staging | git restore src/app.ts |
| ถอนไฟล์ออกจาก staging แต่คงการแก้ไขไว้ใน working tree | git restore --staged src/app.ts |
| ย้ายปลายของ branch ปัจจุบัน และตามตัวเลือกคือ index ด้วย | git reset --hard HEAD~1 |
| เพิ่ม commit ใหม่ที่ย้อน commit เดิม (ปลอดภัยต่อการแชร์) | git revert HEAD |
| ลบไฟล์และโฟลเดอร์ที่ไม่ถูกติดตาม | git clean -fd |
| การ stash งาน |
|---|
| เก็บงานที่ยังไม่ commit แล้วรีเซ็ต working tree | git stash -u |
| แสดงทุก stash ที่คุณเก็บไว้ | git stash list |
| นำ stash กลับมาใช้โดยยังเก็บไว้ในลิสต์ | git stash apply stash@{1} |
| ลบ stash ที่คุณไม่ต้องการแล้ว | git stash drop stash@{1} |
| นำ stash ล่าสุดกลับมาใช้แล้วลบออกจากลิสต์ | git stash pop |
| ไฟล์และ submodule |
|---|
| ย้ายหรือเปลี่ยนชื่อไฟล์ แล้ว stage การเปลี่ยนแปลง | git mv old.ts src/new.ts |
| ลบไฟล์แล้ว stage การลบ | git rm src/old.ts |
| ติดตาม repository อื่นเป็นโฟลเดอร์ภายในอันนี้ | git submodule add git@github.com:org/lib.git lib |
| เช็คเอาต์เฉพาะโฟลเดอร์ที่คุณระบุจาก repository ขนาดใหญ่ | git sparse-checkout set src docs |
| ส่งออก tree ของ commit เป็นไฟล์ tar หรือ zip | git archive -o repo.zip HEAD |