Dark mode
คู่มือการใช้ Git แบบเป็นขั้นตอน
พื้นฐานการใช้งาน Git
การเริ่มต้นโปรเจค
สร้าง repository ใหม่:
bash
# สร้าง Git repository ในโฟลเดอร์ปัจจุบัน
git init
- สร้าง.git directory สำหรับเก็บประวัติ
- ใช้ครั้งแรกเมื่อเริ่มโปรเจคใหม่
ทำงานกับ repository ที่มีอยู่:
bash
# โคลน repository จาก URL ที่ระบุ
git clone https://github.com/user/repo.git
- โคลนทั้ง repository มาไว้เครื่อง
- สามารถระบุชื่อโฟลเดอร์ปลายทางได้
bash
git clone https://github.com/user/repo.git my-project
การทำงานประจำวัน
วงจรการทำงานพื้นฐาน:
ตรวจสอบสถานะ:
bash
# แสดงสถานะไฟล์ที่เปลี่ยนแปลงทั้งหมด
git status
- แสดงไฟล์ที่เปลี่ยนแปลง
- แสดงไฟล์ที่เตรียม commit แล้ว
- แสดงไฟล์ที่ยังไม่ถูก track
เตรียมไฟล์สำหรับ commit:
bash
# เตรียมไฟล์เดียวสำหรับ commit
git add file.txt
# เตรียมทุกไฟล์ที่เปลี่ยนแปลงสำหรับ commit
git add .
- ใช้
git add .
เพื่อเตรียมทุกไฟล์ที่เปลี่ยนแปลง
บันทึกการเปลี่ยนแปลง:
bash
# Commit การเปลี่ยนแปลงพร้อมข้อความอธิบาย
git commit -m "ข้อความอธิบายการเปลี่ยนแปลง"
- ใช้
git commit -m
เพื่อบันทึกการเปลี่ยนแปลงพร้อมข้อความอธิบาย
การทำงานกับ Branch
ภาพรวมการทำงานกับ Branch:
สร้าง branch ใหม่:
bash
# สร้าง branch ใหม่โดยยังไม่เปลี่ยนไปใช้
git branch feature/new-feature
- ใช้
git branch
เพื่อสร้าง branch ใหม่
เปลี่ยนไปใช้ branch:
bash
# เปลี่ยนไปทำงานบน branch ที่ระบุ
git checkout feature/new-feature
- ใช้
git checkout
เพื่อเปลี่ยนไปใช้ branch
ส่ง branch ไปยัง remote:
bash
# ส่ง branch ไปยัง remote repository
git push origin feature/new-feature
- ใช้
git push
เพื่อส่ง branch ไปยัง remote
การทำงานแบบทีม
การอัพเดทโค้ด
ดึงการเปลี่ยนแปลงล่าสุด:
bash
# ดึงและรวมการเปลี่ยนแปลงจาก remote branch
# ใช้เมื่อต้องการอัพเดทโค้ดให้ทันสมัย
git pull origin main
- ใช้
git pull
เพื่อดึงการเปลี่ยนแปลงล่าสุด
แก้ไข conflict:
bash
# เมื่อเกิด conflict ระหว่างการ merge
# 1. เปิดไฟล์ที่ conflict และแก้ไขด้วยมือ
# 2. เตรียมไฟล์ที่แก้ไขแล้ว
git add file-with-conflict.txt
# 3. ดำเนินการ merge ต่อ
git merge --continue
- ใช้
git merge --continue
เพื่อดำเนินการ merge ต่อ
ส่งการเปลี่ยนแปลงของคุณ:
bash
# ส่งการเปลี่ยนแปลงของคุณไปยัง remote repository
# ใช้หลังจาก commit แล้วเท่านั้น
git push origin feature/new-feature
- ใช้
git push
เพื่อส่งการเปลี่ยนแปลงของคุณ
การแก้ไข Conflict
ไฟล์ A -- แก้ไขบรรทัด 10 --> Conflict ไฟล์ B -- แก้ไขบรรทัด 10 --> Conflict Conflict -- git add --> ไฟล์ที่แก้ไขแล้ว
ยกเลิกการเปลี่ยนแปลงในไฟล์:
bash
# ยกเลิกการเปลี่ยนแปลงทั้งหมดในไฟล์ที่ระบุ
# ใช้เมื่อต้องการกลับไปเวอร์ชันล่าสุดที่ commit
git checkout -- file.txt
- ใช้
git checkout --
เพื่อยกเลิกการเปลี่ยนแปลง
แก้ไข commit ล่าสุด:
bash
# แก้ไข commit ล่าสุด (เปลี่ยนข้อความหรือเพิ่มไฟล์)
# 1. เตรียมไฟล์ที่ต้องการเพิ่ม (ถ้ามี)
git add forgotten-file.txt
# 2. แก้ไข commit
git commit --amend
# 3. ต้อง force push ถ้าเคย push ไปแล้ว
git push origin feature/new-feature --force
- ใช้
git commit --amend
เพื่อแก้ไข commit ล่าสุด
ย้าย commit ไป branch อื่น:
bash
# คัดลอก commit หนึ่งไปยัง branch ปัจจุบัน
# หา commit hash ได้จาก git log
git cherry-pick abc1234
# ถ้าเกิด conflict ให้แก้ไขแล้วรัน:
git cherry-pick --continue
- ใช้
git cherry-pick
เพื่อคัดลอก commit
Git Workflow
Working Directory -- git add --> Staging Area Staging Area -- git commit --> Local Repository Local Repository -- git push --> Remote Repository Remote Repository -- git pull --> Working Directory
ใช้ git log --oneline --graph
เพื่อดูประวัติแบบย่อพร้อมแผนภาพ branch ใช้ .gitignore
เพื่อระบุไฟล์/โฟลเดอร์ที่ต้องการไม่ track Commit บ่อยๆ ด้วยข้อความอธิบายที่ชัดเจน ทำความเข้าใจ workflow ของทีมก่อนเริ่มทำงาน
เทคนิคขั้นสูง
การใช้ Git Stash
เก็บการเปลี่ยนแปลงชั่วคราว:
bash
git stash
- ใช้
git stash
เพื่อเก็บการเปลี่ยนแปลงชั่วคราว
นำการเปลี่ยนแปลงกลับมา:
bash
git stash pop
- ใช้
git stash pop
เพื่อนำการเปลี่ยนแปลงกลับมา
การใช้ Git Tag
สร้าง tag สำหรับเวอร์ชัน:
bash
# สร้าง lightweight tag
git tag v1.0.0
# สร้าง annotated tag
git tag -a v1.0.0 -m "Release version 1.0.0"
- ใช้
git tag
เพื่อสร้าง tag สำหรับเวอร์ชัน
ส่ง tag ไปยัง remote:
bash
git push origin v1.0.0
- ใช้
git push
เพื่อส่ง tag ไปยัง remote
การใช้ Git Submodules
เพิ่ม submodule:
bash
git submodule add https://github.com/user/repo.git path/to/submodule
อัพเดท submodules ทั้งหมด:
bash
git submodule update --init --recursive
- ใช้
git submodule add
เพื่อเพิ่ม submodule - ใช้
git submodule update
เพื่ออัพเดท submodules ทั้งหมด
การตั้งค่าและการแก้ไขปัญหา
การตั้งค่า Git
ตั้งค่าผู้ใช้:
bash
git config --global user.name "ชื่อของคุณ"
git config --global user.email "อีเมล@ของคุณ"
- ใช้
git config --global
เพื่อตั้งค่าผู้ใช้
ตั้งค่า editor:
bash
git config --global core.editor code
- ใช้
git config --global
เพื่อตั้งค่า editor
การแก้ไขปัญหา
ดูประวัติการ commit:
bash
# แสดงประวัติแบบย่อ
git log --oneline
# แสดงประวัติพร้อมกราฟ branch
git log --graph --oneline --all
- ใช้
git log
เพื่อดูประวัติการ commit
ค้นหาการเปลี่ยนแปลงในโค้ด:
bash
# ค้นหาว่าใครแก้ไขบรรทัดไหน
git blame file.txt
- ใช้
git blame
เพื่อค้นหาการเปลี่ยนแปลงในโค้ด
การใช้ .gitignore
สร้างไฟล์ .gitignore:
bash
# ตัวอย่างเนื้อหาในไฟล์ .gitignore
node_modules/
.env
*.log
- ใช้
#
เพื่อแสดงความคิดเห็น - ใช้
*
เพื่อแสดงตัวแทน