このGitHubのリポジトリは、ある技術本のサンプルです。
ただし、これを開くと
LICENSE
README.md
だけで、ソースコードがmainブランチにありません。
このリポジトリは、mainブランチ以外のブランチに章ごと分けてサンプルが入っています。
素直にgit clone
をしても、これではmainだけをcloneして、その他のブランチは取得できません。
それぞれの章を取得するには、一つ一つ git checkoutをする必要があります。
サンプルは章ごとにあるので、何回もやるのは面倒なのでAIに聞きつつ、まとめて取得するスクリプトを書いて貰いました。
Windows版
リモートも含めたすべてのブランチ名の取得、確認
git branch -a
すべてのリポジトリをまとめて取得。
$branches = git branch -r | Where-Object { $_ -notmatch '->' -and $_ -notmatch 'HEAD' }
foreach ($branch in $branches) {
$branchName = $branch.Trim() -replace 'origin/', ''
git checkout --track "origin/$branchName"
}
本のサンプルなので、必要なときにgit checkout
すればいいという考え方なのでしょうが、サンプルをまとめてダウンロードしたかったので調べてみました。
git clone
はデフォルトブランチしかダウンロードしてくれません。
ログ
08-21 18:19:29> $branches = git branch -r | Where-Object { $_ -notmatch '->' -and $_ -n
\ebook\Building-Production-Grade-Web-Applications-with-Supabase [Ch02_AppRouter ≡
08-21 18:19:51> foreach ($branch in $branches) {
>> $branchName = $branch.Trim() -replace 'origin/', ''
>> git checkout --track "origin/$branchName"
>> }
fatal: a branch named 'Ch02_AppRouter' already exists
Switched to a new branch 'Ch02_PagesRouter'
branch 'Ch02_PagesRouter' set up to track 'origin/Ch02_PagesRouter'.
Switched to a new branch 'Ch03_TicketSystem_UI'
branch 'Ch03_TicketSystem_UI' set up to track 'origin/Ch03_TicketSystem_UI'.
Switched to a new branch 'Ch04_TicketSystem_Auth'
branch 'Ch04_TicketSystem_Auth' set up to track 'origin/Ch04_TicketSystem_Auth'.
Switched to a new branch 'Ch05_TicketSystem_MultiTenancy'
branch 'Ch05_TicketSystem_MultiTenancy' set up to track 'origin/Ch05_TicketSystem_MultiTenanc
Switched to a new branch 'Ch06_RLS_Permissions_Domains'
branch 'Ch06_RLS_Permissions_Domains' set up to track 'origin/Ch06_RLS_Permissions_Domains'.
Switched to a new branch 'Ch07_Registration'
branch 'Ch07_Registration' set up to track 'origin/Ch07_Registration'.
Switched to a new branch 'Ch08_Tickets'
branch 'Ch08_Tickets' set up to track 'origin/Ch08_Tickets'.
Switched to a new branch 'Ch09_UserList_and_Assignees'
branch 'Ch09_UserList_and_Assignees' set up to track 'origin/Ch09_UserList_and_Assignees'.
Switched to a new branch 'Ch10_Realtime'
branch 'Ch10_Realtime' set up to track 'origin/Ch10_Realtime'.
Switched to a new branch 'Ch11_Storage'
branch 'Ch11_Storage' set up to track 'origin/Ch11_Storage'.
Switched to a new branch 'Ch12_Security'
branch 'Ch12_Security' set up to track 'origin/Ch12_Security'.
Switched to a new branch 'Ch13_Superpowers'
branch 'Ch13_Superpowers' set up to track 'origin/Ch13_Superpowers'.
fatal: a branch named 'main' already exists
最後はmainブランチ
はすでにあるので失敗しています。
Ch02_AppRouter はすでにcheckout
していたので失敗しています。