Git clone将远程分支克隆到本地。
有没有办法在不切换远程仓库的分支的情况下,自己克隆特定的分支?
git clone -b <branch> <remote_repo>
示例:
git clone -b my-branch git@github.com:user/myproject.git
使用Git 1.7.10及更高版本,添加--single-branch
以防止获取所有分支。例如,使用OpenCV 2.4分支:
git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git
git clone --single-branch --branch <branchname> <remote-repo>
--single-branch
选项适用于版本1.7.10及以上版本。
请参见另一个答案,许多人更喜欢使用该答案。
您可能还想确保了解区别。区别在于:通过调用git clone --branch <branchname> url
,您正在获取所有分支并检出其中一个。这可能意味着您的存储库具有5kB文档或wiki分支和5GB数据分支。每当您想要编辑首页页面时,您可能会克隆5GB的数据。
再次强调,并不是说git clone --branch
不能完成此操作,只是当您询问克隆特定分支时,它并不总是您想要实现的方式。
git fetch
不会检出文件,但这与传输无关。 - Michael Krelin - hacker--depth 1
这个参数,这样你只会得到最新的版本,从而节省大量下载时间。 - Eyalgit clone -b branch_name --single-branch 'repo_url'
- Aditya Rajgor以下是一种非常简单的方法 :)
克隆该存储库
git clone <repository_url>
列出所有分支。git branch -a
检出您想要的分支
git checkout <name_of_branch>
要克隆一个分支而不获取其他分支:
mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
remote add
和checkout
就足够了,然后使用git remote rm origin
进行清理。) - Jesse Glick-f
,然后使用 git fetch --depth=1 $BRANCH $TAG
,然后使用 git checkout FETCH_HEAD
。初始化是无害的,更改标签将自动更新已检出的代码。 - taranakigit版本2.9.2
- Budi Mulyo使用:
git checkout -b <branch-name> <origin/branch_name>
例如在我的情况下: git branch -a
* master
origin/HEAD
origin/enum-account-number
origin/master
origin/rel_table_play
origin/sugarfield_customer_number_show_c
要基于我的enum-account-number分支创建一个新分支,我需要执行以下操作:
git checkout -b enum-account-number origin/enum-account-number
按下Return键后,会发生以下情况:
Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number"
git pull origin
可能很有用,这样 git branch -a
就可以列出所有新的(当前的)远程分支。 - André Carongit fetch
,这样就不会自动合并了。 - dkinzer在本地系统上创建一个与该名称相同的分支。例如,假设您想获取名为branch-05142011
的分支。
git branch branch-05142011 origin/branch-05142011
它会给你一个消息:
$ git checkout --track origin/branch-05142011
Branch branch-05142011 set up to track remote branch refs/remotes/origin/branch-05142011.
Switched to a new branch "branch-05142011"
现在只需像下面这样检出分支,你就有了代码
git checkout branch-05142011
git fetch origin [remote-branch]:[new-local-branch]
! - Jeaf Gilbertgit branch ue5-early-access origin/ue5-early-access
,但出现错误:"fatal: Not a valid object name: 'origin/ue5-early-access'.",有什么提示吗? - Aquarius Powergit --branch <branchname> <url>
但是Bash自动完成功能无法识别这个键:--branch
--single-branch
命令;在撰写本文时,Git 2.5已经发布。不用关心旧版本。 - Bernhard Döbler