Skip to content

Benefits of Git

ประโยชน์เมื่อใช้ Gitเมื่อไม่ใช้ Git
การควบคุมเวอร์ชัน- ติดตามประวัติการเปลี่ยนแปลงทั้งหมด
- ย้อนกลับไปเวอร์ชันก่อนหน้าได้
- ดูว่าใครเปลี่ยนแปลงอะไร เมื่อไร
- ไม่มีประวัติการเปลี่ยนแปลง
- ย้อนกลับยาก
- ไม่รู้ว่าใครแก้ไขอะไร
การทำงานร่วมกัน- ทำงานเป็นทีมได้อย่างมีประสิทธิภาพ
- รวมโค้ดจากหลายคนได้อย่างปลอดภัย
- จัดการความขัดแย้งอัตโนมัติ
- ต้องส่งไฟล์ไปมา
- โค้ดทับกันได้ง่าย
- แก้ไขความขัดแย้งยาก
การแบ่งสาขา- พัฒนาหลายฟีเจอร์พร้อมกันได้
- ทดสอบไอเดียใหม่โดยไม่กระทบโค้ดหลัก
- ต้องสร้างโฟลเดอร์แยก
- โค้ดปนกันระหว่างฟีเจอร์
- รวมโค้ดยาก
ความปลอดภัย- โค้ดถูกเก็บทั้งในเครื่องและบนเซิร์ฟเวอร์
- ไม่สูญเสียงานแม้เครื่องมีปัญหา
- เก็บโค้ดในเครื่องเดียว
- เสี่ยงสูญหายหากเครื่องเสีย
การติดตามงาน- มีประวัติการทำงานทั้งหมด
- รู้ว่าใครทำอะไรเมื่อไหร่
- ไม่มีระบบติดตาม
- ตรวจสอบยาก

Installation

bash
# ติดตั้ง Git ด้วย Scoop
scoop install git
bash
# ติดตั้ง Git ด้วย Homebrew
brew install git

Getting Started

Stepคำอธิบายคำสั่งที่ใช้
1ติดตั้ง Git (Windows)scoop install git
2ตั้งค่าข้อมูลผู้ใช้git config --global user.name "ชื่อคุณ"
git config --global user.email "อีเมล@คุณ.com"
3สร้าง repository ใหม่git init
4เพิ่มไฟล์เพื่อติดตามการเปลี่ยนแปลงgit add .
5บันทึกการเปลี่ยนแปลงครั้งแรกgit commit -m "Initial commit"
6ติดตั้ง GitHub CLI (Windows)scoop install gh
cli.github.com faviconDownload GitHub CLI
7ล็อกอินเข้าสู่ GitHubgh auth login
cli.github.com faviconAuthentication Docs
8สร้าง repository ใหม่บน GitHubgh repo create [ชื่อ-repo] --public --clone
cli.github.com faviconRepo Creation Docs
9เชื่อมต่อกับ remote repositorygit remote add origin https://github.com/username/repo.git
10อัปโหลดโค้ดครั้งแรกgit push -u origin main หรือ gh repo push
11เปิด pull request (ถ้าต้องการ)gh pr create --title "หัวข้อ" --body "รายละเอียด"
cli.github.com faviconPR Docs

เรียนรู้เพิ่มเติม: cli.github.com faviconGitHub CLI Manual

Configuration

Git อนุญาตให้ตั้งค่าต่างๆ ได้ทั้งระดับโปรเจคและระดับผู้ใช้

bash
# ตั้งค่าข้อมูลผู้ใช้เฉพาะโปรเจคนี้
git config user.name "ชื่อของคุณ"
git config user.email "อีเมล@ของคุณ.com"

# ตั้งค่า branch เริ่มต้น
git config init.defaultBranch main

# ตั้งค่า merge strategy
git config merge.ff false

# ตั้งค่า editor เฉพาะโปรเจค
git config core.editor "code --wait"

# ตั้งค่า ignore case สำหรับชื่อไฟล์
git config core.ignorecase false
bash
# ตั้งค่าข้อมูลผู้ใช้ทั้งหมด
git config --global user.name "ชื่อของคุณ"
git config --global user.email "อีเมล@ของคุณ.com"

# ตั้งค่า editor หลัก
git config --global core.editor "code --wait"

# ตั้งค่าสีใน terminal
git config --global color.ui auto

# ตั้งค่า pull strategy
git config --global pull.rebase merges

# ตั้งค่า alias ต่างๆ
git config --global alias.st status
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit

# ตั้งค่า credential helper
git config --global credential.helper manager

# ตั้งค่า line endings (สำหรับ Windows)
git config --global core.autocrlf true

CLI Command

Repository Commands

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git initเริ่มต้นโปรเจคใหม่ด้วย Git ในโฟลเดอร์ปัจจุบันgit init
git cloneคัดลอก repositoryเมื่อต้องการดาวน์โหลด repository จาก remote มาไว้ในเครื่อง

Basic Workflow

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git statusตรวจสอบว่าไฟล์ใดมีการเปลี่ยนแปลงบ้างgit status
git addเตรียมไฟล์ที่ต้องการบันทึกการเปลี่ยนแปลงgit add .
git commitบันทึกการเปลี่ยนแปลงเป็นเวอร์ชันใหม่git commit -m "ข้อความอธิบาย"
git pushส่งการเปลี่ยนแปลงขึ้นเซิร์ฟเวอร์git push origin main
git pullดึงการเปลี่ยนแปลงล่าสุดจากเซิร์ฟเวอร์git pull

Branching

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git branchสร้าง/ลบ/ดูสาขาทั้งหมดgit branch new-feature
git checkoutเปลี่ยนไปทำงานบนสาขาอื่นgit checkout main
git mergeรวมการเปลี่ยนแปลงจากสาขาอื่นเข้ามาgit merge feature-branch
git rebaseจัดเรียงประวัติการเปลี่ยนแปลงใหม่git rebase main

Remote

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git remoteจัดการที่อยู่เซิร์ฟเวอร์ปลายทางgit remote add origin https://github.com/user/repo.git
git fetchดึงข้อมูลจากเซิร์ฟเวอร์โดยยังไม่รวมการเปลี่ยนแปลงgit fetch origin

Inspection

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git logดูประวัติการเปลี่ยนแปลงทั้งหมดgit log --oneline
git diffดูรายละเอียดการแก้ไขไฟล์git diff
git showดูข้อมูลเฉพาะ commit ที่ต้องการgit show HEAD

Configuration

Commandใช้ทำอะไรตัวอย่างการใช้งาน
git configตั้งค่าการทำงานของ Gitgit config --global user.name "ชื่อของคุณ"

Basic Workflow

Repository

คำสั่งพื้นฐานสำหรับการสร้างและจัดการ Git repository

Commandใช้ทำอะไรExample
git initเริ่มต้นโปรเจคใหม่ด้วย Git ในโฟลเดอร์ปัจจุบันgit init
git cloneคัดลอก repositoryเมื่อต้องการดาวน์โหลด repository จาก remote มาไว้ในเครื่อง
bash
# สร้าง Git repository ใหม่ในโฟลเดอร์ปัจจุบัน
git init
# ตัวอย่างผลลัพธ์
# Initialized empty Git repository in /path/to/repo/.git/
bash
# ดาวน์โหลด repository จาก remote มาไว้ในเครื่อง
git clone https://github.com/user/repo.git
# ตัวอย่างผลลัพธ์
# Cloning into 'repo'...
# remote: Enumerating objects: 3, done.
# remote: Counting objects: 100% (3/3), done.
# remote: Compressing objects: 100% (2/2), done.
# remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
# Unpacking objects: 100% (3/3), done.

Local Remote

Commandใช้ทำอะไรExample
git remote addเพิ่ม remote repositorygit remote add origin https://github.com/user/repo.git
git remote -vดูรายการ remote ทั้งหมดgit remote -v
git fetchดึงข้อมูลจาก remote โดยยังไม่รวมการเปลี่ยนแปลงgit fetch origin
bash
# เพิ่ม remote repository ชื่อ origin
git remote add origin https://github.com/user/repo.git
# ตัวอย่างผลลัพธ์
# (ไม่มี output เมื่อสำเร็จ)
bash
# ดูรายการ remote ทั้งหมด
git remote -v
# ตัวอย่างผลลัพธ์
# origin  https://github.com/user/repo.git (fetch)
# origin  https://github.com/user/repo.git (push)
bash
# ดึงข้อมูลล่าสุดจาก remote โดยไม่รวมการเปลี่ยนแปลง
git fetch origin
# ตัวอย่างผลลัพธ์
# remote: Enumerating objects: 3, done.
# remote: Counting objects: 100% (3/3), done.
# remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
# Unpacking objects: 100% (3/3), done.
# From https://github.com/user/repo
#   3f7a2e5..1a9b8c2  main     -> origin/main

Inspection

Commandใช้ทำอะไรExample
git statusตรวจสอบว่าไฟล์ใดมีการเปลี่ยนแปลงบ้างgit status
git logดูประวัติ commitgit log --oneline
git diffดูการเปลี่ยนแปลงที่ยังไม่ได้ stagegit diff
bash
# ตรวจสอบสถานะปัจจุบัน
git status
# ตัวอย่างผลลัพธ์
# On branch main
# Changes not staged for commit:
#   modified:   file.txt
bash
# ดูประวัติ commit แบบย่อ
git log --oneline
# ตัวอย่างผลลัพธ์
# 3f7a2e5 (HEAD -> main) Fix critical bug
# 1a9b8c2 Update documentation
bash
# ดูการเปลี่ยนแปลงที่ยังไม่ได้ stage
git diff
# ตัวอย่างผลลัพธ์
# diff --git a/file.txt b/file.txt
# index 7898192..3b7e123 100644
# --- a/file.txt
# +++ b/file.txt
# @@ -1,3 +1,3 @@
# -Old content
# +New content

Branching

คำสั่งสำหรับสร้าง เปลี่ยน และรวมสาขา (branch) ต่างๆ

Commandใช้ทำอะไรExample
git branchจัดการสาขาgit branch new-feature
git checkoutเปลี่ยนสาขาgit checkout main
git mergeรวมสาขาgit merge feature
bash
# สร้างสาขาใหม่ชื่อ new-feature
git branch new-feature
# ตัวอย่างผลลัพธ์
# (ไม่มี output เมื่อสำเร็จ)
bash
# เปลี่ยนไปทำงานบนสาขา main
git checkout main
# ตัวอย่างผลลัพธ์
# Switched to branch 'main'
bash
# รวมสาขา feature เข้ากับ main
git checkout main
git merge feature-branch
# ตัวอย่างผลลัพธ์
# Updating 3f7a2e5..1a9b8c2
# Fast-forward
#  file.txt | 2 +-
#  1 file changed, 1 insertion(+), 1 deletion(-)

Merge

Commandใช้ทำอะไรExample
git mergeรวมการเปลี่ยนแปลงจากสาขาอื่นgit merge feature-branch
git rebaseย้ายฐานของสาขาgit rebase main
bash
# รวมสาขา feature เข้ากับ main
git checkout main
git merge feature-branch
# ตัวอย่างผลลัพธ์
# Updating 3f7a2e5..1a9b8c2
# Fast-forward
#  file.txt | 2 +-
#  1 file changed, 1 insertion(+), 1 deletion(-)
bash
# ย้ายฐานสาขาปัจจุบันไปยัง main
git rebase main
# ตัวอย่างผลลัพธ์
# Successfully rebased and updated refs/heads/feature-branch

Commit

Commandใช้ทำอะไรExample
git commit -mสร้าง commit พร้อมข้อความgit commit -m "Add new feature"
git commit --amendแก้ไข commit ล่าสุดgit commit --amend -m "New message"
git showดูรายละเอียด commitgit show HEAD
bash
# สร้าง commit พร้อมข้อความ
git commit -m "ข้อความ commit"
# ตัวอย่างผลลัพธ์
# [main 1a9b8c2] ข้อความ commit
#  1 file changed, 1 insertion(+), 1 deletion(-)
bash
# แก้ไข commit ล่าสุด
git commit --amend -m "ข้อความใหม่"
# ตัวอย่างผลลัพธ์
# [main 3f7a2e5] ข้อความใหม่
# Date: Mon Jul 15 20:15:16 2025 +0700
#  1 file changed, 1 insertion(+), 1 deletion(-)
bash
# ดูรายละเอียด commit
git show HEAD
# ตัวอย่างผลลัพธ์
# commit 3f7a2e5...
# Author: User <[email protected]>
# Date:   Mon Jul 15 20:15:16 2025 +0700
#
#     ข้อความ commit
#
# diff --git a/file.txt b/file.txt
# index 7898192..3b7e123 100644
# --- a/file.txt
# +++ b/file.txt
# @@ -1,3 +1,3 @@
# -Old content
# +New content

Advanced Workflow

Feature Development

การพัฒนาฟีเจอร์ใหม่โดยแยกเป็นสาขาเฉพาะ เพื่อไม่รบกวนการทำงานหลัก

feature
bash
# สร้างและเปลี่ยนไปสาขาใหม่
git checkout -b feature/new-widget  
# เตรียมและบันทึกการเปลี่ยนแปลง
git add . && git commit -m "Add new feature"
# อัปโหลดการเปลี่ยนแปลงไปยัง remote repository สาขาใหม่
git push origin feature/new-widget

Hotfix

การแก้ไขปัญหาด่วนที่ต้องนำไปใช้ใน production โดยไม่รอ release ครั้งถัดไป

hotfix
bash
# สร้างสาขาจาก main
git checkout -b hotfix/login-issue main 
# ทำการแก้ไขปัญหา
git add . && git commit -m "Fix critical bug"
# เปลี่ยนไปสาขา main และรวมการเปลี่ยนแปลงจากสาขา hotfix
git checkout main && git merge hotfix/login-issue
git checkout develop && git merge hotfix/login-issue

Release Preparation

การเตรียมการสำหรับการปล่อยเวอร์ชันใหม่

release
bash
# สร้างสาขาสำหรับ release
git checkout -b release/v1.2.0 develop
# ทำ final testing
git tag -a v1.2.0 -m "Release version 1.2.0"
git checkout main && git merge release/v1.2.0
git checkout develop && git merge release/v1.2.0
git push origin --tags

Bug Fixing

การแก้ไขข้อบกพร่องทั่วไปที่พบระหว่างการพัฒนา

ขั้นตอนคำสั่งคำอธิบาย
สร้างสาขาแก้ไขgit checkout -b bugfix-123 mainสร้างสาขาใหม่จาก main สำหรับแก้ไขข้อบกพร่อง
แก้ไขและบันทึกgit add . && git commit -m "Fix issue #123"บันทึกการเปลี่ยนแปลงหลังแก้ไข
ส่งไป remotegit push origin bugfix-123อัพโหลดสาขาไปยัง remote repository
bugfix
bash
# สร้างสาขาจาก main สำหรับแก้ไข
git checkout -b bugfix-123 main
# แก้ไขและบันทึกการเปลี่ยนแปลง
git add . && git commit -m "Fix issue #123"
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin bugfix-123

Code Review

กระบวนการตรวจสอบโค้ดก่อนรวมเข้ากับสาขาหลัก

review
bash
# สร้างสาขาสำหรับ feature
git checkout -b feature/new-widget
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin feature/new-widget
# หลังจาก review แล้ว merge เข้า main
git checkout main && git merge feature/new-widget

Code Review

ขั้นตอนคำสั่งคำอธิบาย
สร้างสาขา featuregit checkout -b feature/new-widgetสร้างสาขาสำหรับ feature ใหม่
ส่งไป remotegit push origin feature/new-widgetอัพโหลดสาขาไปยัง remote repository
Merge หลัง approvegit checkout main && git merge feature/new-widgetรวมการเปลี่ยนแปลงเข้า main หลัง approve
review
bash
# สร้างสาขาสำหรับ feature
git checkout -b feature/new-widget
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin feature/new-widget
# หลังจาก review แล้ว merge เข้า main
git checkout main && git merge feature/new-widget

### Bug Fixing
การแก้ไขข้อบกพร่องทั่วไปที่พบระหว่างการพัฒนา

| ขั้นตอน | คำสั่ง | คำอธิบาย |
|---------|--------|----------|
| สร้างสาขาแก้ไข | `git checkout -b bugfix-123 main` | สร้างสาขาใหม่จาก main สำหรับแก้ไขข้อบกพร่อง |
| แก้ไขและบันทึก | `git add . && git commit -m "Fix issue #123"` | บันทึกการเปลี่ยนแปลงหลังแก้ไข |
| ส่งไป remote | `git push origin bugfix-123` | อัพโหลดสาขาไปยัง remote repository |

```mermaid
flowchart LR
    A[Main Branch] -->|git checkout -b| B[Bugfix Branch]
    B -->|Fix Issue| C[Changes]
    C -->|git push| D[Remote Branch]
bugfix
bash
# สร้างสาขาจาก main สำหรับแก้ไข
git checkout -b bugfix-123 main
# แก้ไขและบันทึกการเปลี่ยนแปลง
git add . && git commit -m "Fix issue #123"
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin bugfix-123

Last updated: