How do I clone a specific Git branch? [duplicate]

一曲冷凌霜 提交于 2019-11-26 03:27:30

问题


This question already has an answer here:

  • How do I clone a single branch in Git? 15 answers

Git clone will behave copying remote current working branch into local.

Is there any way to clone a specific branch by myself without switching branches on the remote repository?


回答1:


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.

At the time of writing the original answer below, git had no --single-branch option, but let's preserve it for full satisfaction of angry bees.

The answer so badly disliked by copypasters was this:

git init
git remote add -t refspec remotename host:/dir.git
git fetch



回答2:


git clone -b <branch> <remote_repo>

Example:

git clone -b my-branch git@github.com: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



回答3:


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>



回答4:


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



回答5:


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

"




回答6:


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 like - "Branch branch-05142011 set up to track remote branch branch-05142011 from origin."

Now just checkout the branch like below and you have the code -
git checkout branch-05142011




回答7:


git --branch <branchname> <url>

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

Enjoy.



来源:https://stackoverflow.com/questions/1911109/how-do-i-clone-a-specific-git-branch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!