Git และ GitHub: commit และ push
Git คือโปรแกรมบนเครื่องที่บันทึกประวัติโค้ดเป็น “จุดเซฟ” เรียกว่า commit ย้อนกลับได้ทุกเมื่อ ส่วน GitHub คือเว็บที่เก็บ repository ไว้บนคลาวด์ ให้คนอื่นเห็นและให้ Vercel ดึงไป deploy ได้
At a glance
| คำศัพท์ | ความหมาย |
|---|---|
| Repository (repo) | โฟลเดอร์โปรเจกต์ที่ Git ติดตามประวัติ |
| Staging area | ไฟล์ที่เลือกแล้วว่าจะเอาเข้า commit ถัดไป (git add) |
| Commit | จุดเซฟ 1 จุด พร้อมข้อความว่าเปลี่ยนอะไร |
| Remote (origin) | repo ฉบับบน GitHub |
| Push / Pull | ส่ง commit ขึ้น GitHub / ดึง commit ใหม่ลงมา |
How it works
แก้ไฟล์ ──git add──▶ Staging ──git commit──▶ Local repo ──git push──▶ GitHubGit เก็บประวัติบนเครื่องของคุณ แม้ไม่มีอินเทอร์เน็ตก็ commit ได้ พอ git push ประวัติทั้งหมดจะไปอยู่บน GitHub ด้วย
Installation
ดาวน์โหลดจาก git-scm.com/downloads แล้วกด Next ตามค่าเริ่มต้น จะได้ Git Bash มาด้วย หรือ
winget install -e --id Git.Gitxcode-select --install
# หรือใช้ Homebrew
brew install gitเช็กเวอร์ชัน
git --versionบอก Git ว่าคุณคือใคร
ทำครั้งเดียวต่อเครื่อง ใช้อีเมลเดียวกับบัญชี GitHub
git config --global user.name "Your Name" git config --global user.email "you@example.com" git config --global init.defaultBranch mainสมัคร GitHub
สมัครที่ github.com/signup ถ้ายังไม่มีบัญชี
เอาโปรเจกต์ขึ้น GitHub ครั้งแรก
สร้าง repo บน GitHub
ไปที่ github.com/new ตั้งชื่อ แล้ว ไม่ต้อง ติ๊ก Add README
commit บนเครื่อง
git init # เริ่มให้ Git ติดตามโฟลเดอร์นี้ git add . # เลือกทุกไฟล์เข้า staging git commit -m "first commit" # สร้างจุดเซฟผูกกับ GitHub แล้ว push
git branch -M main git remote add origin https://github.com/<username>/<repo>.git git push -u origin main
ครั้งต่อไปที่แก้โค้ด ใช้แค่ 3 คำสั่งวนไป
git add .
git commit -m "add projects section"
git pushpush แล้วถูกถามรหัสผ่าน?Windows จะเปิดหน้าต่างให้ล็อกอิน GitHub ผ่านเบราว์เซอร์ ถ้า macOS ถามรหัสผ่าน ให้ติดตั้ง GitHub CLI แล้วรัน gh auth login
คำสั่งหลัก
| คำสั่ง | ทำอะไร |
|---|---|
git status | ดูว่าไฟล์ไหนแก้ ไฟล์ไหนอยู่ใน staging (ใช้บ่อยที่สุด) |
git add <file> / git add . | เลือกไฟล์เข้า staging |
git commit -m "ข้อความ" | สร้าง commit |
git log --oneline | ดูประวัติ commit แบบย่อ |
git diff | ดูว่าแก้อะไรไปบ้าง |
git push / git pull | ส่งขึ้น / ดึงลงจาก GitHub |
git clone <url> | ดาวน์โหลด repo ลงเครื่อง |
สร้างไฟล์ .gitignore เพื่อบอก Git ว่าไฟล์ไหนไม่ต้องเก็บ
node_modules/
.env
.DS_Store
