ย้าย Repository จาก GitHub ไป GitLab (หรือกลับกัน) โดยไม่เสีย History

ทำไมต้องย้าย Repository ระหว่าง Git Hosting?

การย้าย Repository ระหว่าง GitHub ไป GitLab (หรือแพลตฟอร์มอื่นๆ) เป็นเรื่องที่อาจเกิดขึ้นในสถานการณ์จริง เช่น การเปลี่ยนแผน subscription เพื่อลดต้นทุน การต้องการเซิร์ฟเวอร์ self-hosted หรือการเปลี่ยนไปใช้บริการใหม่ที่มีฟีเจอร์ดีกว่า บทความนี้จะแสดงวิธีย้าย Repository อย่างปลอดภัยโดยไม่สูญเสีย Commit history, branches, tags ทั้งหมด

วิธี 1: ใช้ git clone –mirror (ผลลัพธ์ที่สมบูรณ์ที่สุด)

วิธีนี้เป็นวิธีที่ดีที่สุด เพราะมันสำเนา Repository ทั้งหมด รวมถึง branches ทั้งหมด, tags, reflog, และ notes

# Step 1: Clone แบบ mirror จาก source repository
git clone --mirror https://github.com/old-user/old-repo.git

# ขั้นตอนนี้จะสร้าง old-repo.git folder เป็น bare repository

# Step 2: Push ไปยัง target repository (GitLab, Gitea, หรือ VPS ของคุณ)
cd old-repo.git
git push --mirror https://gitlab.com/new-user/new-repo.git

# Step 3: ตรวจสอบว่า branches ทั้งหมดมาถึง
git branch -a

# Step 4: Clone แบบปกติจาก new repository
cd /tmp
git clone https://gitlab.com/new-user/new-repo.git
cd new-repo
git log --oneline -5

# Step 5: ตรวจสอบ tags
git tag -l

วิธี 2: ใช้ git push –mirror (ตรงไปตรงมา แต่ต้อง repo ว่าง)

วิธีนี้เหมาะหากคุณมี access ทั้งสองฝั่งและต้องการวิธีที่เร็ว

# Step 1: สร้าง bare repository ชั่วคราว
cd /tmp
git clone --mirror https://github.com/old-user/old-repo.git temp-repo.git

# Step 2: Push ทั้งหมดไปยัง target
cd temp-repo.git
git push --mirror https://gitlab.com/new-user/new-repo.git

# สำคัญ: Target repository ต้อง Empty ก่อน!
# หากไม่ว่าง ให้ push แบบ force (อย่างไรก็ตาม ระมัดระวัง!)
git push --mirror --force https://gitlab.com/new-user/new-repo.git

วิธี 3: ถ้าต้องการย้าย Issues, Pull Requests, Wiki (ต้องขั้นตอนพิเศษ)

ถ้าคุณต้องการย้ายข้อมูลเพิ่มเติมนอกเหนือจาก code, commits, branches และ tags คุณต้องทำขั้นตอนเพิ่มเติม

# GitHub → GitLab: ใช้ GitHub's official exporter
# เข้าไปที่ GitHub settings → Export repository
# สำหรับ Issues และ Pull Requests ต้องใช้ API scripts

# หรือใช้ Tools:
# - github2gitlab (Python script)
# - git-migration-tools
# - Migrate2Gitlab (GitLab built-in tool)

ตรวจสอบ Repository หลังย้าย (Verification Checklist)

  • Commits: ตรวจสอบว่า commit count เหมือนเดิม
    git log --oneline | wc -l
  • Branches: ดู branches ทั้งหมด
    git branch -a
  • Tags: ตรวจสอบ version tags
    git tag -l
  • Remote: เปลี่ยน remote URL
    git remote set-url origin https://gitlab.com/new-user/new-repo.git
  • Test Push: ลองสร้าง branch ใหม่และ push
    git checkout -b test-migration && git push -u origin test-migration

Best Practices เมื่อย้าย Repository

  • Backup: เก็บ backup ของ old repository ก่อนเสมอ
  • Communication: แจ้งให้ทีมรู้ว่าจะย้าย repository เมื่อไหร่
  • Documentation: อัปเดต README.md, CI/CD config, team documentation เพื่อให้ชี้ไปยัง repository ใหม่
  • Staging: ถ้าเป็นไปได้ ทดสอบด้วย repository ทดลองก่อน production
  • Verify: ตรวจสอบ branches, tags, commits ให้ครบถ้วนก่อนแจ้งให้ทีมใช้ repository ใหม่
  • Cleanup: ลบ old repository หลังจากยืนยันว่า migration สำเร็จแล้ว (หรือเก็บไว้เป็น archive)

Update Documentation หลังจากย้าย

# อัปเดต documentation ต่างๆ:
# 1. README.md - เปลี่ยน clone URL
# 2. Contributing guidelines - อัปเดต workflow docs
# 3. CI/CD configuration - แน่ใจว่า webhooks ชี้ไปยัง new repo
# 4. Team wiki - บันทึก URL ใหม่
# 5. Deployment scripts - อัปเดต repository URLs
# 6. Docker CI files - เปลี่ยน git URLs หากมี

สรุป

ย้าย Git repository ระหว่างแพลตฟอร์มนั้นไม่ยาก หลักสำคัญคือใช้ git clone --mirror + git push --mirror เพื่อให้แน่ใจว่าข้อมูลทั้งหมดย้ายไปอย่างสมบูรณ์ หากคุณใช้ ผู้ให้บริการโฮสติ้ง Cloud VPS หรือ self-hosted Git server ก็สามารถทำขั้นตอนเดียวกันนี้ได้ เพียงแต่เปลี่ยน URL จากแพลตฟอร์มที่ใช้ไปยัง VPS ของคุณเท่านั้น