0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

指定ブランチを git clone する

Posted at
動作環境
デバイス Windows10/WSL/Ubuntu
エディタ VSCode
Webサービス GitHub

社用PCのため、セキュリティが厳しく、SSH使用不可。
今回クローンする内容自体は機密事項ではないため、HTTPS版URLを使用する。

git clone で起こった事象

単純に git clone したら、デフォルト(main)のリポジトリがクローンされた。

bash
git clone https://github.com/"ユーザー名"/"リポジトリ名".git

改善したい事象

main ブランチではなく 指定のブランチ(例:develop)をクローンしたい。

実践したこと

1. develop ブランチをローカルにチェックアウト

※初回はリモートブランチからローカルブランチを作るので git checkout -b でもOKらしい。

bash
git checkout develop
エラーコード
fatal: not a git repository (or any of the parent directories): .git

develop ブランチは存在しないようだった。

2. 最初から特定のブランチだけをクローンする

一度クローンしたリポジトリを削除し、新しくクローンしなおすことにした。

bash
git clone -b develop --single-branch https://github.com/"ユーザー名"/"リポジトリ名".git

意味

  • -b develop:このブランチをチェックアウト
  • --single-branch:他のブランチはフェッチしない(軽量化できる)

この方法で問題なくdevelopブランチがクローン出来た。

解決!


今回は試していないが、検索でヒットした方法を以下に記録しておく。

[Tips]複数ブランチを取得したい場合

bash
git clone https://github.com/"ユーザー名"/"リポジトリ名".git
git fetch origin      # リモートリポジトリの最新状態を取得する(ローカルには影響しない)
git branch -a         # すべてのブランチを確認
git checkout develop  # developブランチへ切り替え
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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?