git clone -bで、特定のブランチを指定してクローンする方法
git clone -b <ブランチ名> <リポジトリ> を使うと、clone した直後に、指定したブランチがチェックアウトされた状態になります。ふつうに clone すると、手元のブランチは main(取り出し元の HEAD が指すブランチ)になるので、develop などの開発用のブランチや、リリース用のブランチで作業を始めたいときは、clone のあとに git switch で切り替える手間がかかります。-b を付ければ、その手間が要りません。検証環境でコマンドを実行し、操作の前と後の出力をそのまま載せています。
検証環境は Ubuntu 24.04、Git 2.43.0 です。作者の名前とメールアドレスには例示用の値(Neko <neko@example.com>)を設定し、コミットの日時は環境変数で決めています。取り出し元のリポジトリ(neko-repo)は同じ検証環境の中に置き、パスで指定して clone しました。GitHub などのサーバーから clone したときの動き、Windows と macOS での動作は試していません。
試したリポジトリ
neko-repo には、main、develop、release/1.0 の3つのブランチと、タグ v1.0 があります。取り出し元で今いるブランチ(HEAD)は main です。
$ git log --oneline --all --graph --decorate
* 0055a91 (release/1.0) 1.0 の修正
| * 5eda3ea (develop) 開発中の機能
| * ed8e72b (HEAD -> main) main の更新
|/
* 8ee5f47 (tag: v1.0) 最初のコミット
clone する前に、取り出し元にどんなブランチとタグがあるかは git ls-remote で確かめられます。--heads でブランチ、--tags でタグに絞って表示します。-b に渡すのは、ブランチなら refs/heads/ より後ろ、タグなら refs/tags/ より後ろの名前です。
$ git ls-remote --heads --tags neko-repo
5eda3ead0b88b7b517ccef3d8e9ec1e6cddf0fff refs/heads/develop
ed8e72bcc3ced6b9e8463f4e901c64449e0e6bc2 refs/heads/main
0055a91bfc131875788bd4f5f705a3e041c547ad refs/heads/release/1.0
8ee5f4721173205d2da5f8e5204156af5b3307d5 refs/tags/v1.0
-b を付けずに clone した場合
比べるために、まず -b を付けずに clone しました。手元のブランチは main だけで、作業フォルダーには main のファイル(version.txt)だけがあります。ほかのブランチは、リモート追跡ブランチ(origin/develop など)として取得されています。
$ git clone neko-repo neko-default
Cloning into 'neko-default'...
done.
$ git -C neko-default branch
* main
$ git -C neko-default branch -r
origin/HEAD -> origin/main
origin/develop
origin/main
origin/release/1.0
$ ls neko-default
version.txt
-b で develop を指定して clone する
-b develop を付けて clone しました。手元のブランチは develop になり、git status には origin/develop と同じ状態だと表示されました。作業フォルダーには、develop で追加した develop.txt もあります。
$ git clone -b develop neko-repo neko-develop
Cloning into 'neko-develop'...
done.
$ cd neko-develop
$ git branch
* develop
$ git status
On branch develop
Your branch is up to date with 'origin/develop'.
nothing to commit, working tree clean
$ git log --oneline
5eda3ea 開発中の機能
ed8e72b main の更新
8ee5f47 最初のコミット
$ ls
develop.txt
version.txt
git clone の公式リファレンスによると、-b は、取り出し元の HEAD が指すブランチの代わりに、指定したブランチを新しいリポジトリの HEAD にするオプションです。--branch と書いても同じです。release/1.0 のように / を含む名前も、そのまま指定できました。
$ git clone -q --branch release/1.0 neko-repo neko-release
$ git -C neko-release branch
* release/1.0
$ git -C neko-release log --oneline
0055a91 1.0 の修正
8ee5f47 最初のコミット
ほかのブランチも取得されている
-b を付けても、取得されるのは指定したブランチだけではありません。neko-develop でもリモート追跡ブランチは4つとも揃っていて、git fetch で取得するブランチの設定(remote.origin.fetch)も、すべてのブランチ(*)のままです。origin/HEAD は、取り出し元の HEAD に合わせて、origin/main を指していました。
$ git branch -r
origin/HEAD -> origin/main
origin/develop
origin/main
origin/release/1.0
$ git config --get remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
$ git rev-parse --abbrev-ref origin/HEAD
origin/main
そのため、あとから main に切り替えるときは git switch main だけで済みます。手元に main ブランチはありませんが、origin/main があるので、それを追跡する main ブランチが作られました。git switch の公式リファレンスによると、これは --guess という動きで、標準で有効です。
$ git switch main
Switched to a new branch 'main'
branch 'main' set up to track 'origin/main'.
$ git branch
develop
* main
1つのブランチだけを取得するなら –single-branch も付ける
ほかのブランチが要らないときは、-b に --single-branch を付けます。リモート追跡ブランチは origin/develop だけになり、remote.origin.fetch も develop だけになりました。公式リファレンスの説明でも、--single-branch で取得されるのは、指定したブランチの先頭からたどれる履歴だけです。clone のあとの git fetch で更新されるのも、そのブランチに限られます。
$ git clone -q -b develop --single-branch neko-repo neko-single
$ git -C neko-single branch -r
origin/develop
$ git -C neko-single config --get remote.origin.fetch
+refs/heads/develop:refs/remotes/origin/develop
$ git -C neko-single log --oneline --all
5eda3ea 開発中の機能
ed8e72b main の更新
8ee5f47 最初のコミット
git log --all に main の更新 が出ているのは、develop の履歴の一部だからです。release/1.0 にしかない 1.0 の修正 のコミットは、取得されていません。
この状態では、main に切り替えようとしても origin/main がないので、エラーになりました。
$ git -C neko-single switch main
fatal: invalid reference: main
あとから main も必要になったときは、git remote set-branches --add で、取得するブランチに main を足してから git fetch します。origin/main が取得され、git switch main で切り替えられました。
$ git -C neko-single remote set-branches --add origin main
$ git -C neko-single config --get-all remote.origin.fetch
+refs/heads/develop:refs/remotes/origin/develop
+refs/heads/main:refs/remotes/origin/main
$ git -C neko-single fetch -q
$ git -C neko-single branch -r
origin/develop
origin/main
$ git -C neko-single switch main
Switched to a new branch 'main'
branch 'main' set up to track 'origin/main'.
git clone の公式リファレンスの --depth の説明によると、--depth は --single-branch を含みます。そのため、--depth 1 で最新のコミットだけを取得するときは、--single-branch を付けなくても1つのブランチだけになります。
-b にタグを指定すると、ブランチのない状態になる
-b には、ブランチではなくタグの名前も指定できます。-b v1.0 で clone すると、detached HEAD(どのブランチにもいない状態)になったという案内が出ました。git status にも、ブランチにいないことが表示されます。
$ git clone -b v1.0 neko-repo neko-tag
Cloning into 'neko-tag'...
done.
Note: switching to '8ee5f4721173205d2da5f8e5204156af5b3307d5'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by switching back to a branch.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -c with the switch command. Example:
git switch -c <new-branch-name>
Or undo this operation with:
git switch -
Turn off this advice by setting config variable advice.detachedHead to false
$ git -C neko-tag status
Not currently on any branch.
nothing to commit, working tree clean
$ git -C neko-tag branch
* (no branch)
$ git -C neko-tag log --oneline --decorate
8ee5f47 (HEAD, tag: v1.0) 最初のコミット
公式リファレンスにも、--branch にタグを渡すと、そのコミットで HEAD を切り離す(detached にする)と書かれています。タグの時点のファイルを見るだけなら、このままで構いません。そこから修正を始めるときは、案内のとおり git switch -c でブランチを作ってからコミットします。
$ git -C neko-tag switch -c fix-from-1.0
Switched to a new branch 'fix-from-1.0'
$ git -C neko-tag branch
* fix-from-1.0
取り出し元にない名前を指定したときのエラー
取り出し元にない feature を指定すると、Remote branch feature not found in upstream origin(取り出し元に feature というブランチがない)というエラーで止まりました。clone 先のフォルダー neko-none は残っていません。
$ git clone -b feature neko-repo neko-none
Cloning into 'neko-none'...
fatal: Remote branch feature not found in upstream origin
$ ls -d neko-none
ls: cannot access 'neko-none': No such file or directory
このエラーが出たら、前に示した git ls-remote --heads でブランチ名を確かめます。フォルダーは残らないので、消す手間なく、ブランチ名を直したコマンドをそのまま実行し直せます。




