特定の Git ブランチのクローンを作成する
Kevin Amayi
2023年1月30日
Git
Git Branch
- すべてのブランチをフェッチして特定のブランチにチェックアウトすることにより、リモートリポジトリから特定の Git ブランチのクローンを作成する
- リモートリポジトリから直接特定の Git ブランチのクローンを作成する

この記事では、すべてのブランチをフェッチして特定のブランチをチェックアウトすることにより、リモートリポジトリから特定の git ブランチを複製する方法について説明します。
すべてのブランチをフェッチして特定のブランチにチェックアウトすることにより、リモートリポジトリから特定の Git ブランチのクローンを作成する
master
と gh-pages
の 2つのブランチを含むリモートリポジトリのクローンを作成してから、gh-pages
ブランチに切り替えます。
<!-- The command to use is -->
git clone <remote-repo-url>
<!-- From your terminal run -->
git clone https://github.com/KEVINAMAYI/AkanNameGenerator.git
出力:
Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 1.86 MiB/s, done.
プロジェクトフォルダに入り、以下のコマンドを使用して利用可能なブランチを一覧表示します。
<!-- get into project folder -->
cd AkanNameGenerator
<!-- List branches available -->
git branch -a
出力:
<!-- The asterix indicates we are on branch main -->
* main
remotes/origin/HEAD -> origin/main
remotes/origin/gh-pages
remotes/origin/main
以下のコマンドを使用して、特定のブランチ gh-pages
に切り替えます。
git checkout gh-pages
次のコマンドを実行して、特定のブランチ gh-pages
にいることを確認します。
git branch
出力:
<!-- The asterix indicates we are now on branch gh-pages -->
* gh-pages
main
リモートリポジトリから直接特定の Git ブランチのクローンを作成する
Git コマンドでブランチ名を指定することにより、リモートリポジトリから直接必要な特定のブランチのクローンを作成します。
<!-- The command to use is -->
git clone --branch <branchname> --single-branch <remote-repo-url>
<!-- From your terminal run -->
git clone --branch gh-pages --single-branch https://github.com/KEVINAMAYI/AkanNameGenerator.git
出力:
Cloning into 'AkanNameGenerator'...
remote: Enumerating objects: 94, done.
remote: Total 94 (delta 0), reused 0 (delta 0), pack-reused 94
Unpacking objects: 100% (94/94), 2.38 MiB | 231.00 KiB/s, done.
プロジェクトフォルダに入り、以下を実行して利用可能なブランチを一覧表示します。
<!-- get into project folder -->
cd AkanNameGenerator
<!-- List branches available -->
git branch -a
出力:
<!-- we have only our specific branch -->
* gh-pages
チュートリアルを楽しんでいますか? <a href="https://www.youtube.com/@delftstack/?sub_confirmation=1" style="color: #a94442; font-weight: bold; text-decoration: underline;">DelftStackをチャンネル登録</a> して、高品質な動画ガイドをさらに制作するためのサポートをお願いします。 Subscribe