ชีตเช็คคำสั่ง Git
Git ติดตามการเปลี่ยนแปลงไฟล์ของคุณเป็นชุดของคอมมิทในรีโพซิทอรี คำสั่งเหล่านี้ครอบคลุมขั้นตอนประจำวัน: สร้างรีโพ, บันทึกงาน, แตกสาขา, รวม และแชร์กับรีโมต ระหว่างที่เรียนให้เปิดชีตนี้ไว้
| คำสั่ง | ทำอะไร |
|---|---|
| git init | Create a new, empty Git repository in the current directory. |
| git clone | Download a remote repository and its full history to your machine. |
| git status | Show changed, staged and untracked files in the working tree. |
| git add | Stage changes (files) to be included in the next commit. |
| git commit | Record staged changes as a new commit with a message. |
| git log | Display the commit history of the repository. |
| git diff | Show unstaged or staged differences between states. |
| git branch | List, create or delete branches in the repository. |
| git checkout / switch | Switch branches or restore files (switch is the modern command). |
| git merge | Join another branch's history into the current branch. |
| git rebase | Reapply commits on top of another base commit, rewriting history. |
| git pull | Fetch from a remote and merge the changes into the current branch. |
| git push | Upload local commits to a remote repository. |
| git stash | Temporarily shelve uncommitted changes to reapply later. |
| git remote | Manage the set of remote repositories you track. |
หมายเหตุ
- คำสั่งส่วนใหญ่รับแฟล็ก (เช่น -m สำหรับข้อความ หรือ -a สำหรับทั้งหมด) รัน 'git help <คำสั่ง>' เพื่อดูรายการเต็ม
- ชื่อสาขามีต้นทุนต่ำและทิ้งได้ — คอมมิทบ่อยๆ บนสาขาแฟชัน แล้วค่อยรวมเข้า main
- ก่อนใช้คำสั่งทำลายเช่น rebase หรือ reset ให้แน่ใจว่างานของคุณถูกคอมมิทหรือ stash แล้ว
คำถามที่พบบ่อย
- ความต่างระหว่าง merge และ rebase คืออะไร?
- Merge รวมประวัติด้วยคอมมิทใหม่ที่มีสองผู้ปกครอง ส่วน Rebase เล่นคอมมิทของคุณทับบนอีกสาขาหนึ่ง สร้างประวัติแบบเส้นตรงแต่จะเขียนรหัสคอมมิทใหม่
- จะยกเลิกคอมมิทอย่างไร?
- ใช้ 'git revert <commit>' เพื่อเพิ่มคอมมิทใหม่ที่ยกเลิกการเปลี่ยนแปลง (ปลอดภัย รักษาประวัติร่วม) 'git reset' จะเขียนประวัติใหม่ จึงไม่ควรใช้บนสาขาแชร์
- ความต่างระหว่าง git pull และ git fetch คืออะไร?
- Fetch ดาวน์โหลดคอมมิทและการอ้างอิงจากรีโมตโดยไม่แตะต้นไม้การทำงาน Pull ทำ fetch แล้วจึง merge หรือ rebase การเปลี่ยนแปลงนั้นเข้าสาขาปัจจุบัน
- เมื่อไหร่ควรใช้ git stash?
- Stash เก็บการเปลี่ยนแปลงที่ยังไม่คอมมิทชั่วคราว เพื่อให้คุณสลับสาขาหรือดึงโค้ดได้สะอาด แล้วคืนค่าด้วย 'git stash pop' ภายหลัง