1 of 144

1

Git

jittagornp.me

2 of 144

2

จิตกร พิทักษ์เมธากุล (เดียร์)

โปรแกรมเมอร์

เขียน Blog jittagornp.me

facebook.com/jittagornp

facebook.com/jittagornp.me (Facebook Page)

jittagornp.me

3 of 144

3

jittagornp.me

4 of 144

4

TOO MANY FILES ?

jittagornp.me

5 of 144

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 of 144

6

คำถาม

คิดว่าไฟล์ไหนคือไฟล์ล่าสุด

  • last.js
  • latest.js
  • final_v2.js
  • final_latest.js
  • หรือ จริง ๆ แล้ว ไฟล์ล่าสุดอยู่ใน Folder latest กันแน่

jittagornp.me

7 of 144

7

jittagornp.me

8 of 144

8

ต่อมา

jittagornp.me

9 of 144

9

คำถามต่อมา

  • จำได้มั้ยว่าในแต่ละครั้งที่แก้ไป เราแก้อะไรไปบ้าง แล้วแก้ไปเมื่อไหร่
  • สมมติถ้าแก้ผิด อยากที่จะย้อนกลับไปที่จุดเดิมก่อนแก้ เราทำยังไง
  • งาน ๆ นึง ทำร่วมกันหลายคน ช่วยกันแก้ เรา Control กันยังไง

jittagornp.me

10 of 144

10

คำถามต่อมา

  • จะรู้ได้ยังไงว่า ไฟล์ที่เรากำลังแก้อยู่ ไม่ไปทับกับของคนอื่น
  • อยากเพิ่ม Feature ใหม่ลงไปใน Project แต่ก็กลัวกระทบกับของเดิมที่ทำเสร็จไปแล้ว เราแก้ปัญหานี้ยังไง ?
  • ถ้าเครื่องพัง ไฟล์หาย เราทำยังไง ?
  • อื่น ๆ

jittagornp.me

11 of 144

11

Git

jittagornp.me

12 of 144

12

Git

เป็น Version Control System (VCS) ตัวนึง

ที่สามารถเอามาแก้ปัญหา ดังคำถาม ที่ถามไปแล้วได้

jittagornp.me

13 of 144

13

Git (ต่อ)

โดยมีความสามารถในการ

  • จัดเก็บประวัติการแก้ไขเปลี่ยนแปลง ของไฟล์ต่าง ๆ ใน Project
  • สามารถเรียกคืนประวัติการแก้ไขไฟล์ ย้อนหลังได้
  • สามารถ Backup ไฟล์ได้
  • สามารถใช้พัฒนา Feature ต่าง ๆ แบบขนาน (Parallel) กันไปได้
  • ใช้ทำงานร่วมกันเป็นทีมได้
  • มีความสามารถในการจัดเก็บข้อมูลการแก้ไขไฟล์ แบบ Distributed
  • และอื่น ๆ

jittagornp.me

14 of 144

14

ความเป็นมา

jittagornp.me

15 of 144

15

ความเป็นมา

ในยุคริเริ่มการพัฒนาระบบปฏิบัติการ (Operation System) Linux

Linus Torvalds ผู้สร้าง Linux ได้พยายามมองหา Version Control System สักตัว เพื่อเอาไว้จัดการไฟล์ต่าง ๆ ของ Linux Project

ในปี 2002 (พ.ศ. 2545) Linus ได้นำ BitKeeper มาใช้

และใช้งานไปได้อย่างราบรื่น ตลอดระยะเวลา 3 ปี

jittagornp.me

16 of 144

16

ความเป็นมา (ต่อ)

ต่อมา ต้นปี 2005 Larry McVoy ซึ่งเป็นเจ้าขอ BitKeeper ได้ออกมาไวยวายว่า

มีคนเอา BitKeeper ไปทำ Reverse Engineering (คนทำชื่อ Andrew Tridgell)

เพื่อแกะดูว่า BitKeeper ทำงานยังไง

ทำให้ Larry McVoy ไม่พอใจ เลยยกเลิกสัญญาการใช้งานฟรี ให้แก่ชุมชน Linux

ตูเอง ตูขอโทษ

jittagornp.me

17 of 144

17

ความเป็นมา (ต่อ)

Linus เห็นดังนั้น จึงได้ตัดสินใจ

ทำระบบ Version Control System ขึ้นมาใช้งานเอง ในกลางปี 2005

ในตอนแรก เอามาใช้จัดการกับ Source Code ของระบบปฏิบัติการ Linux

โดยตั้งชื่อเครื่องมือตัวนี้ว่า Git

***** รูปไม่เกี่ยวน่ะ Linus เค้าด่า Nvidia (คนละเรื่องกัน เอารูปมาเฉย ๆ)

jittagornp.me

18 of 144

18

ความเป็นมา (ต่อ)

เมื่อพัฒนาไปได้สักระยะนึง

ก็ได้ยกหน้าที่ดูแล Git ให้กับ Junio Hamano เอาไปดูแลต่อ

ในปลายปี 2005 Git version 1.0 ก็ได้เปิดตัวให้ชาวโลกได้ใช้งานกัน

jittagornp.me

19 of 144

19

Repository

jittagornp.me

20 of 144

20

Repository

คือ ที่ ๆ ใช้สำหรับเก็บไฟล์

ของระบบ Git เช่น

  • ไฟล์ Source Code
  • ไฟล์รูปภาพ
  • ไฟล์อื่น ๆ ใน Project
  • รวมถึงประวัติการแก้ไขไฟล์ต่าง ๆ ก็ถูกจัดเก็บไว้ใน Repository ด้วย

jittagornp.me

21 of 144

21

Commit

jittagornp.me

22 of 144

22

Commit

  • เวลาเราทำการ Save ไฟล์ ศัพท์ของ Git เค้าจะเรียกกันว่า “การ Commit”
  • ทุกการ Commit จะเป็นการเก็บประวัติการแก้ไขไฟล์ไว้ 1 ชุด
  • แต่ละชุดจะมีหมายเลข Commit ID กำกับ
  • ถ้าเราอยากย้อน Version ไปที่ตำแหน่งไหน ก็ให้ใช้ Commit ID นั้นเป็นตัว Reference

Commit ID จะหน้าตาประมาณนี้�1d7e6271b468b884355ba1dc835ca02b1358acbe

jittagornp.me

23 of 144

23

File

jittagornp.me

24 of 144

24

File

Modify

jittagornp.me

25 of 144

25

File

Commit

Repository

Modify

jittagornp.me

26 of 144

26

File

1qa7848381...

Commit

File

Repository

Commit ID

Modify

jittagornp.me

27 of 144

27

File

1qa7848381...

4jshshdsa13...

Commit

File

Repository

Commit ID

Modify

jittagornp.me

28 of 144

28

File

1qa7848381...

4jshshdsa13...

4456dhaf221...

Commit

Repository

Commit ID

Modify

jittagornp.me

29 of 144

29

HEAD

jittagornp.me

30 of 144

30

HAED

คือ Pointer ที่ชี้ไปยัง Commit ต่าง ๆ

โดยปกติ

HEAD จะชี้ไปที่ Commit ล่าสุดเสมอ (ถ้าไม่มีการย้อน Commit)

jittagornp.me

31 of 144

31

commit 1qa7848381… (ล่าสุด)

commit 3232443440...

commit 1189897241...

commit 4359878431...

HEAD

jittagornp.me

32 of 144

32

File Life Cycle

jittagornp.me

33 of 144

33

File Life Cycle

Untracked

คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git

Tracked

คือ ไฟล์ที่ถูก Track แล้ว โดย Git

jittagornp.me

34 of 144

34

File

File

File

File

Untracked

Tracked

jittagornp.me

35 of 144

35

File Life Cycle

Untracked

คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git

Tracked

คือ ไฟล์ที่ถูก Track แล้ว โดย Git

  • Staged คือ สถานะไฟล์ที่เตรียมจะ Commit
  • Unstage คือ ไฟล์ที่ยังไม่พร้อมจะ Commit

jittagornp.me

36 of 144

36

File

File

File

File

Untracked

Tracked

Staged

Unstage

jittagornp.me

37 of 144

37

File Life Cycle

Untracked

คือ ไฟล์ที่ยังไม่ได้ถูก Track โดย Git

Tracked

คือ ไฟล์ที่ถูก Track แล้ว โดย Git

  • Staged คือ สถานะไฟล์ที่เตรียมจะ Commit
  • Unstage คือ ไฟล์ที่ยังไม่พร้อมจะ Commit
    • Committed คือ สถานะไฟล์ที่ถูก Commit แล้ว รอการ Modify
    • Modified คือ สถานะไฟล์ที่ถูก Modify แล้ว

jittagornp.me

38 of 144

38

File

File

File

File

Untracked

Tracked

Staged

Committed

Modified

Unstage

jittagornp.me

39 of 144

39

Working Directory

Staging Area

Repository

jittagornp.me

40 of 144

40

Working Directory

Staging Area

Repository

Untracked

jittagornp.me

41 of 144

41

Working Directory

Staging Area

Repository

Untracked

Staged

jittagornp.me

42 of 144

42

Working Directory

Staging Area

Repository

Untracked

Staged

Commited

jittagornp.me

43 of 144

43

Working Directory

Staging Area

Repository

Untracked

Staged

Commited

Modified

jittagornp.me

44 of 144

44

Working Directory

Staging Area

Repository

Untracked

Staged

Commited

Modified

Staged

jittagornp.me

45 of 144

45

Working Directory

Staging Area

Repository

Untracked

Staged

Commited

Modified

Staged

Commited

jittagornp.me

46 of 144

46

Distributed

(การจัดเก็บข้อมูลการแก้ไขแบบกระจาย)

jittagornp.me

47 of 144

47

Git มีคุณสมบัติเป็น Distributed คือ

ไม่ได้จัดเก็บข้อมูลประวัติการแก้ไขไฟล์ไว้แค่ที่เดียว

แต่เก็บเป็นแบบกระจาย เพื่อป้องกันปัญหา Single Point of Fail

คือ ต่อให้ Repository ต้นทางที่เก็บข้อมูล ได้รับความเสียหาย

ก็ยังสามารถที่จะเรียกคืนประวัติการแก้ไขไฟล์จาก Repository อื่น ๆ ได้

jittagornp.me

48 of 144

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 of 144

49

Infrastructure

jittagornp.me

50 of 144

50

Repo

Repo

Repo

Repo

Repo

Remote Repository

Remote Repository

Local Repository

Client

Remote Server

jittagornp.me

51 of 144

51

Branch

jittagornp.me

52 of 144

52

Git Branch

เป็นคุณสมบัติเด่นอย่างนึงของ Git เลยก็ว่าได้ คือ

เราสามารถแตก Branch ออกมาจาก Code ตัวหลัก (Master)

เพื่อพัฒนา Feature บางอย่างเป็นแบบขนาน (Paralell)

ไปพร้อมกับตัวหลัก (Master) ได้

เมื่อพัฒนาเสร็จแล้ว

ก็ค่อยรวม (Merge) กลับ

ไปที่ Master เหมือนเดิม

New Feature

Master

jittagornp.me

53 of 144

53

jittagornp.me

54 of 144

54

Remote Repository

(Git Hosting)

jittagornp.me

55 of 144

55

Git Hosting (Remote Repository)

เป็น Server ที่ทำหน้าที่ ให้บริการฝากไฟล์ Git

ซึ่งเราสามารถนำ Source Code ต่าง ๆ ของเรา

ไปเก็บไว้บน Git Hosting ได้

มีทั้ง แบบเสียตังค์ และแบบที่ให้ใช้งานฟรี

มีให้เลือกใช้หลากหลาย Brand

แต่ละ Brand ก็มีจุดเด่น และจุดด้อยแตกต่างกันไป

ทั้งนี้ ขึ้นอยู่ที่ตัวเรา ว่าจะเลือกใช้ตัวไหน

Repo

Remote Repositories

Repo

Repo

Repo

Repo

Repo

jittagornp.me

56 of 144

56

Git Hosting

  • GitHub
  • GitLab
  • BitBucket
  • Google Cloud Source
  • AWS CodeCommit
  • Microsoft Azure Repos
  • etc.

jittagornp.me

57 of 144

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 of 144

58

Installation

jittagornp.me

59 of 144

59

Installation

สามารถ Download ได้จาก

https://git-scm.com/downloads

jittagornp.me

60 of 144

60

Command Line

jittagornp.me

61 of 144

61

Command Line

  • git version
  • git help
  • git config
  • git init
  • git status
  • git add
  • git rm
  • git commit
  • git log

  • git diff
  • git reset
  • git branch
  • git checkout
  • git merge
  • git remote
  • git clone
  • git fetch
  • git pull

  • git push
  • git tag
  • git clean
  • etc.

jittagornp.me

62 of 144

62

Git Version

jittagornp.me

63 of 144

63

Git Version

สำหรับดู Version ของ Git

�$ git version

jittagornp.me

64 of 144

64

Git Help

jittagornp.me

65 of 144

65

Git Help

สำหรับดูคำสั่ง Git ว่ามีคำสั่งหรือ Options อะไรใช้ได้บ้าง

�$ git help

$ git help <COMMAND_NAME>

jittagornp.me

66 of 144

66

jittagornp.me

67 of 144

67

Git Config

jittagornp.me

68 of 144

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 of 144

69

Git Config (ต่อ)

ดูว่ามีค่า Config อะไรบ้าง

�$ git config --list //เฉพาะ Repository นี้

$ git config --global --list //ทั้งหมด

jittagornp.me

70 of 144

70

jittagornp.me

71 of 144

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 of 144

72

Git Init

jittagornp.me

73 of 144

73

Git Init

ใช้สำหรับ Initial (สร้าง) Git Repository

$ git init

เมื่อ Run คำสั่งเสร็จ

จะมี Folder .git ถูกสร้างขึ้นมาใน Folder ที่เรา Run คำสั่งนี้

ข้างใน .git เป็นพื้นที่เก็บข้อมูลต่าง ๆ ของ Repository นี้

jittagornp.me

74 of 144

74

jittagornp.me

75 of 144

75

Git Status

jittagornp.me

76 of 144

76

Git Status

ใช้สำหรับตรวจสอบสถานะของไฟล์ต่าง ๆ ใน Repository

ซึ่งจะแสดงผลสถานะ ตามหัวข้อ File Life Cycle ที่ผ่านมา

$ git status

jittagornp.me

77 of 144

77

jittagornp.me

78 of 144

78

Git Add

jittagornp.me

79 of 144

79

Git Add

ใช้สำหรับ Add ไฟล์ เข้า Staging Area

หรือ ใช้สำหรับเปลี่ยนสถานะของไฟล์จากสถานะ “Untracked => Staged” เพื่อให้ Git เริ่ม Track ไฟล์นี้นั่นเอง

$ git add . //ทุกไฟล์ภายใต้ Current Path

$ git add <FILE_NAME> //เฉพาะเจาะจงไฟล์

jittagornp.me

80 of 144

80

Working Directory

Staging Area

Repository

Untracked

Staged

git add <FILE>

jittagornp.me

81 of 144

81

Git Add (ต่อ)

นอกจากจะเปลี่ยนสถานะจาก Untracked => Staged แล้ว

ยังใช้เปลี่ยนสถานะจาก Unstaged อื่น ๆ ไปยังสถานะ Staged อีกด้วย เช่น

Modified => Staged

jittagornp.me

82 of 144

82

Working Directory

Staging Area

Repository

Untracked

Staged

git add <FILE>

Modified

Staged

jittagornp.me

83 of 144

83

jittagornp.me

84 of 144

84

Git Remove

jittagornp.me

85 of 144

85

Git Remove

ใช้สำหรับลบไฟล์ออกจาก Staging Area

ทำให้ไฟล์นั้นกลับมาอยู่ในสถานะ Untracked อีกครั้ง

$ git rm --cached <FILE>

jittagornp.me

86 of 144

86

Working Directory

Staging Area

Repository

Untracked

Staged

git rm --cached <file>

jittagornp.me

87 of 144

87

jittagornp.me

88 of 144

88

Git Commit

jittagornp.me

89 of 144

89

Git Commit

ใช้สำหรับ Save ไฟล์ หรือย้ายไฟล์จาก Staging Area => Repository

เมื่อเราทำการ Commit ไฟล์จะถูกเก็บเป็นประวัติไว้ 1 ชุด

โดยมีหมายเลข Commit ID กำกับไว้

$ git commit -m “<COMMIT_MESSAGE>”

jittagornp.me

90 of 144

90

Working Directory

Staging Area

Repository

Staged

Commited

git commit -m “MESSAGE”

jittagornp.me

91 of 144

91

jittagornp.me

92 of 144

92

Git Log

jittagornp.me

93 of 144

93

Git Log

ใช้สำหรับดูประวัติการ Commit ต่าง ๆ ที่ผ่านมา ว่าทำอะไรไปบ้าง

$ git log

ออกจาก log ให้กด q

jittagornp.me

94 of 144

94

jittagornp.me

95 of 144

95

Git Diff

jittagornp.me

96 of 144

96

Git Diff (Difference)

ใช้สำหรับเช็คว่า มีอะไรเปลี่ยนแปลงไปบ้าง เพิ่ม ลบ แก้ไข อะไรไปบ้าง

เมื่อเทียบกับ Commit ที่ผ่านมา

$ git diff <COMMIT_ID>

$ git diff <COMMIT_ID> <COMMIT_ID>

$ git diff HEAD

jittagornp.me

97 of 144

97

jittagornp.me

98 of 144

98

Git Reset

jittagornp.me

99 of 144

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 of 144

100

Git Reset (ต่อ)

ทำการย้อน Commit ไปยัง Commit ต่าง ๆ

$ git reset --soft <COMMIT_ID> //ยังคงสถานะไฟล์เป็น Staged ไว้

jittagornp.me

101 of 144

101

Git Branch

jittagornp.me

102 of 144

102

Git Branch

เป็นคำสั่งที่ใช้ทำงานร่วมกับ Branch

เช่น ดูว่า Repository นี้มี Branch อะไรบ้าง

$ git branch

jittagornp.me

103 of 144

103

Git Branch (ต่อ)

ถ้าต้องการสร้าง Branch ใหม่ ให้ใช้คำสั่งนี้

$ git branch <BRANCH_NAME>

เช่น ทำการสร้าง Branch ชื่อ v2

$ git branch v2

jittagornp.me

104 of 144

104

Git Branch (ต่อ)

ถ้าต้องการลบ Branch ให้ใช้คำสั่งนี้

$ git branch -d <BRANCH_NAME>

เช่น ทำการลบ Branch ชื่อ v2

$ git branch -d v2

jittagornp.me

105 of 144

105

Git Checkout

jittagornp.me

106 of 144

106

Git Checkout

ใช้สำหรับเปลี่ยน Branch

หรือ ย้อนกลับไปยังตำแหน่งที่เราเคย Commit ไว้

$ git checkout <BRANCH_NAME>

$ git checkout <COMMIT_ID>

$ git checkout <FILE_NAME>

jittagornp.me

107 of 144

107

Git Checkout (ต่อ)

เช่น

$ git checkout master //เปลี่ยนไปที่ Branch master

$ git checkout 1d7e6271b468b884355ba1dc835ca02b1358acbe

//ย้อนไปที่ Commit ID : 1d7e6271b468b884355ba1dc835ca02b1358acbe

$ git checkout file1.txt //ย้อนไฟล์กลับไปยังตำแหน่งก่อนที่จะแก้ไข

jittagornp.me

108 of 144

108

jittagornp.me

109 of 144

109

Git Merge

jittagornp.me

110 of 144

110

Git Merge

ใช้สำหรับรวม Branch เข้าด้วยกัน

$ git merge <BRANCH_NAME>

เช่น ต้องการรวม Branch master เข้ากับ Branch v2

$ git checkout master //เปลี่ยน Branch ไปที่ Master ก่อน

$ git merge v2

jittagornp.me

111 of 144

111

Git Remote

jittagornp.me

112 of 144

112

Git Remote

ใช้สำหรับทำงานร่วมกับ Remote Repository

ดูว่า Local Repository นี้ เชื่อมต่อหรือมี Remote Repository อะไรบ้าง

$ git remote -v

jittagornp.me

113 of 144

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 of 144

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 of 144

115

Git Clone

jittagornp.me

116 of 144

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 of 144

117

jittagornp.me

118 of 144

118

Git Fetch

jittagornp.me

119 of 144

119

Git Fetch

ใช้สำหรับ Check Local Repository กับ Remote Repository

ว่าตรงกันหรือไม่ มีอะไรเปลี่ยนแปลงมั้ย โดยไม่ดึง Code ลงมา

$ git fetch

jittagornp.me

120 of 144

120

Git Pull

jittagornp.me

121 of 144

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 of 144

122

Git Push

jittagornp.me

123 of 144

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 of 144

124

Git Tag

jittagornp.me

125 of 144

125

Git Tag

เป็นคำสั่งที่ใช้สำหรับสร้าง Tag หรือกำหนด Tag

ให้กับ Commit เพื่อเอาไว้ระบุจุดที่ต้องการ Freeze หรือ Release Source Code

เช่น เราพัฒนาระบบมาถึงจุด ๆ นึงที่เราคิดว่า OK แล้ว (ตาม Milestone ที่ตั้งไว้)

เราอยากที่จะ Release Code นั้นออกมาเป็น Version RELEASE-1.0.0

ให้เราทำดังนี้

jittagornp.me

126 of 144

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 of 144

127

Git Tag (ต่อ)

ใน Git Remote Repository หรือ Git Hosting

จะมีการ Freeze Code เป็น Release Tag ไว้

ถ้าใน GitHub จะเห็น Release ขึ้นที่หน้า Repository ดังนี้

jittagornp.me

128 of 144

128

Git Tag (ต่อ)

jittagornp.me

129 of 144

129

Git Tag (ต่อ)

กรณีที่เราต้องการดึง (Checkout) Tag นั้น ๆ กลับมา ให้ทำดังนี้

$ git checkout tags/<TAG_NAME>

เช่น

$ git checkout tags/RELEASE-1.0.0

jittagornp.me

130 of 144

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 of 144

131

Git Clean

jittagornp.me

132 of 144

132

Git Clean

ใช้สำหรับทำงานกับไฟล์ที่อยู่ในสถานะ Untracked

$ git clean -n //แสดงไฟล์ทั้งหมดที่อยู่ในสถานะ Untracked

$ git clean -df //ลบไฟล์ที่อยู่ในสถานะ Untracked ออก

jittagornp.me

133 of 144

133

GUI

jittagornp.me

134 of 144

134

GUI

ถ้าใครขี้เกียจใช้งาน Command Line

สามารถ Download Git แบบ GUI ได้ที่

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

  • GitKraken
  • Sourcetree
  • GitHub Desktop
  • TortoiseGit
  • etc.

jittagornp.me

135 of 144

135

Git Ignore

jittagornp.me

136 of 144

136

Git Ignore

เป็นกรรมวิธีที่ใช้สำหรับบอก Git ว่า

ไม่ต้องสนใจ ไม่ต้องไปยุ่งอะไรกับ List ไฟล์ ที่ถูกเขียนไว้ในไฟล์ .gitignore

ปกติไฟล์ .gitignore จะวางไว้ใน Level เดียวกันกับ Folder .git (Repository)

jittagornp.me

137 of 144

137

Git Ignore (ต่อ)

ตัวอย่าง .gitignore (Git ไม่ต้อง Track ไม่ต้องยุ่งอะไรกับไฟล์พวกนี้)

/target/

/.settings/

/node_modules/

.classpath

.factorypath

.project

.nb-configuration.xml

y

jittagornp.me

138 of 144

138

Conflict

jittagornp.me

139 of 144

139

Conflict

คือ การที่มีคนพยายามแก้ไขไฟล์เดียวกัน พร้อมกัน (Comit พร้อมกัน)

เลยทำให้เกิด Conflict (การขัดแย้งกัน) ขึ้น

jittagornp.me

140 of 144

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 of 144

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 of 144

142

เรียนรู้เพิ่มเติม

jittagornp.me

143 of 144

143

เรียนรู้เพิ่มเติม

jittagornp.me

144 of 144

144

สำหรับเพื่อน ๆ คนไหน ที่ชื่นชอบบทความ

และอยากจะสนุนสนันค่ากาแฟเล็ก ๆ น้อย ๆ

สามารถคลิกที่ลิงค์นี้เพื่อไปยังหน้า Buy me a coffee ได้ครับ

jittagornp.me