🚀 Advanced Git & GitHub

บทเรียนขั้นสูง - For Thai English Learners

💻 Command Line | 🌿 Branches | 🤝 Collaboration

💻 ติดตั้ง Git บนเครื่อง
Installing Git on Your Computer

🎯 Why Install Git Locally? / ทำไมต้องติดตั้ง Git?

English: Installing Git on your computer lets you work offline, use powerful command-line tools, and work faster than using GitHub's website!

ไทย: การติดตั้ง Git บนเครื่องให้คุณทำงานออฟไลน์ได้ ใช้เครื่องมือ command-line ที่ทรงพลัง และทำงานได้เร็วกว่าใช้เว็บไซต์ GitHub!

1ดาวน์โหลด Git / Download Git

สำหรับ Windows:

  1. ไปที่ https://git-scm.com
  2. คลิก "Download for Windows"
  3. เปิดไฟล์ที่ดาวน์โหลด
  4. คลิก "Next" ตลอด (ใช้ค่าเริ่มต้น)
  5. คลิก "Install"

สำหรับ Mac:

  1. เปิด Terminal
  2. พิมพ์: git --version
  3. ถ้ายังไม่มี Git จะถามให้ติดตั้ง Xcode Command Line Tools

English: For Windows, download from git-scm.com and install. For Mac, open Terminal and type "git --version" - it will prompt installation if needed.

2ตั้งค่า Git / Configure Git

เปิด Git Bash (Windows) หรือ Terminal (Mac/Linux) และพิมพ์:

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

English: Open Git Bash or Terminal and configure your name and email. This information will appear in your commits.

💡 Pro Tip / เคล็ดลับ

ใช้อีเมลเดียวกับที่ลงทะเบียน GitHub เพื่อให้คอมมิตแสดงโปรไฟล์ของคุณ!

English: Use the same email as your GitHub account so your commits link to your profile!

3ทดสอบการติดตั้ง / Test Installation

git --version

ถ้าเห็นเลขเวอร์ชัน เช่น git version 2.40.0 แสดงว่าติดตั้งสำเร็จ! 🎉

English: If you see a version number like "git version 2.40.0", installation was successful!

🌿 การทำงานกับ Branches
Working with Branches

🌳 What are Branches? / Branches คืออะไร?

English: Branches let you create separate versions of your project to try new features without affecting the main code. Think of it like creating a copy to experiment with!

ไทย: Branches ให้คุณสร้างเวอร์ชันแยกของโปรเจกต์เพื่อทดลองฟีเจอร์ใหม่โดยไม่กระทบโค้ดหลัก คิดเหมือนการสร้างสำเนาเพื่อทดลอง!

1ดู Branches ทั้งหมด / View All Branches

git branch

คำสั่งนี้แสดงรายการ branches ทั้งหมด Branch ที่มี * คือ branch ปัจจุบัน

English: This command shows all branches. The branch with * is your current branch.

2สร้าง Branch ใหม่ / Create a New Branch

git branch feature-new-design

สร้าง branch ชื่อ "feature-new-design" (แต่ยังไม่ได้ย้ายไป)

English: This creates a new branch called "feature-new-design" but doesn't switch to it yet.

3สลับไป Branch อื่น / Switch to Another Branch

git checkout feature-new-design

หรือใช้คำสั่งใหม่:

git switch feature-new-design

English: Use "git checkout" or the newer "git switch" command to change branches.

⚡ Shortcut / ทางลัด

สร้างและสลับไปพร้อมกัน:

git checkout -b feature-new-design

หรือ

git switch -c feature-new-design

4Merge Branches / รวม Branches

เมื่อฟีเจอร์เสร็จแล้ว ให้รวม (merge) กลับเข้า main:

  1. สลับกลับไป main: git checkout main
  2. Merge branch: git merge feature-new-design
git checkout main git merge feature-new-design

English: When your feature is ready, switch back to main and merge your branch into it.

5ลบ Branch / Delete a Branch

git branch -d feature-new-design

ลบ branch หลังจาก merge เสร็จแล้ว (ใช้ -D ถ้าต้องการบังคับลบ)

English: Delete a branch after merging. Use -D to force delete if needed.

⌨️ คำสั่ง Git ที่สำคัญ
Essential Git Commands

📝 Basic Workflow / ขั้นตอนการทำงานพื้นฐาน

1Clone Repository / คัดลอกรีโพ

git clone https://github.com/username/repository-name.git

คัดลอก repository จาก GitHub มาเครื่องคุณ

English: Copy a repository from GitHub to your computer.

2Check Status / ตรวจสอบสถานะ

git status

ดูว่าไฟล์ไหนเปลี่ยนแปลง ไฟล์ไหนพร้อม commit

English: Check which files have changed and which are ready to commit.

3Add Files / เพิ่มไฟล์

# เพิ่มไฟล์เดียว / Add one file git add filename.txt # เพิ่มทุกไฟล์ / Add all files git add . # เพิ่มไฟล์ทั้งหมดในโฟลเดอร์ / Add all files in folder git add foldername/

English: Add files to the staging area, preparing them for commit.

4Commit Changes / บันทึกการเปลี่ยนแปลง

git commit -m "Add new feature: user login"

บันทึกการเปลี่ยนแปลงพร้อมข้อความอธิบาย

English: Save your changes with a descriptive message.

5Push to GitHub / ส่งขึ้น GitHub

git push origin main

ส่งการเปลี่ยนแปลงจากเครื่องคุณไป GitHub

English: Send your changes from your computer to GitHub.

6Pull from GitHub / ดึงจาก GitHub

git pull origin main

ดึงการเปลี่ยนแปลงล่าสุดจาก GitHub มาเครื่องคุณ

English: Get the latest changes from GitHub to your computer.

📊 Git Commands Reference / ตารางคำสั่ง Git

Command / คำสั่ง Description / คำอธิบาย
git init สร้าง Git repository ใหม่ / Create new Git repository
git status ดูสถานะไฟล์ / Check file status
git add . เพิ่มไฟล์ทั้งหมด / Add all files
git commit -m "message" บันทึกการเปลี่ยนแปลง / Save changes
git push ส่งขึ้น GitHub / Upload to GitHub
git pull ดึงจาก GitHub / Download from GitHub
git branch ดู branches / List branches
git checkout -b name สร้าง branch ใหม่ / Create new branch
git merge name รวม branch / Merge branch
git log ดูประวัติ commits / View commit history
git diff ดูความแตกต่าง / See differences
git remote -v ดู remote URLs / Show remote URLs

🔀 Pull Requests
การขอรวมโค้ด

🤝 What is a Pull Request? / Pull Request คืออะไร?

English: A Pull Request (PR) is a way to ask the project owner to review and merge your changes. It's how teams collaborate on code!

ไทย: Pull Request (PR) คือวิธีขอให้เจ้าของโปรเจกต์ตรวจสอบและรวมการเปลี่ยนแปลงของคุณ เป็นวิธีที่ทีมทำงานร่วมกันบนโค้ด!

1Fork Repository / Fork รีโพ

ก่อนแก้ไขโปรเจกต์ของคนอื่น ให้ Fork ก่อน:

  1. ไปที่ repository ที่ต้องการ
  2. คลิกปุ่ม Fork มุมบนขวา
  3. ตอนนี้คุณมีสำเนาของรีโพในบัญชีของคุณ!

English: Click the Fork button to create your own copy of someone else's repository.

2Clone และแก้ไข / Clone and Edit

git clone https://github.com/YOUR-USERNAME/repository-name.git cd repository-name git checkout -b fix-typo

Clone รีโพที่ fork มา สร้าง branch ใหม่ และแก้ไข

English: Clone your forked repository, create a new branch, and make your changes.

3Push Branch / Push Branch

git add . git commit -m "Fix typo in README" git push origin fix-typo

บันทึกและส่ง branch ของคุณไป GitHub

English: Commit your changes and push your branch to GitHub.

4สร้าง Pull Request / Create Pull Request

  1. ไปที่รีโพบน GitHub
  2. คลิก Pull requests
  3. คลิก New pull request
  4. เลือก branch ของคุณ
  5. เขียนคำอธิบายว่าเปลี่ยนอะไร
  6. คลิก Create pull request

English: On GitHub, click Pull requests, then New pull request. Choose your branch, describe your changes, and create the PR.

✍️ Good PR Description / คำอธิบาย PR ที่ดี

  • อธิบายว่าแก้ไขอะไร (What changed)
  • อธิบายว่าทำไม (Why)
  • ระบุ issue ที่เกี่ยวข้อง (Related issues)
  • ใส่ภาพหน้าจอถ้าเป็น UI (Screenshots for UI changes)

5รอการตรวจสอบ / Wait for Review

เจ้าของโปรเจกต์จะ:

  • ตรวจสอบโค้ดของคุณ
  • แสดงความคิดเห็น (Comment)
  • ขอให้แก้ไข (Request changes)
  • หรือ Merge เข้าโปรเจกต์!

English: The project owner will review your code, comment, request changes, or merge it into the project!

🎊 ยอดเยี่ยม! คุณรู้วิธีทำ Pull Request แล้ว!
Excellent! You know how to create Pull Requests!

⚠️ แก้ไข Conflicts
Resolving Conflicts

🔥 What is a Conflict? / Conflict คืออะไร?

ไทย: Conflict เกิดเมื่อมีคนแก้ไขโค้ดส่วนเดียวกันในเวลาเดียวกัน Git ไม่รู้ว่าจะเลือกเวอร์ชันไหน

English: A conflict happens when two people edit the same part of code. Git doesn't know which version to keep!

1เมื่อเกิด Conflict / When Conflict Occurs

$ git merge feature-branch Auto-merging file.txt CONFLICT (content): Merge conflict in file.txt Automatic merge failed; fix conflicts and then commit the result.

Git จะบอกว่าไฟล์ไหนมีปัญหา

English: Git tells you which files have conflicts.

2เปิดไฟล์และดู Conflict / Open File and See Conflict

<<<<<<< HEAD This is my version of the code ======= This is their version of the code >>>>>>> feature-branch

Git ใส่เครื่องหมายพิเศษเพื่อแสดงส่วนที่ขัดแย้ง:

  • <<<<<<< HEAD = โค้ดเวอร์ชันของคุณเริ่มต้น
  • ======= = ตัวแบ่ง
  • >>>>>>> branch-name = โค้ดเวอร์ชันอื่นจบ

English: Git marks the conflicting sections with special markers showing both versions.

3แก้ไข Conflict / Fix the Conflict

ลบเครื่องหมายพิเศษและเลือกโค้ดที่ถูกต้อง:

This is the correct combined version of the code

English: Delete the markers and keep the correct code. You can keep one version, the other, or combine both!

4บันทึกและ Commit / Save and Commit

git add file.txt git commit -m "Resolve merge conflict"

หลังแก้ไขเสร็จ ให้ add และ commit ตามปกติ

English: After fixing, add and commit the resolved file.

💡 Preventing Conflicts / ป้องกัน Conflicts

  • Pull บ่อยๆ เพื่อดึงโค้ดล่าสุด
  • สื่อสารกับทีมว่าใครทำอะไร
  • แบ่งงานให้คนละส่วน
  • Commit บ่อยๆ เป็นชิ้นเล็กๆ

English: Pull often, communicate with your team, divide work clearly, and commit frequently in small pieces!

Best Practices
แนวปฏิบัติที่ดี

📝 Commit Messages / ข้อความ Commit

แนวปฏิบัติที่ดี:

  • ใช้กริยาปัจจุบัน: "Add" ไม่ใช่ "Added"
  • ชัดเจนและสั้น (50 ตัวอักษร)
  • อธิบายว่า "อะไร" และ "ทำไม"
  • ใช้ภาษาอังกฤษในโปรเจกต์ระหว่างประเทศ
# Good / ดี git commit -m "Add user authentication feature" git commit -m "Fix navigation menu bug on mobile" git commit -m "Update README with installation steps" # Bad / ไม่ดี git commit -m "update" git commit -m "fix" git commit -m "changes"

🌿 Branch Naming / การตั้งชื่อ Branch

รูปแบบที่นิยม:

  • feature/feature-name - ฟีเจอร์ใหม่
  • bugfix/bug-description - แก้บั๊ก
  • hotfix/urgent-fix - แก้เร่งด่วน
  • docs/update-readme - เอกสาร
# Examples / ตัวอย่าง feature/user-login bugfix/payment-error hotfix/security-patch docs/api-documentation

📁 .gitignore File

ไฟล์ .gitignore บอก Git ว่าอย่าติดตามไฟล์ไหนบ้าง:

# ไฟล์ที่ไม่ควร commit / Files not to commit node_modules/ .env *.log .DS_Store dist/ build/ *.pyc __pycache__/

English: The .gitignore file tells Git which files to ignore, like dependencies, environment variables, and build files.

💪 เยี่ยมมาก! คุณรู้วิธีใช้ Git และ GitHub แบบมืออาชีพแล้ว!
Amazing! You know how to use Git and GitHub professionally!

🎓 สรุป
Summary

✅ สิ่งที่คุณได้เรียนรู้ / What You Learned

  • ติดตั้งและตั้งค่า Git บนเครื่อง
  • ทำงานกับ Branches
  • คำสั่ง Git ที่สำคัญทั้งหมด
  • สร้าง Pull Requests
  • แก้ไข Merge Conflicts
  • Best Practices สำหรับมืออาชีพ

English: You learned how to install Git, work with branches, use essential commands, create pull requests, resolve conflicts, and follow best practices!

🚀 Next Level / ระดับถัดไป

เรียนรู้ต่อไป:

  • Git Rebase และ Cherry-pick
  • Git Stash และ Reflog
  • GitHub Actions (CI/CD)
  • Git Hooks
  • Advanced merging strategies
  • Contributing to open source projects

🌟 Congratulations! / ยินดีด้วย!

ไทย: คุณมีความรู้ Git และ GitHub เพียงพอที่จะทำงานในทีมได้แล้ว! ต่อไปฝึกฝนโดยการสร้างโปรเจกต์และมีส่วนร่วมใน open source!

English: You now have enough Git and GitHub knowledge to work in a team! Practice by creating projects and contributing to open source!

💻 Happy Coding! / สนุกกับการเขียนโค้ด! 💻

Made with 💙 for Thai English Learners

สร้างด้วย 💙 สำหรับคนไทยที่กำลังเรียนภาษาอังกฤษ

📱 Keep pressing 🔊 to practice pronunciation!

กดปุ่ม 🔊 ต่อไปเพื่อฝึกการออกเสียง!