git 克隆特定分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41233378/
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-09 04:41:54  来源:igfitidea点击:

Cloning specific branch

gitgithub

提问by Jeff P

I am new to git version control and I dont know how to clone / pull a specific branch of a repo . Trying to get the branch masterof the project, but it defaults to branch test_1

我是 git 版本控制的新手,我不知道如何克隆/拉取 repo 的特定分支。试图获取master项目的分支,但默认为分支test_1

I have tried using the command git clonebut it grabbed default test_1. I have also tried reading other questions but the commands are confusing me and I dont want to break anything.

我曾尝试使用该命令,git clone但它抓住了 default test_1。我也尝试阅读其他问题,但命令让我感到困惑,我不想破坏任何东西。

How do i clone the master branch of this project so i can make changes and push to it?

我如何克隆这个项目的主分支,以便我可以进行更改并推送到它?

回答by CodeWizard

You can use the following flags --single-branch&& --depthto download the specific branch and to limit the amount of history which will be downloaded.

您可以使用以下标志--single-branch&&--depth下载特定分支并限制将下载的历史记录数量。

You will clone the repo from a certain point in time and only for the given branch

您将从某个时间点克隆 repo,并且仅针对给定的分支

git clone -b <branch> --single-branch <url> --depth <number of commits>

--[no-]single-branch


Clone onlythe history leading to the tip of a single branch, either specified by the --branchoption or the primary branch remote's HEADpoints at.

Further fetches into the resulting repository will only update the remote-trackingbranch for the branch this option was used for the initial cloning. If the HEAD at the remote did not point at any branch when --single-branchclone was made, no remote-tracking branch is created.

--[no-]单分支


克隆导致单个分支尖端的历史记录,由--branch选项指定或远程主分支HEAD指向。

进一步提取到结果存储库中只会更新remote-tracking此选项用于初始克隆的分支的分支。如果在进行--single-branch克隆时远程的 HEAD 没有指向任何分支,则不会创建远程跟踪分支。



--depth

Create a shallow clone with a history truncated to the specified number of commits

- 深度

创建一个浅克隆,其历史记录被截断为指定的提交次数

回答by amar71094

you can use this command for particular branch clone :

您可以将此命令用于特定分支克隆:

git clone <url of repo> -b <branch name to be cloned>

Eg: git clone https://www.github.com/Repo/FirstRepo -b master

回答by Tahir77667

To clone a particular branch in a git repository

克隆 git 存储库中的特定分支

- Command:git clone "repository_url" -b "branch_name"
- Example:git clone https://gitlab.com/amm.kazi/yusufoverseas.git-b routes

- 命令:git clone "repository_url" -b "branch_name"
- 示例:git clone https://gitlab.com/amm.kazi/yusufoverseas.git-b routes

回答by Mort

I don't think you fully understand how git by default gives you all history of all branches.

我认为您没有完全理解 git 默认情况下如何为您提供所有分支的所有历史记录。

git clone --branch master <URL>will give you what you want.

git clone --branch master <URL>会给你你想要的。

But in fact, in any of the other repos where you ended up with test_1checked out, you could have just done git checkout masterand it would have switched you to the master branch.

但实际上,在您最终test_1签出的任何其他存储库中,您可能刚刚完成git checkout master,它会将您切换到主分支。

(What @CodeWizard says is all true, I just think it's more advanced than what you really need.)

(@CodeWizard 所说的都是真的,我只是认为它比您真正需要的更先进。)

回答by pedrorijo91

a git repository has several branches. Each branch follows a development line, and it has its origin in another branch at some point in time (except the first branch, typically called master, that it starts as the default branch until someone changes, what almost never happens)

一个 git 存储库有几个分支。每个分支都遵循一条开发线,并且它在某个时间点在另一个分支中具有其起源(第一个分支除外,通常称为master,它作为默认分支开始,直到有人更改,这几乎从未发生过)

If you are new with git, remember those 2 fundamentals. Now, you just need to clone the repository, and it will be in some branch. if the branch is the one you are looking for, awesome. If not, you just need to change to the other branch - this is called checkout. Just type git checkout <branch-name>

如果您是 git 新手,请记住这两个基础知识。现在,您只需要克隆存储库,它将位于某个分支中。如果分支是您正在寻找的分支,那就太棒了。如果没有,您只需要更改到另一个分支 - 这称为结帐。只需输入git checkout <branch-name>

In some cases you want to get updates for a specific branch. Just do git pull origin <branch-name>and it will 'download' the new commits (changes). If you didn't do any changes, it should go easy. If you also introduced changes on that branches, conflicts may appear. let me know if you need more info on this case also

在某些情况下,您希望获取特定分支的更新。只要这样做git pull origin <branch-name>,它就会“下载”新的提交(更改)。如果你没有做任何改变,它应该很容易。如果您还在该分支上引入了更改,则可能会出现冲突。如果您还需要有关此案例的更多信息,请告诉我

回答by Abhishek Kumar

Please try like this : git clone --single-branch --branch <branchname> <url>

请尝试这样: git clone --single-branch --branch <branchname> <url>

replace <branchname>with your branch and <url>with your url.

替换<branchname>为您的分支和<url>您的网址。

url will be like http://[email protected]:portno/yourrepo.git.

网址会像http://[email protected]:portno/yourrepo.git.

回答by Ariful Islam

You may try this

你可以试试这个

git clone --single-branch --branch <branchname> host:/dir.git