สรุป Git (command line) พื้นฐานที่ใช้บ่อยสุด

ODENZA
2 min readDec 22, 2020

--

ในบทความนี้ มีเนื้อหาเกี่ยวกับ Git command line พื้นฐานที่ใช้บ่อยๆ บางคนอาจจะรู้แล้วแต่พอไม่ได้ใช้มันก็ลืม ขอให้บทความนี้เป็นเหมือนบันทึก ไว้อ่านเวลาที่ลืมแล้วกันนะครับ

git: https://git-scm.com/

Git command line basics ดังนี้

  • 1–8 : Basics in local repository
  • 9–14 : List, create, delete branch
  • 15–17 : push & pull between local and remote repository
1. git init                     
2. git add <file name>
3. git add .
4. git status
5. git commit -m “description”
6. git commit -a -m “description”
7. git log
8. git checkout <commit number>
9. git branch
10. git branch <branch-name>
11. git checkout <branch-name>
12. git checkout -b <branch-name>
13. git branch -d <branch-name>
14. git merge <branch-name>
15. git push origin <branch-name>
16. git clone
17. git pull
18. rm -rf .git

เปิด Terminal (Mac OS) มา แล้ว cd ไปที่ folder ของ project

  1. git init : Create an empty local Git repository
    สร้าง git เปล่าๆขึ้นมาให้กับ project งานของเรา
    (Command+Shift+Dot เพื่อดู hidden file ที่ซ่อนอยู่)
  2. git add <file name> : Add file contents to the index(repository)
    add file เข้าไปใน git ที่เรา init ขึ้นมา
  3. git add . : Add all file
    add file ของ project ทั้งหมด
  4. git status : Show the working tree status
    แสดงสถานะการทำงานว่ามีการแก้ไข หรือ ลบ อะไรต่างๆ
  5. git commit -m “description” : Record changes to the repository
    (m ย่อมาจาก message)
    บันทึกการเปลี่ยนแปลงการทำงานพร้อมกับระบุเนื้อหานั้นได้โดยใส่ description
  6. git commit -a -m “description” : Add file and record changes to the repository (a: add, m: message)
    เพิ่มและบันทึกการเปลี่ยนแปลงพร้อมกับระบุเนื้อหา
  7. git log : Show commit logs(commit history)
    แสดงประวัติที่เคย commit ไป
  8. git checkout <commit number> : Back to old version of commit
    กลับไป commit ก่อนๆ โดยใส่ commit number
  9. git branch : Show list of branch
    แสดงสาขาหรือ branch ที่มีอยู่
  10. git branch <branch-name> : Create new a branch with name
    สร้าง branch ใหม่
  11. git checkout <branch-name> : Switch branch to <branch-name>
    สลับ branch เพื่อไปทำงานที่ <branch-name>
  12. git checkout -b <branch-name> : Create & Switch branch
    สร้าง branch ใหม่และสลับในคราวเดียวกัน
  13. git branch -d <branch-name> : Delete <branch-name> (d=delete)
    ลบ <branch-name>
  14. git merge <branch-name> : Join two or more development histories together
    รวม branch เข้าด้วยกัน
  15. git push origin <branch-name> : Push the branch to remote repository(=upload)
    upload file จาก local ไป remote(server)
  16. git clone : Clone a repository into a new directory(=download)
    การคัดลอก project จาก remote มายัง local
  17. git pull : Pull latest from remote repo (=download change only)
    ดึง file จาก remote มายัง local เพื่อทำงานกับ project นั้นต่อไป
  18. rm -rf .git: remove git

--

--

ODENZA
ODENZA

No responses yet