如何克隆特定的 Git 分支?

发布于 2024-08-14 08:58:11 字数 61 浏览 9 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

何以心动 2024-08-21 08:58:11
git clone -b <branch> <remote_repo>

示例:

git clone -b my-branch [email protected]: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 -b <branch> <remote_repo>

Example:

git clone -b my-branch [email protected]:user/myproject.git

With Git 1.7.10 and later, add --single-branch to prevent fetching of all branches. Example, with OpenCV 2.4 branch:

git clone -b opencv-2.4 --single-branch https://github.com/Itseez/opencv.git
马蹄踏│碎落叶 2024-08-21 08:58:11
git clone --single-branch --branch <branchname> <remote-repo>

--single-branch 选项从版本 1.7.10 及更高版本。

另请参阅许多人喜欢的其他答案

您可能还想确保您了解其中的差异。不同之处在于:通过调用 git clone --branch; url 您正在获取所有分支并签出其中一个。例如,这可能意味着您的存储库有 5kB 文档或 wiki 分支和 5GB 数据分支。每当您想要编辑首页时,您最终可能会克隆 5GB 的数据。

再说一次,这并不是说 git clone --branch 不是实现这一目标的方法,只是当您询问克隆特定分支。

git clone --single-branch --branch <branchname> <remote-repo>

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.

丿*梦醉红颜 2024-08-21 08:58:11

这是一个非常简单的方法:)

克隆存储库

git clone <repository_url>

列出所有分支

git branch -a 

签出您想要的分支

git checkout <name_of_branch>

Here is a really simple way to do it :)

Clone the repository

git clone <repository_url>

List all branches

git branch -a 

Checkout the branch that you want

git checkout <name_of_branch>
似最初 2024-08-21 08:58:11

要克隆分支而不获取其他分支:

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH

To clone a branch without fetching other branches:

mkdir $BRANCH
cd $BRANCH
git init
git remote add -t $BRANCH -f origin $REMOTE_REPO
git checkout $BRANCH
瑶笙 2024-08-21 08:58:11

使用:

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

因此,要根据我的枚举帐户编号分支创建一个新分支,我会这样做:

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"

Use:

git checkout -b <branch-name> <origin/branch_name>

For example in my case:

 git branch -a
* master
  origin/HEAD
  origin/enum-account-number
  origin/master
  origin/rel_table_play
  origin/sugarfield_customer_number_show_c

So to create a new branch based on my enum-account-number branch, I do:

git checkout -b enum-account-number origin/enum-account-number

After you hit Return, the following happens:

Branch enum-account-number set up to track remote branch refs/remotes/origin/enum-account-number.
Switched to a new branch "enum-account-number"
も让我眼熟你 2024-08-21 08:58:11

使用该名称在本地系统上创建一个分支。例如,假设您想要获取名为 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

Create a branch on the local system with that name. e.g. say you want to get the branch named branch-05142011

git branch branch-05142011 origin/branch-05142011

It'll give you a message:

$ 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"

Now just checkout the branch like below and you have the code

git checkout branch-05142011
清眉祭 2024-08-21 08:58:11
git --branch <branchname> <url>

但是 Bash 补全没有得到这个键: --branch

git --branch <branchname> <url>

But Bash completion don't get this key: --branch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文