Dark mode
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 |
7 | ล็อกอินเข้าสู่ GitHub | gh auth login |
8 | สร้าง repository ใหม่บน GitHub | gh repo create [ชื่อ-repo] --public --clone |
9 | เชื่อมต่อกับ remote repository | git 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 "รายละเอียด" |
เรียนรู้เพิ่มเติม: GitHub 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 | ตั้งค่าการทำงานของ Git | git 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 repository | git 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 | ดูประวัติ commit | git log --oneline |
git diff | ดูการเปลี่ยนแปลงที่ยังไม่ได้ stage | git 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 | ดูรายละเอียด commit | git 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
การพัฒนาฟีเจอร์ใหม่โดยแยกเป็นสาขาเฉพาะ เพื่อไม่รบกวนการทำงานหลัก
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 ครั้งถัดไป
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
การเตรียมการสำหรับการปล่อยเวอร์ชันใหม่
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" | บันทึกการเปลี่ยนแปลงหลังแก้ไข |
ส่งไป remote | git push origin bugfix-123 | อัพโหลดสาขาไปยัง remote repository |
bash
# สร้างสาขาจาก main สำหรับแก้ไข
git checkout -b bugfix-123 main
# แก้ไขและบันทึกการเปลี่ยนแปลง
git add . && git commit -m "Fix issue #123"
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin bugfix-123
Code 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
ขั้นตอน | คำสั่ง | คำอธิบาย |
---|---|---|
สร้างสาขา feature | git checkout -b feature/new-widget | สร้างสาขาสำหรับ feature ใหม่ |
ส่งไป remote | git push origin feature/new-widget | อัพโหลดสาขาไปยัง remote repository |
Merge หลัง approve | git checkout main && git merge feature/new-widget | รวมการเปลี่ยนแปลงเข้า main หลัง approve |
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]
bash
# สร้างสาขาจาก main สำหรับแก้ไข
git checkout -b bugfix-123 main
# แก้ไขและบันทึกการเปลี่ยนแปลง
git add . && git commit -m "Fix issue #123"
# ส่งการเปลี่ยนแปลงไปยัง remote
git push origin bugfix-123