如何克隆特定的 Git 分支?
Git克隆会将远程分支克隆到本地。
有没有办法自己克隆特定分支而不需要切换远程存储库上的分支?
Git clone will clone remote branch into local.
Is there any way to clone a specific branch by myself without switching branches on the remote repository?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
示例:
对于 Git 1.7.10 及更高版本,添加
--single-branch
以防止获取所有分支。例如,使用 OpenCV 2.4 分支:Example:
With Git 1.7.10 and later, add
--single-branch
to prevent fetching of all branches. Example, with OpenCV 2.4 branch:--single-branch
选项从版本 1.7.10 及更高版本。另请参阅许多人喜欢的其他答案。
您可能还想确保您了解其中的差异。不同之处在于:通过调用 git clone --branch; url 您正在获取所有分支并签出其中一个。例如,这可能意味着您的存储库有 5kB 文档或 wiki 分支和 5GB 数据分支。每当您想要编辑首页时,您最终可能会克隆 5GB 的数据。
再说一次,这并不是说 git clone --branch 不是实现这一目标的方法,只是当您询问克隆特定分支。
The
--single-branch
option is valid from version 1.7.10 and later.Please see also the other answer which many people prefer.
You may also want to make sure you understand the difference. And the difference is: by invoking
git clone --branch <branchname> url
you're fetching all the branches and checking out one. That may, for instance, mean that your repository has a 5kB documentation or wiki branch and 5GB data branch. And whenever you want to edit your frontpage, you may end up cloning 5GB of data.Again, that is not to say
git clone --branch
is not the way to accomplish that, it's just that it's not always what you want to accomplish, when you're asking about cloning a specific branch.这是一个非常简单的方法:)
克隆存储库
列出所有分支
签出您想要的分支
Here is a really simple way to do it :)
Clone the repository
List all branches
Checkout the branch that you want
要克隆分支而不获取其他分支:
To clone a branch without fetching other branches:
使用:
例如在我的例子中:
因此,要根据我的枚举帐户编号分支创建一个新分支,我会这样做:
点击 Return 后,会发生以下情况:
Use:
For example in my case:
So to create a new branch based on my enum-account-number branch, I do:
After you hit Return, the following happens:
使用该名称在本地系统上创建一个分支。例如,假设您想要获取名为
branch-05142011
的分支,它会给您一条消息:
现在只需像下面这样签出分支,您就可以获得代码
Create a branch on the local system with that name. e.g. say you want to get the branch named
branch-05142011
It'll give you a message:
Now just checkout the branch like below and you have the code
但是 Bash 补全没有得到这个键:
--branch
But Bash completion don't get this key:
--branch