【问题标题】:Clone another branch after cloning single-branch克隆单分支后克隆另一个分支
【发布时间】:2022-01-20 08:25:22
【问题描述】:
我正在使用以下命令克隆一个分支:
git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder
现在我想从服务器检查另一个分支。我尝试了下面的命令,它没有工作
git checkout another_branch
克隆一个分支后,如何克隆/checkout/pull/fetch 另一个分支?
【问题讨论】:
标签:
git
branch
git-clone
git-checkout
【解决方案1】:
您可以通过在git fetch 调用中的远程名称之后指定另一个远程分支来获取另一个远程分支:
git fetch origin another_branch
获取后,您会在 FETCH_HEAD 中找到它,并使用它来创建本地分支:
git checkout FETCH_HEAD -b another_branch
【解决方案2】:
除了Mureinik's answer——它适用于一些“一次性”/短期工作案例——您还可以使用git remote 添加其他分支,或将您的单分支克隆更新为全分支克隆:
git remote set-branches --add origin another-branch
在此之后,git fetch origin 将创建远程跟踪名称origin/another-branch,这将允许git checkout another-branch 调用--guess 模式从您的远程跟踪名称创建您的(本地)分支名称another-branch origin/another-branch.
要对克隆进行去单分支化,请使用:
git remote set-branches origin "*"
(照常跟在git fetch之后)。
请注意,您是否需要引用星号取决于您的命令行解释器,但通常这样做是安全。