ここには、次のような十分な回答があります。
1つのブランチをダウンロードします(一部を含む--single-branch
):
# (We'll refer to this as "the 1st command" below.)
# 1. Clone ONLY `branch_name`, then check it out. The `--single-branch` part
# means that ONLY `branch_name` is cloned, fetched, pulled, and
# tracked. _No other remote branches will be cloned to your PC
# whatsoever._
git clone -b branch_name --single-branch \
https://github.com/some_project/some_project.git
...またはそのいくつかのバージョン、およびちょうど言及しているいくつかのバージョン:
すべてのブランチをダウンロードします(--single-branch
パーツなし):
# (We'll refer to this as "the 2nd command" below.)
# 2. Clone ALL remote branches, then `git checkout` the `branch_name`
# branch.
git clone -b branch_name \
https://github.com/some_project/some_project.git
しかし、私はこれら2つのことを少し詳しく説明し、より身近な同等のコマンドのセットを示して、それぞれの内部で何が起こっているかを確認できるようにしたいと思います。
https://github.com/micronucleus/micronucleus.gitのGitHubに、リモートブランチmaster
とversion_2.5
(これは実際に今すぐ実行できる実際の例です)リモートリポジトリがあると仮定します。
上から2番目のコマンドの内訳:
2番目のコマンド(git clone -b version_2.5 https://github.com/micronucleus/micronucleus.git
)は、すべてのリモートブランチをローカルPCにクローンしますが、version_2.5
ブランチではなくブランチをチェックアウトしmaster
ます。その1つのコマンドは、これを行うのと同じです。
git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus # cd into the repo you just cloned
git checkout version_2.5
# To be pedantic, also delete the local `master` branch since
# technically it won't exist yet since you haven't yet checked
# it out with `git checkout master`, which would create it from
# your locally-stored remote-tracking branch, `origin/master`
git branch -d master
パーツは、の代わりにブランチを-b version_2.5
自動的にチェックアウトしました。version_2.5
master
git branch -a
ただし、すべてのブランチがローカルPCに複製されたことを示しています。ここでは、私たちが所属しているローカルブランチversion_2.5
、ローカルに保存されているリモートトラッキングブランチ origin/HEAD
(を指すorigin/master
)、およびorigin/master
、を見ることができorigin/version_2.5
ます。
$ git branch -a
* version_2.5
remotes/origin/HEAD -> origin/master
remotes/origin/master
remotes/origin/version_2.5
また、参照が何であるかを確認することもできますfetch
。ファイルを開いて.git/config
直接表示するか、次のコマンドを実行することができgit config remote.origin.fetch
ます。
$ git config remote.origin.fetch
+refs/heads/*:refs/remotes/origin/*
上記のgit fetch
コマンド(これはgit pull
と同等であるため、によってトリガーされgit fetch && git merge
ます)は、リモートのすべてのブランチのすべてのヘッドをフェッチするように構成されていることがわかりますorigin
。私はこの部分の専門家ではありませんが、それが+refs/heads/*:refs/remotes/origin/*
意味することだと思います。
上からの最初のコマンドの内訳:
最初のコマンド(git clone -b version_2.5 --single-branch https://github.com/micronucleus/micronucleus.git
)は、version_2.5
ブランチのみをローカルPCに複製し、それもチェックアウトします。その1つのコマンドは、これを実行するのと同じです(少なくとも、すべてではなく1つのブランチのみを複製するため、最初にダウンロードするデータがはるかに少ないことを除いて、最終結果では):
git clone https://github.com/micronucleus/micronucleus.git
cd micronucleus # cd into the repo you just cloned
git checkout version_2.5
# Delete ALL other branches, including remote-tracking ones, which are not the
# `version_2.5` branch:
# One local branch
git branch -d master
# ALL other locally-stored remote-tracking branches
git branch -dr origin/HEAD
git branch -dr origin/master
# Fix your `.git/config` file so that `git fetch` does the right thing, fetching
# ONLY the `version_2.5` branch head from the `origin/version_2.5` remote branch:
git config remote.origin.fetch \
"+refs/heads/version_2.5:refs/remotes/origin/version_2.5"
この-b version_2.5
部分により、デフォルトでブランチversion_2.5
の代わりにブランチがチェックアウトさmaster
れ(前述のとおり)、その--single-branch
部分によって次のことが発生しました。
- PCに複製される他のブランチはありません。
git fetch
またはを呼び出したときに他のブランチがフェッチされないように構成する必要があります。git fetch
git pull
このコマンドは本当に複製され、必要なブランチを1つだけフェッチします。これで完了です。
git branch -a
version_2.5
ブランチのみが複製され、チェックアウトされたことを示しています。ここでは、*
どのブランチがチェックアウトされているかがわかります。また、ローカルに保存されたリモートトラッキングブランチがあることもわかりorigin/version_2.5
ます。
$ git branch -a
* version_2.5
remotes/origin/version_2.5
また、参照が何であるかを確認することもできますfetch
。ファイルを開いて.git/config
直接表示するか、次のコマンドを実行することができgit config remote.origin.fetch
ます。
$ git config remote.origin.fetch
+refs/heads/version_2.5:refs/remotes/origin/version_2.5
上記のように、コマンドはリモートブランチからブランチヘッドgit fetch
のみをフェッチします。それでおしまい!他のリモートブランチがフェッチされないことに注意してください。version_2.5
origin/version_2.5
概要:
したがって、-b branch_name
基本的に使用するbranch_name
と、クローンの後にブランチがチェックアウトされますが、すべてのリモートブランチがクローンされますが、追加--single-branch
すると、ONLYbranch_name
がクローン、フェッチ、プル、および追跡されます。他のリモートブランチは、PCに複製されません。
個人的には、すべてのブランチをローカルPCに複製し-b branch_name
たいので、このオプションだけを好みます。唯一の例外は、数十、さらには数百または数千のリモートブランチを持つ巨大な共有モノレポの場合です。その場合は、使用して、気になる1つのメインブランチだけを複製して実行します。たとえば、巨大なモノレポジトリでブランチ用に50 GiBのデータをダウンロードする方が、200 GiBのデータをダウンロードするよりも優れているため、ピアのブランチを2000個もダウンロードできます。-b branch_name --single-branch
master
参照:
- ブランチを1つだけクローンする
- Gitでリモートブランチの追跡を停止するにはどうすればよいですか?