git 从 master 以外的分支克隆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17383217/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:28:27  来源:igfitidea点击:

Clone from a branch other than master

gitgithubgithub-for-windows

提问by user2510555

I am trying to pull from a repository in Github. But I don't want to clone the master branch. I want to clone some other branch. When I try git clone <url>, I get the files from master branch. What should I do?

我正在尝试从 Github 的存储库中提取。但我不想克隆主分支。我想克隆一些其他分支。当我尝试时git clone <url>,我从 master 分支获取文件。我该怎么办?

Also, suppose the code is updated in the repository and I want to get the latest code, should I again use git clone? Because the size of the project is huge. Also if I make changes to the project locally, and then I again use git clone, will the changes I made still be there? What if I don't want changes to be there?

另外,假设代码在存储库中更新并且我想获取最新代码,我应该再次使用git clone吗?因为项目规模巨大。另外,如果我在本地对项目进行更改,然后再次使用 git clone,那么我所做的更改还会存在吗?如果我不想在那里进行更改怎么办?

I am not even sure if git cloneis the right command. git pullor git fetch?

我什至不确定这是否git clone是正确的命令。git pull或者git fetch

I am sorry, I am very new to git.

对不起,我对 git 很陌生。

回答by Aguardientico

Try this:

尝试这个:

git init
git fetch url-to-repo branchname:refs/remotes/origin/branchname

EDIT

编辑

A better solution:

更好的解决方案:

git clone -b mybranch --single-branch git://sub.domain.com/repo.git

回答by kamjagin

git clone <url>

clones and creates remote-tracking branches for each branch. If you want to see available branches (after cloning), you type

克隆并为每个分支创建远程跟踪分支。如果要查看可用分支(克隆后),请键入

git branch -l

To switch to a particular branch after cloning you do:

要在克隆后切换到特定分支,请执行以下操作:

git checkout <branchname>

where branchname is the name of the branch :)

其中 branchname 是分支的名称:)

If you want to clone and checkout a specific branch you do

如果你想克隆和签出一个特定的分支,你可以

git clone -b <branchname> <url>

The other commands you mention are for "updating" your current working copy. git pullgets all changes from the remote repository and merges them while git fetchonly gets them without merging.

您提到的其他命令用于“更新”您当前的工作副本。git pull从远程存储库获取所有更改并合并它们,而git fetch只获取它们而不合并。

回答by Balog Pal

use git clone --branch <name>possibly adding --single-branch

使用git clone --branch <name>可能添加--single-branch

as usual you have git clone --helpto read details on commands

像往常一样,您必须git clone --help阅读有关命令的详细信息