0
0

git cloneでは取得できなかったブランチをgit checkoutでまとめて取得。

Last updated at Posted at 2024-08-21

この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していたので失敗しています。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0