Download - วิธีใช้ Git เบื้องต้น
![Page 1: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/1.jpg)
วธิใีช้ GIT เบื้องต้น
![Page 2: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/2.jpg)
สำ�หรบัผมท่ีเขยีนแอปพลิเคชัน่ม�ซกัระยะหน่ึงก็มโีอก�สได้ใชG้ITบอ่ยนะครบั ผมจงึขอนิย�ม
มนัว�่ตัวชว่ยในก�รจดัก�รกับเวอรช์ัน่ต่�งๆของโปรแกรม โดยเวล�ท่ีเร�เขยีนโปรแกรมอะไรซกัตัว เวล�ท่ีเร�ต้องก�รbackup ตัวโปรแกรมของเร� เร�ก็จะใชว้ธิ ีcopy/past
ลงในคอมพวิเตอรข์องเร�ใชไ่หมครบั
![Page 3: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/3.jpg)
แต่ถ้�อยูดี่ๆถ้�ฮ�รด์ดิสของเร�มปัีญห�เร�คงไมส่�ม�รถนำ�โปรแกรมท่ีเขยีนออกม�ใชไ้ด้ และในก�รทำ�ง�นเป็นทีมท่ีเร�ต้องทำ�ง�นรว่มกับ
คนอ่ืนเร�คงไมอ่ย�กม�นัง่ก็อปลงแฟลชไดรฟ์หรอืสง่เมล์ใชไ่หมครบั
![Page 4: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/4.jpg)
Git (ในคว�มเข�้ใจของผม) ก็เป็นเหมอืนตัวกล�งท่ีคอยชว่ยเร�ในก�รเก็บไฟล์ขอ้มูล โดยแยกเป็นแต่ละเวอรช์ัน่ และชว่ยใหเ้ร�ส�ม�รถทำ�ง�นรว่มกับคนอ่ืนได้ง่�ยขึ้น
![Page 5: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/5.jpg)
แนะนำ�ใหอ่้�นเพิม่เติมท่ีDevAhoyGitBook
![Page 6: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/6.jpg)
แต่ถ้�จะใหเ้ข�้ใจมนัอย�่งถ่องแท้ เร�ก็ม�ลองใช้ง�นมนัดจูรงิๆเลยดีกว�่ก่อนอ่ืนก็ไปด�วน์โหลดท่ี
https://www.git-scm.com/
![Page 7: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/7.jpg)
เมื่อทำ�ก�รลงโปรแกรมเสรจ็เรยีบรอ้ยแล้วก็ดับเบิล้คลิกท่ีตัวgit-bashได้เลย
![Page 8: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/8.jpg)
เมื่อเปิดม�หน้�ต�จะเป็นแบบนี้ ขนัตอนแรกเร�ต้องกำ�หนดชื่อ และอีเมล์ ซะก่อน
![Page 9: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/9.jpg)
![Page 10: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/10.jpg)
![Page 11: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/11.jpg)
ทำ�ก�รเชค็โดยพมิพ์git config --list
![Page 12: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/12.jpg)
โดยในตัวอย�่งน้ีผมจะสร�้งโฟลเดอรไ์วอั้นหนึ่งนะครบัชื่อว�่ GitTest โดยจะมไีฟล์ v1.txt
![Page 13: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/13.jpg)
![Page 14: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/14.jpg)
ก่อนอ่ืนผมทำ�ก�รเข�้ไปท่ีไดรฟD์ ท่ีไดเรก็ทอรีช่ื่อ GitTestสำ�หรบัใครท่ีเข�้ผิดส�ม�รถใชค้ำ�สัง่ cd.. เพื่อยอ้นกลับ หรอื cd\ เพื่อกลับไปที่ Root directory ได้นะครบั
![Page 15: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/15.jpg)
ต่อม�เร�จะทำ�ก�รสร�้ง git repository ขึ้นม�
![Page 16: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/16.jpg)
เปิด Show hidden files ก่อนนะครบั
![Page 17: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/17.jpg)
จะได้โฟลเดอร ์.git แบบนี้
![Page 18: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/18.jpg)
สถ�นะต่�งๆของ git มอีะไรบ�้ง
![Page 19: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/19.jpg)
Untracked ไฟล์ยงัไมถ่กู track โดยgit (ไฟล์ท่ีสรา้งใหม)่
Unmodified ไฟล์อยูใ่นgitเรยีบรอ้ย และไมม่กีารปรบัแต่ง
Modified ไฟล์มกีารปรบัแต่งแต่ยงัไมไ่ด้ commit
Staged ไฟล์ท่ีอยูใ่นindex คือไฟล์ท่ีพรอ้มจะcommit
ลง Repository
![Page 20: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/20.jpg)
เมอืเร�พมิพค์ำ�สัง่ git status เร�จะเหน็ว�่เจ�้ v1.txtอยูใ่นสถ�นะ untracked
![Page 21: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/21.jpg)
สิง่แรกท่ีผมทำ�คือทำ�ก�รเพิม่ v1 เข�้ไปใน git Repository (* ใชใ้นกรณีท่ีจำ�น�มสกลุไฟล์ไมไ่ด้)
![Page 22: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/22.jpg)
เมื่อทำ�ก�รเชค็จะเหน็ว�่ v1.txt อยูใ่นสถ�นะ Unmodified เรยีบรอ้ย
![Page 23: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/23.jpg)
แต่ถ้�ในกรณีท่ีเร�มไีฟล์เยอะแบบน้ี เร�คงไมอ่ย�กม�นัง่เพิม่ทีละไฟล์หรอกนะครบั
![Page 24: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/24.jpg)
ท�ง git เลยมคีำ�สัง่พเิศษท่ีใชส้ำ�หรบัเพิม่ไฟล์ท่ีอยูใ่นสถ�นะUntracked ทัง้หมด คือ git add -A
![Page 25: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/25.jpg)
![Page 26: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/26.jpg)
ในกรณีท่ีเร�ต้องก�ร untracked ไฟล์ เร�ส�ม�รถทำ�ได้โดยก�รใชค้ำ�สัง่git rm –cached ต�มด้วยชื่อไฟล์
![Page 27: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/27.jpg)
![Page 28: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/28.jpg)
ขัน้ต่อไปคือก�รcommit file โดยผมจะลบไฟล์ทัง้หมดท้ิงเหลือแค่
ไฟล์ v1.txt เท่�นัน้
![Page 29: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/29.jpg)
ไฟล์จะอยูใ่นสถ�นะ deleted นะครบั เร�ก็ใชค้ำ�สัง่ git add --update เพื่ออัพเดทไฟล์นะครบั
![Page 30: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/30.jpg)
ต่อไปก็ใชค้ำ�สัง่ commit -m ต�มด้วยขอ้คว�มท่ีใชร้ะบุเชน่เร�ได้เพิม่อะไรไปบ�้งในก�รcommit
![Page 31: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/31.jpg)
![Page 32: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/32.jpg)
และก็ใชค้ำ�สัง่ git log เพื่อดวู�่เร�ได้commit อะไรไปบ�้ง
![Page 33: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/33.jpg)
หลังจ�กนัน้ลองใชค้ำ�สัง่ git status ดจูะเหน็ว�่ไฟล์v1.txt ได้ห�ยไปแล้ว ก็คือเร�ได้commitเรยีบรอ้ยแล้ว
![Page 34: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/34.jpg)
ต่อม�ผมได้เพิม่ไฟล์ v2.txt
![Page 35: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/35.jpg)
และได้commit เข�้สู ่git แต่ถ้�ผมต้องก�รท่ีจะดึง version เก่�ม�ใช้ผมส�ม�รถทำ�ได้โดยก�รนำ�เลข checksum เพื่ออ้�งอิงถึงversion ท่ีผมต้องก�ร
![Page 36: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/36.jpg)
แบบนี้
![Page 37: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/37.jpg)
กลับม�แล้วว
![Page 38: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/38.jpg)
v1 v2
แต่ถ้�เร�ขีเ้กียจใชว้ธิแีรก ก็ส�ม�รถใชค้ำ�สัง่
git checkout HEAD~1 (โดยตัวเลขก็นับยอ้นหลังไปนะ
ครบั ว�่จะกลับไปก่ีเวอรช์ัน่)
![Page 39: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/39.jpg)
ก�รcheckout ไมใ่ชก่�รลบนะครบัเพยีงแต่เป็นก�รยอ้นกลับไปเท่�นัน้
แต่ถ้�เร�ยอ้นกลับไปและทำ�ก�รcommitละ
![Page 40: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/40.jpg)
สร�้งไฟล์ v3.txt ขึ้นม�แล้วcommit ซะเลย
![Page 41: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/41.jpg)
v1 v2
ไฟล์ v2 จะยงัคงอยูน่ะครบัเพยีงแต่เร�ทำ�ก�รแตก branch
ออกไป ก็เหมอืนเร�สร�้งโลกคู่ขน�นขึ้นม�อีกโลกหน่ึงโดยไมว่�่เร�จะทำ�อะไร ก็ไมม่ผีลกับโลก
จรงิของเร�v3
![Page 42: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/42.jpg)
โดยเร�ส�ม�รถเชค็ได้นะครบัว�่ตอนน้ีเร�อยูโ่ลกไหนด้วยก�ร
พมิพ์
![Page 43: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/43.jpg)
ถ้�ต้องก�รกลับไปยงัโลกจรงิก็ส�ม�รถใชค้ำ�สัง่
git checkout master ได้เลย
![Page 44: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/44.jpg)
v1 v2
เร�จะกลับไปยงั v2 นะครบั และถ้�เพื่อนๆลองใชค้ำ�สัง่ git branch
ดก็ูจะไมเ่จอเจ�้ v3 นะครบั
v3
![Page 45: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/45.jpg)
v1 v2
เพร�ะเร�ไมไ่ด้สร�้ง branch ไวที้่ v2 แต่สร�้งไวท่ี้ v1 ต่�งห�ก ถ้�ต้องก�รไปยงัv3 ก็ใหก้ลับไปยงั
v1 ก่อนและก็ใชค้ำ�สัง่ checkout เหมอืนเดิม
v3
![Page 46: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/46.jpg)
หลังจ�กท่ีเร�ได้ลองสร�้ง branch จ�กก�ร commit ไปแล้ว แต่ถ้�เร�ต้องก�รสร�้ง branch โดย
กำ�หนดเองในกรณีท่ีอย�กทดลองอะไรเล่นๆละ
![Page 47: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/47.jpg)
ส�ม�รถทำ�ได้โดยใชค้ำ�สัง่ git branch ต�มด้วยชื่อลองเชค็ดก็ูจะได้แบบนี้ครบั
![Page 48: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/48.jpg)
ก�รอัพ repository ขึ้น github
![Page 49: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/49.jpg)
หลังจ�กท่ีเร�ได้เรยีนรูก้�รใช ้git เบื้องต้นกันไปแล้วต่อไปเร�ก็จะทำ�ก�รอัพ repository ขึ้น githubในกรณีท่ีต้องก�รทำ�ง�นเป็นทีมนะ
ครบั(จะใชตั้วอ่ืนก็ได้นะ)
![Page 51: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/51.jpg)
![Page 52: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/52.jpg)
ตัง้ชื่อและก็กำ�หนดร�ยระเอียดให้เรยีบรอ้ย
ถ้�ต้องก�รกำ�หนดเป็น private ต้องเสยีเงินนะครบั
![Page 53: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/53.jpg)
ก่อนจะ push ก็ทำ�ก�รเชค็ก่อนว�่ เรยีบรอ้ยดีแล้ว
จ�กนัน้ก็ใส ่username password
![Page 54: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/54.jpg)
เสรจ็แล้ว จะเหน็ว�่ท่ีเร� commit หรอืสร�้ง branch มกี�รแจง้ไวด้้วย
![Page 55: วิธีใช้ Git เบื้องต้น](https://reader031.vdocuments.pub/reader031/viewer/2022020119/589f3cb11a28ab490c8b554b/html5/thumbnails/55.jpg)
จบแล้วครบั ถ้�ใครชอบหรอืไม่ชอบ(ชอบเหอะ) ส�ม�รถติดต�ม
ได้ท่ี www.droid-wizard.blogspot.com