ย้ายจาก SVN มาใช้ Git: คู่มือ Migration สำหรับทีมที่ใช้ระบบเก่า
ระบบควบคุมเวอร์ชัน SVN (Subversion) เคยเป็นตัวเลือกยอดนิยม แต่ปัจจุบัน Git ได้กลายเป็นมาตรฐานของวงการ Development คู่มือนี้จะช่วยให้ทีมของคุณย้ายจาก SVN ไป Git อย่างปลอดภัย โดยรักษา History ของโปรเจกต์ไว้อย่างสมบูรณ์
ทำไมต้องย้ายจาก SVN มาใช้ Git?
- Git มี Community ที่ใหญ่และ Tools ที่หลากหลายกว่า SVN
- Branch ใน Git ดีและเร็วกว่า SVN มากโดยไม่ต้องสร้างไดเรกทอรี่เพิ่มเติม
- Git สนับสนุนการทำงาน Offline ได้ดี ขณะที่ SVN ต้องเชื่อมต่อ Server ตลอดเวลา
- GitHub, GitLab, Bitbucket เป็นแพลตฟอร์มโปรดของทีมสมัยใหม่
- Git ถูกผนวกเข้าในเครื่องมือพัฒนา IDE เกือบทุกตัว
ความแตกต่างพื้นฐานระหว่าง SVN และ Git
| ลักษณะ | SVN | Git |
| ประเภท | Centralized | Distributed |
| Repository | ต้องเชื่อมต่อ Server | Local + Remote |
| Branch | หนักและช้า | เบาและเร็ว |
| Merge | ยุ่งยากกว่า | อัตโนมัติและยอดเยี่ยม |
| Offline | ต้องมี Connection | ทำได้ทั้งหมด |
ขั้นตอนการ Migrate จาก SVN ไป Git
1. เตรียม SVN Authors File
ก่อนอื่น คุณต้องสร้าง authors file ที่แมป SVN username ไปยัง Git user format:
svnadmin dump /path/to/svn-repo | svndump-analyze.py > authors.txt
# หรือสร้างด้วยมือ
john = John Doe <[email protected]>
jane = Jane Smith <[email protected]>
admin = Admin User <[email protected]>
2. Clone SVN Repository ด้วย git svn
ใช้คำสั่ง git svn clone เพื่อ Convert SVN repository ไป Git โดยรักษา History ไว้:
# Clone SVN repository พร้อม branches และ tags
git svn clone --stdlayout \
--authors-file=authors.txt \
https://svn.example.com/repo/project \
project-git
# หากโครงสร้าง SVN ไม่ใช่ standard layout (trunk/branches/tags)
git svn clone \
--authors-file=authors.txt \
https://svn.example.com/repo/project \
project-git
คำสั่งนี้อาจใช้เวลาพอสมควรขึ้นอยู่กับขนาดของ Repository และจำนวน Commits
3. ทำความสะอาด Repository ที่ Converted
หลังจาก Clone เสร็จ ให้ทำความสะอาด metadata ของ SVN:
cd project-git
# ลบ svn-remote reference
git config --remove-section svn-remote.svn
# ลบ local SVN branches
git branch -r | grep "svn" | xargs git branch -rd
# ทำความสะอาด reflog
git reflog expire --expire=now --all
git gc --aggressive --prune=now
4. ตั้งค่า Upstream Remote Repository
เพิ่ม Remote repository (GitHub, GitLab, Gitea) ที่คุณต้องการใช้:
# เพิ่ม remote
git remote add origin https://github.com/yourcompany/project.git
# Push ทั้งหมด (branches และ tags)
git push -u origin --all
git push -u origin --tags
5. ตรวจสอบ Branches และ Tags
ตรวจสอบว่า Branches และ Tags ได้ย้ายมาอย่างถูกต้อง:
git branch -a # ดู branches ทั้งหมด
git tag -l # ดู tags ทั้งหมด
git log --oneline # ตรวจสอบ commit history
การจัดการเมื่อ SVN Repository มีขนาดใหญ่
หากมี History ที่ยาวมากหรือไฟล์ขนาดใหญ่ ให้พิจารณาวิธีเหล่านี้:
Shallow Clone (ไม่รักษา History เต็ม)
# Clone เฉพาะ N commits ล่าสุด
git svn clone --stdlayout \
--authors-file=authors.txt \
-r {OLDEST_REVISION}:HEAD \
https://svn.example.com/repo/project \
project-git
Incremental Migration
หาก SVN repository มีประวัติยาวมากที่สุด ให้พิจารณาแบ่งออกเป็นส่วนเล็ก ๆ:
# ทำการ Migrate เฉพาะส่วนที่จำเป็น
git svn clone --stdlayout \
--authors-file=authors.txt \
-r 100:200 \
https://svn.example.com/repo/project \
project-git-part1
การอัปเดต Authors File ให้ถูกต้อง
ห้าม Commits ขณะที่ Authors file ไม่สมบูรณ์ เพราะจะทำให้ Commits มีชื่อเก่า:
# ดึงรายชื่อผู้ใช้ทั้งหมดจาก SVN
svn log https://svn.example.com/repo/project \
| grep "^r" | awk '{print $3}' | sort | uniq
# แล้วสร้าง authors.txt ให้ครบถ้วน
ปัญหาและวิธีแก้ไขทั่วไป
ปัญหา: git svn clone ช้า
ใช้ตัวเลือก –no-minimize-url เพื่อลดการเชื่อมต่อ:
git svn clone --stdlayout \
--authors-file=authors.txt \
--no-minimize-url \
https://svn.example.com/repo/project \
project-git
ปัญหา: Characters ทั่วไป (BOM, Line Endings)
ตรวจสอบและแก้ไข Line Endings ในไฟล์ก่อน Push:
# ตั้งค่า .gitattributes
echo "* text=auto" > .gitattributes
git add .gitattributes
git commit -m "Add gitattributes"
การอัปเดต Team ในการใช้ Git
- ให้อบรม: ควรจัดอบรมให้ทีมเข้าใจพื้นฐาน Git, Branch, Merge เบื้องต้น
- สร้าง Git Workflow: เลือก Git Flow, GitHub Flow หรือ Trunk-Based Development
- ตั้ง Branch Protection: ปกป้อง main/master branch ต้องผ่าน Pull Request Review
- สร้าง Documentation: เขียนคู่มือ Git สำหรับเฉพาะทีมของคุณ
สรุป
การย้ายจาก SVN มาใช้ Git เป็นการตัดสินใจที่ดี มันให้ความยืดหยุ่น ประสิทธิภาพ และการสนับสนุนจากชุมชนที่มากมาย ด้วยการปฏิบัติตามขั้นตอนในคู่มือนี้ ทีมของคุณจะสามารถยุติการใช้ SVN และย้ายไป Git ได้อย่างราบรื่น โดยรักษา History ที่มีค่าไว้อย่างสมบูรณ์
