#คู่มือการใช้งาน Git, GitHub และ GitHub CLI ตั้งแต่พื้นฐานจนถึงการทำงานร่วมกัน

การพัฒนาซอฟต์แวร์ในปัจจุบันมักมีนักพัฒนาหลายคนทำงานร่วมกัน มีการปรับปรุงโค้ดอย่างต่อเนื่อง และต้องสามารถตรวจสอบได้ว่าใครแก้ไขอะไร เมื่อใด และเพราะเหตุใด เครื่องมืออย่าง Git, GitHub และ GitHub CLI จึงมีบทบาทสำคัญในการจัดการซอร์สโค้ดและกระบวนการพัฒนาซอฟต์แวร์

บทความนี้อธิบายความแตกต่างของเครื่องมือทั้งสาม พร้อมตัวอย่างคำสั่งที่สามารถนำไปใช้งานได้จริง ตั้งแต่การสร้าง Repository ไปจนถึงการสร้าง Pull Request และตรวจสอบ GitHub Actions ผ่าน Terminal

#Git, GitHub และ GitHub CLI แตกต่างกันอย่างไร

เครื่องมือ หน้าที่
git ระบบควบคุมเวอร์ชันที่ใช้จัดการไฟล์และประวัติการแก้ไขในเครื่อง
GitHub บริการออนไลน์สำหรับจัดเก็บ Git Repository และทำงานร่วมกัน
gh GitHub CLI สำหรับจัดการ Repository, Issue, Pull Request, Actions และ Release ผ่าน Terminal

กล่าวอย่างง่ายคือ git ใช้จัดการซอร์สโค้ด ส่วน GitHub ใช้จัดเก็บและทำงานร่วมกันบนระบบออนไลน์ ขณะที่ gh ช่วยให้เราสั่งงาน GitHub โดยไม่ต้องเปิดเว็บเบราว์เซอร์

#แนวคิดสำคัญของ Git

ก่อนเริ่มใช้คำสั่ง ควรเข้าใจกระบวนการจัดเก็บไฟล์ของ Git ซึ่งประกอบด้วยพื้นที่หลักดังนี้

  1. Working Directory คือไฟล์ที่กำลังแก้ไขอยู่ในเครื่อง
  2. Staging Area คือพื้นที่เตรียมไฟล์ก่อนสร้าง Commit
  3. Local Repository คือประวัติ Commit ที่จัดเก็บอยู่ในเครื่อง
  4. Remote Repository คือ Repository ที่จัดเก็บอยู่บน GitHub

กระบวนการทำงานพื้นฐานมีลักษณะดังนี้

แก้ไขไฟล์ → git add → git commit → git push

#การติดตั้ง Git และ GitHub CLI

#Windows

เปิด PowerShell หรือ Windows Terminal แล้วรันคำสั่งต่อไปนี้

winget install --id Git.Git -e
winget install --id GitHub.cli -e

#macOS

หากติดตั้ง Homebrew แล้ว สามารถใช้คำสั่งต่อไปนี้

brew install git
brew install gh

#Ubuntu และ Debian

ติดตั้ง Git ด้วยคำสั่ง

sudo apt update
sudo apt install git

สำหรับ GitHub CLI ควรใช้ขั้นตอนจากเอกสารการติดตั้งอย่างเป็นทางการ เนื่องจากต้องเพิ่ม Repository และ Signing Key ของ GitHub CLI

ตรวจสอบการติดตั้งด้วยคำสั่ง

git --version
gh --version

#การตั้งค่า Git ครั้งแรก

กำหนดชื่อและอีเมลที่จะปรากฏในประวัติ Commit

git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"

กำหนดชื่อ Branch เริ่มต้นเป็น main

git config --global init.defaultBranch main

ตรวจสอบค่าที่กำหนดไว้

git config --global --list

หากต้องการดูค่ารายการใดรายการหนึ่ง สามารถระบุชื่อได้โดยตรง

git config user.name
git config user.email

#การเข้าสู่ระบบ GitHub ผ่าน Terminal

หลังติดตั้ง GitHub CLI ให้เข้าสู่ระบบด้วยคำสั่ง

gh auth login

สำหรับการใช้งานทั่วไปสามารถเลือกตัวเลือกดังนี้

GitHub.com
HTTPS
Login with a web browser

ตรวจสอบสถานะการเข้าสู่ระบบ

gh auth status

GitHub ไม่รองรับการใช้รหัสผ่านของบัญชีในการยืนยันตัวตนสำหรับคำสั่ง Git ผ่าน HTTPS แล้ว ควรใช้ GitHub CLI, Personal Access Token หรือ SSH Key

#การสร้าง Git Repository

สร้างโฟลเดอร์โปรเจกต์และเริ่มต้น Repository

mkdir my-project
cd my-project
git init -b main

ตรวจสอบสถานะของไฟล์

git status

เพิ่มไฟล์ที่ต้องการเข้าสู่ Staging Area

git add README.md

หากต้องการเพิ่มไฟล์ทั้งหมด

git add .

สร้าง Commit แรก

git commit -m "Initial commit"

ข้อความ Commit ควรสั้น กระชับ และบอกสิ่งที่เปลี่ยนแปลง เช่น

git commit -m "feat: add student login"
git commit -m "fix: validate email before registration"
git commit -m "docs: update installation guide"
git commit -m "test: add login feature tests"

#คำสั่งตรวจสอบการเปลี่ยนแปลง

#ตรวจสอบสถานะของ Repository

git status

#ดูไฟล์ที่ถูกแก้ไขแต่ยังไม่ได้เพิ่มเข้า Staging Area

git diff

#ดูสิ่งที่อยู่ใน Staging Area

git diff --staged

#ดูประวัติ Commit

git log

แสดงประวัติแบบกระชับและเห็นความสัมพันธ์ของ Branch

git log --oneline --graph --decorate --all

#ดูรายละเอียดของ Commit

git show COMMIT_ID

ตัวอย่าง

git show a81b93c

#การเชื่อมโปรเจกต์กับ GitHub

#วิธีที่ 1: สร้าง GitHub Repository ด้วย GitHub CLI

สร้าง Public Repository จากโฟลเดอร์ปัจจุบันและ Push โค้ดขึ้น GitHub

gh repo create my-project \
  --public \
  --source=. \
  --remote=origin \
  --push

หากเป็นโปรเจกต์ที่ไม่ต้องการเปิดเผยต่อสาธารณะ ให้ใช้ --private

gh repo create my-project \
  --private \
  --source=. \
  --remote=origin \
  --push

#วิธีที่ 2: เชื่อมกับ Repository ที่สร้างไว้แล้ว

git remote add origin https://github.com/USERNAME/REPOSITORY.git
git push -u origin main

ตรวจสอบ Remote

git remote -v

เปลี่ยน URL ของ Remote

git remote set-url origin https://github.com/USERNAME/NEW-REPOSITORY.git

ชื่อ origin เป็นเพียงชื่อเรียก Remote เริ่มต้นที่นิยมใช้ ไม่ใช่คำสงวนของ Git

#การ Clone โปรเจกต์จาก GitHub

ใช้คำสั่ง Git

git clone https://github.com/USERNAME/REPOSITORY.git
cd REPOSITORY

หรือใช้ GitHub CLI

gh repo clone USERNAME/REPOSITORY
cd REPOSITORY

หลัง Clone แล้ว Git จะสร้าง Remote ชื่อ origin และ Remote-tracking Branch ให้อัตโนมัติ

#การใช้งาน Push, Pull และ Fetch

ส่ง Commit จากเครื่องขึ้น GitHub

git push

Push Branch ขึ้น Remote เป็นครั้งแรก พร้อมกำหนด Upstream

git push -u origin main

หลังจากกำหนด Upstream แล้ว ครั้งต่อไปสามารถใช้เพียง

git push

ดึงการเปลี่ยนแปลงจาก Remote และรวมเข้ากับ Branch ปัจจุบัน

git pull

หากต้องการยอมรับเฉพาะการอัปเดตแบบ Fast-forward

git pull --ff-only

ดึงข้อมูลจาก Remote แต่ยังไม่รวมเข้ากับ Branch ปัจจุบัน

git fetch origin

ตรวจสอบ Commit ที่มีอยู่บน Remote แต่ยังไม่มีใน Local Branch

git log main..origin/main

เปรียบเทียบ Local Branch กับ Remote Branch

git diff main origin/main

git fetch เหมาะสำหรับกรณีที่ต้องการตรวจสอบการเปลี่ยนแปลงก่อน ส่วน git pull จะดึงข้อมูลและนำมารวมกับ Branch ปัจจุบันต่อทันที

#การทำงานกับ Branch

Branch ช่วยแยกงานแต่ละ Feature หรือการแก้ไข Bug ออกจาก Branch หลัก ทำให้สามารถพัฒนาและตรวจสอบโค้ดได้โดยไม่กระทบกับระบบที่ใช้งานอยู่

แสดงรายการ Branch

git branch

สร้าง Branch ใหม่และสลับไปใช้งาน

git switch -c feature/student-login

สลับกลับไปยัง Branch main

git switch main

Push Branch ใหม่ขึ้น GitHub

git push -u origin feature/student-login

รวม Branch เข้ากับ main

git switch main
git pull --ff-only
git merge feature/student-login
git push origin main

ลบ Local Branch หลังรวมงานแล้ว

git branch -d feature/student-login

ลบ Remote Branch

git push origin --delete feature/student-login

#Workflow สำหรับการพัฒนา Feature

ตัวอย่างกระบวนการที่แนะนำสำหรับการทำงานเป็นทีม

# 1. สลับไปยัง Branch หลักและอัปเดตโค้ด
git switch main
git pull --ff-only

# 2. สร้าง Branch สำหรับ Feature ใหม่
git switch -c feature/student-login

# 3. ตรวจสอบการเปลี่ยนแปลงหลังแก้ไขไฟล์
git status
git diff

# 4. เพิ่มและ Commit การเปลี่ยนแปลง
git add .
git commit -m "feat: add student login"

# 5. Push Branch ขึ้น GitHub
git push -u origin feature/student-login

# 6. สร้าง Pull Request
gh pr create \
  --base main \
  --title "Add student login" \
  --body "เพิ่มระบบเข้าสู่ระบบสำหรับนักศึกษา"

#การจัดการ Pull Request ด้วย GitHub CLI

แสดง Pull Request ที่เปิดอยู่

gh pr list

ดูรายละเอียด Pull Request ของ Branch ปัจจุบัน

gh pr view

เปิด Pull Request ในเว็บเบราว์เซอร์

gh pr view --web

สร้าง Pull Request แบบใช้ข้อมูลจาก Commit

gh pr create --fill

สร้าง Draft Pull Request

gh pr create --draft --fill

ตรวจสอบสถานะ CI ของ Pull Request ปัจจุบัน

gh pr checks

ตรวจสอบ Pull Request หมายเลข 25

gh pr checks 25

ดาวน์โหลด Branch ของ Pull Request มาทดสอบ

gh pr checkout 25

อนุมัติ Pull Request

gh pr review 25 --approve

รวม Pull Request แบบ Squash และลบ Branch

gh pr merge 25 --squash --delete-branch

การ Merge แบบ Squash จะรวม Commit ย่อยของ Feature Branch ให้เป็น Commit เดียวบน Branch หลัก เหมาะสำหรับ Repository ที่ต้องการประวัติ Commit ที่กระชับ

#การจัดการ Issue

สร้าง Issue แบบโต้ตอบ

gh issue create

สร้าง Issue พร้อมกำหนดรายละเอียด

gh issue create \
  --title "Login page returns 500" \
  --body "เกิดข้อผิดพลาดเมื่อผู้ใช้เข้าสู่ระบบ" \
  --label bug

แสดง Issue ที่เปิดอยู่

gh issue list

ดูรายละเอียด Issue

gh issue view 10

เปิด Issue ในเว็บเบราว์เซอร์

gh issue view 10 --web

ปิด Issue

gh issue close 10

หากข้อความใน Pull Request ระบุ Fixes #10 หรือ Closes #10 Issue หมายเลข 10 จะถูกปิดอัตโนมัติเมื่อ Pull Request ถูกรวมเข้า Branch หลัก

#การใช้งาน GitHub Actions ผ่าน Terminal

แสดง Workflow ที่มีอยู่

gh workflow list

สั่งรัน Workflow

gh workflow run tests.yml

แสดงประวัติการทำงาน

gh run list

ดูรายละเอียด Run

gh run view RUN_ID

ดู Log

gh run view RUN_ID --log

ติดตามการทำงานแบบต่อเนื่อง

gh run watch RUN_ID

รัน Job ที่ล้มเหลวใหม่

gh run rerun RUN_ID --failed

คำสั่งเหล่านี้มีประโยชน์สำหรับงาน CI/CD เพราะสามารถตรวจสอบ Build, Test และ Deployment ได้โดยไม่ต้องเปิดหน้า GitHub Actions

#การจัดการ Repository ด้วย GitHub CLI

ดูข้อมูล Repository ปัจจุบัน

gh repo view

เปิด Repository ในเว็บเบราว์เซอร์

gh repo view --web

แสดง Repository ของผู้ใช้

gh repo list USERNAME

Fork และ Clone Repository

gh repo fork OWNER/REPOSITORY --clone

การ Fork เหมาะสำหรับการพัฒนาโครงการที่เราไม่มีสิทธิ์ Push โดยตรง เมื่อแก้ไขเสร็จแล้วจึงสร้าง Pull Request กลับไปยัง Repository ต้นทาง

#การสร้าง Tag และ GitHub Release

สร้าง Annotated Tag

git tag -a v1.0.0 -m "Version 1.0.0"

Push Tag ขึ้น GitHub

git push origin v1.0.0

สร้าง GitHub Release พร้อมสร้าง Release Notes อัตโนมัติ

gh release create v1.0.0 \
  --title "Version 1.0.0" \
  --generate-notes

แสดง Release

gh release list

ดาวน์โหลดไฟล์จาก Release

gh release download v1.0.0

#การยกเลิกและย้อนกลับการเปลี่ยนแปลง

#ยกเลิกการแก้ไขไฟล์ที่ยังไม่ได้เพิ่มเข้า Staging Area

git restore filename

คำสั่งนี้จะทำให้การแก้ไขไฟล์หายไป จึงควรตรวจสอบให้แน่ใจก่อนใช้งาน

#นำไฟล์ออกจาก Staging Area แต่เก็บการแก้ไขไว้

git restore --staged filename

#แก้ข้อความหรือเพิ่มไฟล์เข้า Commit ล่าสุด

git commit --amend

กำหนดข้อความใหม่โดยตรง

git commit --amend -m "ข้อความ Commit ใหม่"

#ย้อนกลับ Commit ที่ Push และแชร์กับผู้อื่นแล้ว

git revert COMMIT_ID

git revert จะสร้าง Commit ใหม่เพื่อย้อนผลของ Commit เก่า จึงไม่เขียนประวัติที่ผู้อื่นใช้งานอยู่ทับใหม่

#การเก็บงานชั่วคราวด้วย Stash

หากต้องเปลี่ยน Branch แต่ยังไม่พร้อม Commit สามารถเก็บงานไว้ชั่วคราว

git stash push -m "unfinished login work"

ดูรายการ Stash

git stash list

นำงานล่าสุดกลับมาและลบออกจากรายการ Stash

git stash pop

นำงานกลับมาโดยยังเก็บรายการ Stash ไว้

git stash apply

#การแก้ Merge Conflict

Merge Conflict เกิดขึ้นเมื่อ Git ไม่สามารถตัดสินใจได้ว่าควรเลือกการแก้ไขจาก Branch ใด ตัวอย่างข้อความในไฟล์ที่ Conflict มีลักษณะดังนี้

<<<<<<< HEAD
โค้ดจาก Branch ปัจจุบัน
=======
โค้ดจากอีก Branch
>>>>>>> feature/student-login

ให้แก้ไขไฟล์โดยเลือกหรือรวมโค้ดที่ต้องการ แล้วลบเครื่องหมาย Conflict จากนั้นรัน

git add .
git commit -m "fix: resolve merge conflict"

หาก Conflict เกิดระหว่าง Rebase

git add .
git rebase --continue

หากต้องการยกเลิก Rebase

git rebase --abort

#การใช้งานไฟล์ .gitignore

ไฟล์ .gitignore ใช้ระบุไฟล์หรือโฟลเดอร์ที่ไม่ต้องการให้ Git ติดตาม ตัวอย่างสำหรับโปรเจกต์ Laravel

/vendor/
/node_modules/
.env
.env.*
/storage/*.key
/public/build/
.phpunit.result.cache

ไม่ควร Commit ข้อมูลต่อไปนี้

  • ไฟล์ .env
  • API Key
  • Access Token
  • Database Password
  • Private SSH Key
  • Service Account Credential

หากข้อมูลลับถูก Push ขึ้น GitHub แล้ว การลบออกจาก Commit ล่าสุดอาจไม่เพียงพอ เพราะข้อมูลอาจยังอยู่ในประวัติ Repository ควรยกเลิกหรือเปลี่ยน Secret นั้นทันที

#คำสั่งที่ควรใช้อย่างระมัดระวัง

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

git reset --hard
git clean -fd
git push --force
git branch -D BRANCH_NAME

หากจำเป็นต้อง Force Push บน Feature Branch ควรใช้

git push --force-with-lease

--force-with-lease ช่วยป้องกันการเขียนทับ Commit ใหม่ของผู้อื่นที่เราไม่ได้ดึงลงมา แต่ก็ยังควรหลีกเลี่ยงการใช้กับ Branch ที่หลายคนใช้งานร่วมกัน

#Git และ GitHub CLI Cheat Sheet

# เริ่มต้น Repository
git init -b main
git clone URL

# ตรวจสอบสถานะและประวัติ
git status
git diff
git diff --staged
git log --oneline --graph --all

# บันทึกการเปลี่ยนแปลง
git add .
git commit -m "feat: description"

# จัดการ Branch
git branch
git switch -c feature/name
git switch main
git branch -d feature/name

# ติดต่อ Remote
git remote -v
git fetch origin
git pull --ff-only
git push
git push -u origin BRANCH

# เข้าสู่ระบบและจัดการ Repository
gh auth login
gh auth status
gh repo create
gh repo clone OWNER/REPOSITORY
gh repo view --web

# จัดการ Issue
gh issue list
gh issue create
gh issue view ISSUE_NUMBER

# จัดการ Pull Request
gh pr create --fill
gh pr list
gh pr view
gh pr checks
gh pr merge --squash --delete-branch

# จัดการ GitHub Actions
gh workflow list
gh run list
gh run view RUN_ID --log
gh run watch RUN_ID

#สรุป

Git, GitHub และ GitHub CLI เป็นเครื่องมือที่ทำงานร่วมกัน แต่มีหน้าที่ต่างกันอย่างชัดเจน

  • ใช้ git สำหรับจัดการไฟล์ Branch และประวัติ Commit
  • ใช้ GitHub สำหรับจัดเก็บ Repository และทำงานร่วมกันบนออนไลน์
  • ใช้ gh สำหรับสั่งงานความสามารถของ GitHub ผ่าน Terminal

สำหรับการเริ่มต้น ควรฝึก Workflow พื้นฐานให้คล่อง ได้แก่ git status, git add, git commit, git pull, git push และการสร้าง Branch เมื่อทำงานเป็นทีมควรพัฒนาแต่ละ Feature บน Branch แยก สร้าง Pull Request เพื่อตรวจสอบโค้ด และรวมงานเข้าสู่ main หลังจาก CI และการ Review ผ่านเรียบร้อยแล้ว

#เอกสารอ้างอิง