1
Git
jittagornp.me
2
จิตกร พิทักษ์เมธากุล (เดียร์)
โปรแกรมเมอร์
เขียน Blog jittagornp.me
facebook.com/jittagornp
facebook.com/jittagornp.me (Facebook Page)
jittagornp.me
3
jittagornp.me
4
TOO MANY FILES ?
jittagornp.me
5
v1.js
v2.js
2020-03-27.js
latest.js
last.js
final.js
final_v2.js
final_latest.js
latest
backup-27-03-20
เคยทำอะไรแบบนี้มั้ย ตั้งชื่อไฟล์เป็นชื่อต่าง ๆ เพื่อ Backup ข้อมูล
jittagornp.me
6
คำถาม
คิดว่าไฟล์ไหนคือไฟล์ล่าสุด
jittagornp.me
7
jittagornp.me
8
ต่อมา
jittagornp.me
9
คำถามต่อมา
jittagornp.me
10
คำถามต่อมา
jittagornp.me
11
Git
jittagornp.me
12
Git
เป็น Version Control System (VCS) ตัวนึง
ที่สามารถเอามาแก้ปัญหา ดังคำถาม ที่ถามไปแล้วได้
jittagornp.me
13
Git (ต่อ)
โดยมีความสามารถในการ
jittagornp.me
14
ความเป็นมา
jittagornp.me
15
ความเป็นมา
ในยุคริเริ่มการพัฒนาระบบปฏิบัติการ (Operation System) Linux
Linus Torvalds ผู้สร้าง Linux ได้พยายามมองหา Version Control System สักตัว เพื่อเอาไว้จัดการไฟล์ต่าง ๆ ของ Linux Project
ในปี 2002 (พ.ศ. 2545) Linus ได้นำ BitKeeper มาใช้
และใช้งานไปได้อย่างราบรื่น ตลอดระยะเวลา 3 ปี
jittagornp.me
16
ความเป็นมา (ต่อ)
ต่อมา ต้นปี 2005 Larry McVoy ซึ่งเป็นเจ้าขอ BitKeeper ได้ออกมาไวยวายว่า
มีคนเอา BitKeeper ไปทำ Reverse Engineering (คนทำชื่อ Andrew Tridgell)
เพื่อแกะดูว่า BitKeeper ทำงานยังไง
ทำให้ Larry McVoy ไม่พอใจ เลยยกเลิกสัญญาการใช้งานฟรี ให้แก่ชุมชน Linux
ตูเอง ตูขอโทษ
jittagornp.me
17
ความเป็นมา (ต่อ)
Linus เห็นดังนั้น จึงได้ตัดสินใจ
ทำระบบ Version Control System ขึ้นมาใช้งานเอง ในกลางปี 2005
ในตอนแรก เอามาใช้จัดการกับ Source Code ของระบบปฏิบัติการ Linux
โดยตั้งชื่อเครื่องมือตัวนี้ว่า Git
***** รูปไม่เกี่ยวน่ะ Linus เค้าด่า Nvidia (คนละเรื่องกัน เอารูปมาเฉย ๆ)
jittagornp.me
18
ความเป็นมา (ต่อ)
เมื่อพัฒนาไปได้สักระยะนึง
ก็ได้ยกหน้าที่ดูแล Git ให้กับ Junio Hamano เอาไปดูแลต่อ
ในปลายปี 2005 Git version 1.0 ก็ได้เปิดตัวให้ชาวโลกได้ใช้งานกัน
jittagornp.me
19
Repository
jittagornp.me
20
Repository
คือ ที่ ๆ ใช้สำหรับเก็บไฟล์
ของระบบ Git เช่น
jittagornp.me
21
Commit
jittagornp.me
22
Commit
Commit ID จะหน้าตาประมาณนี้�1d7e6271b468b884355ba1dc835ca02b1358acbe
jittagornp.me
23
File
jittagornp.me
24
File
Modify
jittagornp.me
25
File
Commit
Repository
Modify
jittagornp.me
26
File
1qa7848381...
Commit
File
Repository
Commit ID
Modify
jittagornp.me
27
File
1qa7848381...
4jshshdsa13...
Commit
File
Repository
Commit ID
Modify
jittagornp.me
28
File
1qa7848381...
4jshshdsa13...
4456dhaf221...
Commit
Repository
Commit ID
Modify
jittagornp.me
29
HEAD
jittagornp.me
30
HAED
คือ Pointer ที่ชี้ไปยัง Commit ต่าง ๆ
โดยปกติ
HEAD จะชี้ไปที่ Commit ล่าสุดเสมอ (ถ้าไม่มีการย้อน Commit)
jittagornp.me
31
commit 1qa7848381… (ล่าสุด)
commit 3232443440...
commit 1189897241...
commit 4359878431...
HEAD
jittagornp.me
32
File Life Cycle
jittagornp.me
33
File Life Cycle
Untracked
คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git
Tracked
คือ ไฟล์ที่ถูก Track แล้ว โดย Git
jittagornp.me
34
File
File
File
File
Untracked
Tracked
jittagornp.me
35
File Life Cycle
Untracked
คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git
Tracked
คือ ไฟล์ที่ถูก Track แล้ว โดย Git
jittagornp.me
36
File
File
File
File
Untracked
Tracked
Staged
Unstage
jittagornp.me
37
File Life Cycle
Untracked
คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git
Tracked
คือ ไฟล์ที่ถูก Track แล้ว โดย Git
jittagornp.me
38
File
File
File
File
Untracked
Tracked
Staged
Committed
Modified
Unstage
jittagornp.me
39
Working Directory
Staging Area
Repository
jittagornp.me
40
Working Directory
Staging Area
Repository
Untracked
jittagornp.me
41
Working Directory
Staging Area
Repository
Untracked
Staged
jittagornp.me
42
Working Directory
Staging Area
Repository
Untracked
Staged
Commited
jittagornp.me
43
Working Directory
Staging Area
Repository
Untracked
Staged
Commited
Modified
jittagornp.me
44
Working Directory
Staging Area
Repository
Untracked
Staged
Commited
Modified
Staged
jittagornp.me
45
Working Directory
Staging Area
Repository
Untracked
Staged
Commited
Modified
Staged
Commited
jittagornp.me
46
Distributed
(การจัดเก็บข้อมูลการแก้ไขแบบกระจาย)
jittagornp.me
47
Git มีคุณสมบัติเป็น Distributed คือ
ไม่ได้จัดเก็บข้อมูลประวัติการแก้ไขไฟล์ไว้แค่ที่เดียว
แต่เก็บเป็นแบบกระจาย เพื่อป้องกันปัญหา Single Point of Fail
คือ ต่อให้ Repository ต้นทางที่เก็บข้อมูล ได้รับความเสียหาย
ก็ยังสามารถที่จะเรียกคืนประวัติการแก้ไขไฟล์จาก Repository อื่น ๆ ได้
jittagornp.me
48
Centralized vs Distributed
Remote Server
Repo
PC
PC
PC
Repo
Repo
Repo
Centralized Server
Repo
PC
PC
PC
Repo
Repo
Repo
jittagornp.me
49
Infrastructure
jittagornp.me
50
Repo
Repo
Repo
Repo
Repo
Remote Repository
Remote Repository
Local Repository
Client
Remote Server
jittagornp.me
51
Branch
jittagornp.me
52
Git Branch
เป็นคุณสมบัติเด่นอย่างนึงของ Git เลยก็ว่าได้ คือ
เราสามารถแตก Branch ออกมาจาก Code ตัวหลัก (Master)
เพื่อพัฒนา Feature บางอย่างเป็นแบบขนาน (Paralell)
ไปพร้อมกับตัวหลัก (Master) ได้
เมื่อพัฒนาเสร็จแล้ว
ก็ค่อยรวม (Merge) กลับ
ไปที่ Master เหมือนเดิม
New Feature
Master
jittagornp.me
53
jittagornp.me
54
Remote Repository
(Git Hosting)
jittagornp.me
55
Git Hosting (Remote Repository)
เป็น Server ที่ทำหน้าที่ ให้บริการฝากไฟล์ Git
ซึ่งเราสามารถนำ Source Code ต่าง ๆ ของเรา
ไปเก็บไว้บน Git Hosting ได้
มีทั้ง แบบเสียตังค์ และแบบที่ให้ใช้งานฟรี
มีให้เลือกใช้หลากหลาย Brand
แต่ละ Brand ก็มีจุดเด่น และจุดด้อยแตกต่างกันไป
ทั้งนี้ ขึ้นอยู่ที่ตัวเรา ว่าจะเลือกใช้ตัวไหน
Repo
Remote Repositories
Repo
Repo
Repo
Repo
Repo
jittagornp.me
56
Git Hosting
jittagornp.me
57
Git Hosting
การเลือกใช้ Git Hosting แนะให้อ่านบทความนี้
ศึก Git Hosting ฟรี การปะทะของ GitHub, Bitbucket, GitLab และ Azure DevOps ใช้ตัวไหนดี โดย Nuuneoi
https://nuuneoi.com/blog/blog.php?read_id=956
jittagornp.me
58
Installation
jittagornp.me
59
jittagornp.me
60
Command Line
jittagornp.me
61
Command Line
jittagornp.me
62
Git Version
jittagornp.me
63
Git Version
สำหรับดู Version ของ Git
�$ git version
jittagornp.me
64
Git Help
jittagornp.me
65
Git Help
สำหรับดูคำสั่ง Git ว่ามีคำสั่งหรือ Options อะไรใช้ได้บ้าง
�$ git help
$ git help <COMMAND_NAME>
jittagornp.me
66
jittagornp.me
67
Git Config
jittagornp.me
68
Git Config
ใช้สำหรับกำหนดค่า Config ค่าต่าง ๆ ให้ Git
เช่น การกำหนด ชื่อ และ email ของผู้ใช้งาน��$ git config --global user.name "YOUR_NAME"
$ git config --global user.email "YOUR_EMAIL@email.com"
jittagornp.me
69
Git Config (ต่อ)
ดูว่ามีค่า Config อะไรบ้าง
�$ git config --list //เฉพาะ Repository นี้
$ git config --global --list //ทั้งหมด
jittagornp.me
70
jittagornp.me
71
Git Config (ต่อ)
สำหรับ Save Username และ Password ของ Repository นั้น ๆ
�$ git config credential.helper store �
��สำหรับ Save Username และ Password ของทุก ๆ Repository
�$ git config --global credential.helper store
jittagornp.me
72
Git Init
jittagornp.me
73
Git Init
ใช้สำหรับ Initial (สร้าง) Git Repository
$ git init
เมื่อ Run คำสั่งเสร็จ
จะมี Folder .git ถูกสร้างขึ้นมาใน Folder ที่เรา Run คำสั่งนี้
ข้างใน .git เป็นพื้นที่เก็บข้อมูลต่าง ๆ ของ Repository นี้
jittagornp.me
74
jittagornp.me
75
Git Status
jittagornp.me
76
Git Status
ใช้สำหรับตรวจสอบสถานะของไฟล์ต่าง ๆ ใน Repository
ซึ่งจะแสดงผลสถานะ ตามหัวข้อ File Life Cycle ที่ผ่านมา
$ git status
jittagornp.me
77
jittagornp.me
78
Git Add
jittagornp.me
79
Git Add
ใช้สำหรับ Add ไฟล์ เข้า Staging Area
หรือ ใช้สำหรับเปลี่ยนสถานะของไฟล์จากสถานะ “Untracked => Staged” เพื่อให้ Git เริ่ม Track ไฟล์นี้นั่นเอง
$ git add . //ทุกไฟล์ภายใต้ Current Path
$ git add <FILE_NAME> //เฉพาะเจาะจงไฟล์
jittagornp.me
80
Working Directory
Staging Area
Repository
Untracked
Staged
git add <FILE>
jittagornp.me
81
Git Add (ต่อ)
นอกจากจะเปลี่ยนสถานะจาก Untracked => Staged แล้ว
ยังใช้เปลี่ยนสถานะจาก Unstaged อื่น ๆ ไปยังสถานะ Staged อีกด้วย เช่น
Modified => Staged
jittagornp.me
82
Working Directory
Staging Area
Repository
Untracked
Staged
git add <FILE>
Modified
Staged
jittagornp.me
83
jittagornp.me
84
Git Remove
jittagornp.me
85
Git Remove
ใช้สำหรับลบไฟล์ออกจาก Staging Area
ทำให้ไฟล์นั้นกลับมาอยู่ในสถานะ Untracked อีกครั้ง
$ git rm --cached <FILE>
jittagornp.me
86
Working Directory
Staging Area
Repository
Untracked
Staged
git rm --cached <file>
jittagornp.me
87
jittagornp.me
88
Git Commit
jittagornp.me
89
Git Commit
ใช้สำหรับ Save ไฟล์ หรือย้ายไฟล์จาก Staging Area => Repository
เมื่อเราทำการ Commit ไฟล์จะถูกเก็บเป็นประวัติไว้ 1 ชุด
โดยมีหมายเลข Commit ID กำกับไว้
$ git commit -m “<COMMIT_MESSAGE>”
jittagornp.me
90
Working Directory
Staging Area
Repository
Staged
Commited
git commit -m “MESSAGE”
jittagornp.me
91
jittagornp.me
92
Git Log
jittagornp.me
93
Git Log
ใช้สำหรับดูประวัติการ Commit ต่าง ๆ ที่ผ่านมา ว่าทำอะไรไปบ้าง
$ git log
ออกจาก log ให้กด q
jittagornp.me
94
jittagornp.me
95
Git Diff
jittagornp.me
96
Git Diff (Difference)
ใช้สำหรับเช็คว่า มีอะไรเปลี่ยนแปลงไปบ้าง เพิ่ม ลบ แก้ไข อะไรไปบ้าง
เมื่อเทียบกับ Commit ที่ผ่านมา
$ git diff <COMMIT_ID>
$ git diff <COMMIT_ID> <COMMIT_ID>
$ git diff HEAD
jittagornp.me
97
jittagornp.me
98
Git Reset
jittagornp.me
99
Git Reset
ใช้สำหรับการ Reset State ไฟล์ไปที่ State ใด ๆ
เช่น ถ้าเราเผลอลบไฟล์สักไฟล์นึงทิ้ง แล้วต้องการเอากลับมาคืน
$ rm README.md //ลบไฟล์
$ git add README.md //สมมติว่าเผลอ Add ไปทำให้พร้อม Commit
เอากลับมาให้ทำดังนี้
$ git reset HEAD README.md //เปลี่ยนจาก Staged => Unstaged
$ git checkout README.md //กู้คืนไฟล์
jittagornp.me
100
Git Reset (ต่อ)
ทำการย้อน Commit ไปยัง Commit ต่าง ๆ
$ git reset --soft <COMMIT_ID> //ยังคงสถานะไฟล์เป็น Staged ไว้
jittagornp.me
101
Git Branch
jittagornp.me
102
Git Branch
เป็นคำสั่งที่ใช้ทำงานร่วมกับ Branch
เช่น ดูว่า Repository นี้มี Branch อะไรบ้าง
$ git branch
jittagornp.me
103
Git Branch (ต่อ)
ถ้าต้องการสร้าง Branch ใหม่ ให้ใช้คำสั่งนี้
$ git branch <BRANCH_NAME>
เช่น ทำการสร้าง Branch ชื่อ v2
$ git branch v2
jittagornp.me
104
Git Branch (ต่อ)
ถ้าต้องการลบ Branch ให้ใช้คำสั่งนี้
$ git branch -d <BRANCH_NAME>
เช่น ทำการลบ Branch ชื่อ v2
$ git branch -d v2
jittagornp.me
105
Git Checkout
jittagornp.me
106
Git Checkout
ใช้สำหรับเปลี่ยน Branch
หรือ ย้อนกลับไปยังตำแหน่งที่เราเคย Commit ไว้
$ git checkout <BRANCH_NAME>
$ git checkout <COMMIT_ID>
$ git checkout <FILE_NAME>
jittagornp.me
107
Git Checkout (ต่อ)
เช่น
$ git checkout master //เปลี่ยนไปที่ Branch master
$ git checkout 1d7e6271b468b884355ba1dc835ca02b1358acbe
//ย้อนไปที่ Commit ID : 1d7e6271b468b884355ba1dc835ca02b1358acbe
$ git checkout file1.txt //ย้อนไฟล์กลับไปยังตำแหน่งก่อนที่จะแก้ไข
jittagornp.me
108
jittagornp.me
109
Git Merge
jittagornp.me
110
Git Merge
ใช้สำหรับรวม Branch เข้าด้วยกัน
$ git merge <BRANCH_NAME>
เช่น ต้องการรวม Branch master เข้ากับ Branch v2
$ git checkout master //เปลี่ยน Branch ไปที่ Master ก่อน
$ git merge v2
jittagornp.me
111
Git Remote
jittagornp.me
112
Git Remote
ใช้สำหรับทำงานร่วมกับ Remote Repository
ดูว่า Local Repository นี้ เชื่อมต่อหรือมี Remote Repository อะไรบ้าง
$ git remote -v
jittagornp.me
113
Git Remote (ต่อ)
สำหรับเพิ่ม Remote Repository URL เข้าไปใน Local Repository
$ git remote add <NAME> <REMOTE_URL>
เช่น
$ git remote add origin https://github.com/jittagornp/vue.git
$ git remote -v
jittagornp.me
114
Git Remote (ต่อ)
สำหรับเปลี่ยน Remote Repository URL
$ git remote set-url <NAME> <NEW_REMOTE_URL>
เช่น
$ git remote set-url origin https://github.com/jittagornp/react.git
$ git remote -v
jittagornp.me
115
Git Clone
jittagornp.me
116
Git Clone
ใช้สำหรับ Clone Remote Repository
มาไว้ในเครื่องเรา (ยกมาทั้งชุด)
$ git clone <REMOTE_URL>
เช่น
$ git clone https://github.com/vuejs/vue.git
Repo
Repo
Remote Repository
Local Repository
Clone
jittagornp.me
117
jittagornp.me
118
Git Fetch
jittagornp.me
119
Git Fetch
ใช้สำหรับ Check Local Repository กับ Remote Repository
ว่าตรงกันหรือไม่ มีอะไรเปลี่ยนแปลงมั้ย โดยไม่ดึง Code ลงมา
$ git fetch
jittagornp.me
120
Git Pull
jittagornp.me
121
Git Pull (Fetch + Merge)
ใช้สำหรับ ดึง Update Code จาก Remote Repository
ลงมา และ (Auto) Merge เข้ากับ Local Repository
$ git pull <REMOTE_URL> <BRANCH_NAME>
$ git pull
Repo
Repo
Remote Repository
Local Repository
Pull
jittagornp.me
122
Git Push
jittagornp.me
123
Git Push
ใช้สำหรับโยน Code จาก Local Repository
หลังจากที่เรา Commit แล้ว
ขึ้นไป Merge เข้ากับ Remote Repository
$ git push <REMOTE_URL> <BRANCH_NAME>
$ git push
Repo
Repo
Remote Repository
Local Repository
Push
jittagornp.me
124
Git Tag
jittagornp.me
125
Git Tag
เป็นคำสั่งที่ใช้สำหรับสร้าง Tag หรือกำหนด Tag
ให้กับ Commit เพื่อเอาไว้ระบุจุดที่ต้องการ Freeze หรือ Release Source Code
เช่น เราพัฒนาระบบมาถึงจุด ๆ นึงที่เราคิดว่า OK แล้ว (ตาม Milestone ที่ตั้งไว้)
เราอยากที่จะ Release Code นั้นออกมาเป็น Version RELEASE-1.0.0
ให้เราทำดังนี้
jittagornp.me
126
Git Tag (ต่อ)
สร้าง Tag ที่ต้องการ Release (จะสร้างเป็นชื่ออะไร รูปแบบไหนก็ได้)
$ git tag -a <TAG_NAME> -m “<MESSAGE>”
$ git tag -a RELEASE-1.0.0 -m “Some Message”
ตอน Push Code ให้ Push ไปที่ Tag แทนการ Push ไปที่ Branch ดังนี้
$ git push origin --tag RELEASE-1.0.0
jittagornp.me
127
Git Tag (ต่อ)
ใน Git Remote Repository หรือ Git Hosting
จะมีการ Freeze Code เป็น Release Tag ไว้
ถ้าใน GitHub จะเห็น Release ขึ้นที่หน้า Repository ดังนี้
jittagornp.me
128
Git Tag (ต่อ)
jittagornp.me
129
Git Tag (ต่อ)
กรณีที่เราต้องการดึง (Checkout) Tag นั้น ๆ กลับมา ให้ทำดังนี้
$ git checkout tags/<TAG_NAME>
เช่น
$ git checkout tags/RELEASE-1.0.0
jittagornp.me
130
Git Tag (ต่อ)
คำสั่งอื่น ๆ ของ Tag
$ git tag //แสดง Tag ทั้งหมด
$ git tag --delete <TAG_NAME> //ลบ Tag ที่ Local Repository
$ git push --delete origin <TAG_NAME> //ลบ Tag บน Remote Repository
jittagornp.me
131
Git Clean
jittagornp.me
132
Git Clean
ใช้สำหรับทำงานกับไฟล์ที่อยู่ในสถานะ Untracked
$ git clean -n //แสดงไฟล์ทั้งหมดที่อยู่ในสถานะ Untracked
$ git clean -df //ลบไฟล์ที่อยู่ในสถานะ Untracked ออก
jittagornp.me
133
GUI
jittagornp.me
134
GUI
ถ้าใครขี้เกียจใช้งาน Command Line
สามารถ Download Git แบบ GUI ได้ที่
https://git-scm.com/downloads/guis
jittagornp.me
135
Git Ignore
jittagornp.me
136
Git Ignore
เป็นกรรมวิธีที่ใช้สำหรับบอก Git ว่า
ไม่ต้องสนใจ ไม่ต้องไปยุ่งอะไรกับ List ไฟล์ ที่ถูกเขียนไว้ในไฟล์ .gitignore
ปกติไฟล์ .gitignore จะวางไว้ใน Level เดียวกันกับ Folder .git (Repository)
jittagornp.me
137
Git Ignore (ต่อ)
ตัวอย่าง .gitignore (Git ไม่ต้อง Track ไม่ต้องยุ่งอะไรกับไฟล์พวกนี้)
/target/
/.settings/
/node_modules/
.classpath
.factorypath
.project
.nb-configuration.xml
y
jittagornp.me
138
Conflict
jittagornp.me
139
Conflict
คือ การที่มีคนพยายามแก้ไขไฟล์เดียวกัน พร้อมกัน (Comit พร้อมกัน)
เลยทำให้เกิด Conflict (การขัดแย้งกัน) ขึ้น
jittagornp.me
140
Conflict (ต่อ)
ซึ่งมันจะขึ้นประมาณนี้ ถ้าเรา Pull Code ลงมา
<<<<<<< HEAD
This is my Code on Local Repository
=======
Last Update on Remote Repository
>>>>>>> <COMMIT_ID>
ให้ทำการแก้ไขให้ถูกต้อง โดยเลือกว่าจะเอาอันไหน หรือแก้เป็นแบบไหน
Code เราที่กำลังแก้
อยู่ที่ Local Repository เรา
Code คนอื่นที่แก้ แล้ว Push ไปไว้ที่ Remote Repository แล้ว
jittagornp.me
141
Conflict (ต่อ)
เมื่อแก้เสร็จ
ก็ให้ทำการ Add, Commit แล้ว Push กลับไปเหมือนเดิม
$ git add <FILE_NAME> //เปลี่ยน จาก Unstaged => staged เพื่อรอ Commit
$ git commit -m “<COMMIT_MESSAGE>”
$ git push origin master//โยน Code ขึ้น Remote Repository
jittagornp.me
142
เรียนรู้เพิ่มเติม
jittagornp.me
143
เรียนรู้เพิ่มเติม
jittagornp.me
144
สำหรับเพื่อน ๆ คนไหน ที่ชื่นชอบบทความ
และอยากจะสนุนสนันค่ากาแฟเล็ก ๆ น้อย ๆ
สามารถคลิกที่ลิงค์นี้เพื่อไปยังหน้า Buy me a coffee ได้ครับ
jittagornp.me