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