วันนี้ขอจดบันทึกการ import main branch จาก Bitbucket มาไว้ที่ Azure repos ด้วย Powershell
โจทย์: มี branch เยอะมากที่ใช้พัฒนางานให้ลูกค้าที่ Bitbucket แต่ต้องการ import เฉพาะ main branch ไปให้ ลูกค้า ที่ Azure repos ทำไงดี
คำตอบที่ 1: สร้าง Azure repos แล้ว ดาวโหลดไฟล์ใน main branch ของ Bitbucket ไปวางไว้ใน main branch ใน Azure repos แล้ว push ก็จบแล้ว ใช่ไหมครับ
คำตอบที่ 2: บางคนเช่นผมไม่อยากใช้ท่าง่ายๆแบบนั้น อยากกดปุ่ม Import repository จาก Azure repos แล้ว import มาให้หมด แล้วค่อยลบออก ถ้าใครชอบถ้ายาก ให้ดูขั้นตอนด้านล่างนี้ได้เลยครับผม
- เข้าไปที่ Azure Repos แล้วทำตาม คู่มือของ Microsoft: https://docs.microsoft.com/en-us/azure/devops/repos/git/import-git-repository?view=azure-devops
- clone git จาก Azure Repos ลงมาที่เครื่องตัวเอง
- git checkout main
- เปิด powershell แล้วใช้คำสั่งนี้ เพื่อทำการ checkout ทุก branch
git branch -r | Select-String -Pattern “->” -NotMatch | Select-String -pattern “^ origin/” | foreach { $_ -replace ‘^ origin/’, ‘’ } | Foreach { git checkout $_ }
- ใช้คำสั่ง วนลูป ลบทุก branch บนเครื่องที่ไม่ใช่ main
git branch | %{ $_.Trim() } | ?{ $_ -ne ‘* main’ } | %{ git branch -D $_ }
- push คำสั่ง remote delete branch กลับขึ้นไปที่ Azure Repos
git branch -r | Select-String -Pattern “->” -NotMatch | Select-String -pattern “^ origin/” | foreach { $_ -replace ‘^ origin/’, ‘’ } | ?{ $_ -ne ‘main’ } | Foreach { git push origin — delete $_ }
เท่านี้เป็นอันจบขั้นตอนครับผม หวังว่าพอจะเป็นแนวทางเผื่อใครสนใจนำไปทดสอบได้นะครับ แล้วพบกันใหม่ครับ
Happy Coding!
Reference