วิธีสร้าง Git Repository ใหม่ด้วย git init และ git clone อย่างถูกต้อง

เมื่อคุณตัดสินใจใช้ Git ในโปรเจ็กต์ สิ่งแรกที่ต้องทำคือการเริ่มต้น Repository มีอยู่ 2 วิธีหลักคือ git init สำหรับโปรเจ็กต์ใหม่ และ git clone สำหรับการดาวน์โหลดโปรเจ็กต์ที่มีอยู่แล้ว บทความนี้จะอธิบายทั้งสองวิธีอย่างละเอียด พร้อมตัวอย่างจริงที่ใช้ได้ทันที

git init — เริ่มต้น Repository ใหม่จากศูนย์

git init ใช้เพื่อเริ่มต้น Git repository ใหม่ในโฟลเดอร์ที่เลือก คำสั่งนี้จะสร้างโฟลเดอร์ .git ซ่อนที่บรรจุไฟล์ทั้งหมดที่ Git ต้องการเพื่อติดตามเวอร์ชัน

วิธีใช้ git init

กรณีที่ 1: เริ่มต้นในโฟลเดอร์ที่มีอยู่แล้ว

# เข้าไปในโฟลเดอร์โปรเจ็กต์ที่มีอยู่แล้ว
cd my-project

# เริ่มต้น Git
git init

# ผลลัพธ์:
# Initialized empty Git repository in /home/user/my-project/.git/

กรณีที่ 2: สร้างโฟลเดอร์ใหม่พร้อมกัน

# สร้างโฟลเดอร์ใหม่ชื่อ my-project และเริ่มต้น Git ในคำสั่งเดียว
git init my-project

# ผลลัพธ์:
# Initialized empty Git repository in /home/user/my-project/.git/

สิ่งที่เกิดขึ้นหลังจาก git init

ls -la my-project/
# จะเห็นโฟลเดอร์ .git ซ่อน
# drwxr-xr-x  .git/

โฟลเดอร์ .git นี้คือ “hqวใจ” ของ Git ภายในประกอบด้วย:

  • HEAD: ชี้ไปยัง branch หรือ commit ปัจจุบัน
  • objects/: เก็บข้อมูลไฟล์และ commit ทั้งหมด
  • refs/: เก็บข้อมูล branch และ tag
  • config: ค่าตั้งค่า repository

⚠️ อย่าลบโฟลเดอร์ .git เด็ดขาด มิฉะนั้นคุณจะสูญเสียประวัติการเปลี่ยนแปลงทั้งหมด

ขั้นตอนหลังจาก git init

# 1. สร้าง repository
git init my-project
cd my-project

# 2. สร้างไฟล์เริ่มต้น
echo "# My Project" > README.md

# 3. เพิ่มและ commit
git add README.md
git commit -m "Initial commit"

# 4. (ถ้าเชื่อมต่อ GitHub) ตั้ง remote
git remote add origin https://github.com/username/my-project.git
git push -u origin main

git clone — ดาวน์โหลด Repository ที่มีอยู่แล้ว

git clone ใช้เมื่อต้องการใช้งานโปรเจ็กต์ที่มีอยู่แล้วบน GitHub, GitLab หรือ Server อื่น คำสั่งนี้จะดาวน์โหลดทั้งโปรเจ็กต์พร้อมประวัติทั้งหมด และตั้งค่า remote ให้อัตโนมัติ

รูปแบบคำสั่ง

git clone <URL> [<ชื่อโฟลเดอร์ปลายทาง>]

ตัวอย่างการใช้งาน

Clone ด้วย HTTPS (วิธีปกติ)

git clone https://github.com/username/repo.git
# ผล: สร้างโฟลเดอร์ชื่อ "repo" ในไดเร็กทอรี่ปัจจุบัน

Clone ด้วย SSH (ปลอดภัยกว่า แนะนำ)

git clone [email protected]:username/repo.git
# ปลอดภัยกว่า HTTPS เพราะไม่ต้องกรอก password ทุกครั้ง

Clone ลงในโฟลเดอร์ชื่อที่กำหนดเอง

git clone https://github.com/username/repo.git my-custom-name
# เก็บลงในโฟลเดอร์ชื่อ my-custom-name แทน

Clone Shallow (เฉพาะ commit ล่าสุด สำหรับเป็นต้องการแค่โค้ดล่าสุด)

git clone --depth 1 https://github.com/username/repo.git
# ดาวน์โหลดเฉพาะ commit ล่าสุด เร็วกว่าและเปลืองน้อยกว่า

Clone เฉพาะ Branch ที่ต้องการ

git clone -b develop https://github.com/username/repo.git
# Clone เฉพาะ branch "develop"

สิ่งที่ git clone ทำให้อัตโนมัติ

เมื่อสั่ง git clone Git จะทำสิ่งเหล่านี้ให้อัตโนมัติ:

  1. ดาวน์โหลดไฟล์ทั้งหมดพร้อมประวัติ commit
  2. ตั้ง remote ชื่อ origin ชี้ไปยัง URL ที่ระบุ
  3. Checkout branch หลัก (โดยทั่วไปคือ main หรือ master)
# ตรวจสอบหลัง clone
cd repo
git remote -v
# origin  https://github.com/username/repo.git (fetch)
# origin  https://github.com/username/repo.git (push)

เปรียบเทียบ git init vs git clone

คุณสมบัติ git init git clone
ใช้เมื่อ เริ่ม project ใหม่จากศูนย์ ใช้งาน project ที่มีอยู่แล้ว
Remote ต้องตั้งเอง (git remote add) ตั้ง origin อัตโนมัติ
ประวัติ เริ่มต้นใหม่ ไม่มีประวัติ ได้ประวัติทั้งหมด
ระหว่างทีม ตั้งค่าเอง เหมาะ project เดี่ยว เหมาะทำงานร่วมทีม

ตัวอย่างจริง: เริ่มต้น Project ผ่าน git init

# สร้าง Project ใหม่
mkdir my-web-project
cd my-web-project
git init

# สร้างไฟล์ .gitignore
cat > .gitignore << EOF
node_modules/
.env
*.log
EOF

# สร้างไฟล์ README
echo "# My Web Project" > README.md

# commit แรก
git add .
git commit -m "Initial project setup"

# เชื่อมต่อ GitHub
git remote add origin https://github.com/yourname/my-web-project.git
git branch -M main
git push -u origin main

ตัวอย่างจริง: เข้าร่วมทีมผ่าน git clone

# เพื่อนส่ง link มาให้อัน clone นี้
git clone https://github.com/team/project.git
cd project

# สร้าง branch สำหรับงานของตัวเอง
git checkout -b feature/my-feature

# ทำงานแล้ว push
git add .
git commit -m "Add my feature"
git push -u origin feature/my-feature

ปัญหาที่พบบ่อยและวิธีแก้ไข

git init แล้วแต่ push ไม่ได้

# เกิด error: remote origin already exists หรือ ไม่มี remote
# ตรวจสอบ
git remote -v

# ถ้าไม่มี remote: เพิ่มเข้าไป
git remote add origin https://github.com/username/repo.git

# ถ้า branch ยังเป็น master เปลี่ยนเป็น main
git branch -M main
git push -u origin main

git clone ดาวน์โหลดช้ามาก

# ปัญหา: repository ใหญ่ ดาวน์โหลดนานมาก
# วิธีแก้: ใช้ --depth 1 สำหรับเอาเฉพาะโค้ดล่าสุด
git clone --depth 1 https://github.com/username/large-repo.git

สรุป

ทั้ง git init และ git clone เป็นจุดเริ่มต้นของการทำงานกับ Git การเลือกใช้เพียงคำเดียว: ถ้าเริ่มใหม่ ใช้ git init ถ้ามีอยู่แล้ว ใช้ git clone หลังจากนั้นคุณก็พร้อมทำงานได้ทันที

สำหรับนักพัฒนาที่ต้องการเซิร์ฟเวอร์สร้าง repository ส่วนตัว หรือต้องการสภาพแวดล้อมที่มี Git ติดตั้งพร้อม Cloud VPS จาก ผู้ให้บริการโฮสติ้ง ให้คุณมีสิทธิ์เต็มในการตั้งค่าเซิร์ฟเวอร์ รองรับทีม developer ได้อย่างเต็มประสิทธิภาพ