如何在git中克隆一个分支?

时间:2021-04-09 16:27:14

I have a local git repository called 'skeleton' that I use for storing project skeletons. It has a few branches, for different kinds of projects:

我有一个本地git仓库,称为“骨架”,用于存储项目骨架。它有几个分支,用于不同的项目:

casey@agave [~/Projects/skeleton] git branch
* master
  rails
  c
  c++

If I want to check out the master branch for a new project, I can do

如果我想要查询一个新项目的主分支,我可以。

casey@agave [~/Projects] git clone skeleton new
Initialized empty Git repository in /Users/casey/Projects/new/.git/

and everything is how I want it. Specifically, the new master branch points to skeleton master branch, and I can push and pull to move around changes to the basic project setup.

一切都是我想要的。具体地说,新的主分支指向骨架主分支,我可以推动和拉动围绕对基本项目设置的更改。

What doesn't work, however, is if I want to clone another branch. I can't get it so that I only pull the branch I want, for instance the rails branch, and then the new repo has a 'master' branch that pushes to and pulls from the skeleton repo's 'rails' branch, by default.

然而,如果我想克隆另一个分支,那就不可行了。我无法得到它,所以我只需要拖动我想要的分支,例如rails分支,然后新的repo有一个“master”分支,它会在默认情况下,从骨架repo的“rails”分支中推送和提取。

Is there a good way to go about doing this? Or, maybe this isn't the way that git wants me to structure things, and I'm certainly open to that. Perhaps I should have multiple repos, with the rails skeleton repo tracking the master skeleton repo? And any individual project cloning the rails skeleton repo.

有什么好的方法去做这件事吗?或者,也许这不是git想要我构造东西的方式,我当然对此持开放态度。也许我应该有多个repos,用rails框架repo跟踪master骨架repo?以及任何克隆rails骨架的项目。

Any thoughts and suggestions are appreciated!

感谢您的任何想法和建议!

13 个解决方案

#1


594  

Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

注:git1.7.10(2012年4月)实际上只允许你克隆一个分支:

# clone only the remote primary HEAD (default: origin/master)
git clone --single-branch

as in:
git clone <url> --branch <branch> --single-branch [<folder>]

You can see it in t5500-fetch-pack.sh:

你可以在t5500- fetchpack .sh看到它。

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu comments that:

东武的评论:

This is implicit when doing a shallow clone.
This makes git clone --depth 1 the easiest way to save bandwidth.

这是在做一个浅克隆时隐含的。这使得git克隆——深度1是节省带宽的最简单方法。

And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".

而由于Git 1.9.0(2014年2月),浅层克隆支持数据传输(push/pull),所以这个选项现在更有用了。看到更多的“是git克隆——深度1(浅层克隆)比它所做的更有用吗?”


"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

“取消”一个浅层克隆在“转换浅克隆到完全克隆”(git 1.8.3+)中详细说明。

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

As Chris comments:

克里斯的评论:

the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

将缺失分支反向的魔法线——单分支是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

#2


617  

One way is to execute the following

一种方法是执行以下操作。

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth. Now, starting with GIT 1.7.10, you can now do this

branch_name是您选择的分支,“/您的/文件夹”是该分支的目标文件夹。这是真的,这将会带来其他的分支给你机会来合并来来回回。现在,从GIT 1.7.10开始,现在可以这样做了。

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

#3


100  

Using git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

使用git版本1.7.3.1(在Windows上),这里是我所做的($BRANCH是我想要签出的分支的名称,$REMOTE_REPO是我想要克隆的远程存储库的URL):

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

The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.

这种方法的优点是,随后的git pull(或git fetch)调用也将只下载请求的分支。

#4


22  

You can try the long winded way:

你可以试着用长条的方式:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original 
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

什么这是:

  • Create and init empty git repository.
  • 创建并初始化空的git存储库。
  • Adds the original repository as a remote called origin.
  • 将原始存储库添加为远程调用的源。
  • Fetches only the branch you require from the remote called origin.
  • 从远程调用的原点获取所需的分支。
  • Creates and checks out a new brach that is setup to track the source branch you just cloned.
  • 创建并检查一个新的brach,用于跟踪刚刚克隆的源分支。

Hopefully that will be something like what you are after.

希望这将会是你想要的东西。

#5


11  

from git-clone man page

从git clone手册页

--single-branch is your friend during clone
remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

——在克隆时,单分支是您的朋友,记住要使用-branch <分支名称> ,或仅克隆远程主头(默认为master)

always remember to do CTRL+F5 to read fresh source, not the one from cache :-)
(I didn't so didn't know about this option for long time)

记住要按CTRL+F5来读取新的源文件,而不是从缓存中读取数据:-)(很长时间以来我都不知道这个选项)

#6


10  

You can do it by using the below command:

您可以使用以下命令来完成:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

#7


2  

git clone <url> --branch <branch> --single-branch

Just put url and branch name

只需输入url和分支名称。

#8


1  

Can be done in 2 steps

可以分为两步吗?

  1. Clone the repository

    克隆存储库

    • git clone <http url>
    • git克隆< http url >
  2. Checkout the branch you want

    结帐你要的分行。

    • git checkout $BranchName
    • git checkout BranchName美元

#9


1  

After cloning was complete, I had to enter git submodule update --init --recursive to download all submodules

在克隆完成之后,我必须输入git子模块更新——init——来下载所有子模块。

#10


1  

open the cmd
cd folder_name (Enter the path where to clone the branch) Just a command

打开cmd cd folder_name(输入克隆分支的路径),只需要一个命令。

git clone url_of_projecturltoclone -b branch_name

#11


1  

for cloning branch of git you don't have public key the use this

对于git的克隆分支,您没有使用它的公钥。

git clone -b <branch> <git repo url or clone url you get from git repo>

#12


0  

Clone only one branch this is the most easy way:

只克隆一个分支这是最简单的方法:

$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git

#13


-2  

  1. Open Git bash shell.
  2. 开放的Git bash shell。
  3. Create an directory in your file system where you want to checkout.
    • $ mkdir Feature_develop_branch
    • mkdir Feature_develop_branch美元
  4. 在您想要签出的文件系统中创建一个目录。mkdir Feature_develop_branch美元
  5. change directory to Feature_develop_branch folder.
    • $ cd Feature_develop_branch
    • $ cd Feature_develop_branch
  6. 将目录更改为Feature_develop_branch文件夹。$ cd Feature_develop_branch
  7. clone the repository using external clone url.
  8. 使用外部克隆url克隆存储库。美元git克隆https://someurl.repositoryName.git
  9. After cloning, change the directory to created repositoryName.
    • $ cd /repositoryName
    • $ cd / repositoryName
  10. 在克隆之后,将目录更改为创建存储库名称。$ cd / repositoryName
  11. Check out the branch.
    • $ git checkout <Branch Name>
    • $ git checkout <分支名称> 。
  12. 查看分支。$ git checkout <分支名称> 。

#1


594  

Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:

注:git1.7.10(2012年4月)实际上只允许你克隆一个分支:

# clone only the remote primary HEAD (default: origin/master)
git clone --single-branch

as in:
git clone <url> --branch <branch> --single-branch [<folder>]

You can see it in t5500-fetch-pack.sh:

你可以在t5500- fetchpack .sh看到它。

test_expect_success 'single branch clone' '
  git clone --single-branch "file://$(pwd)/." singlebranch
'

Tobu comments that:

东武的评论:

This is implicit when doing a shallow clone.
This makes git clone --depth 1 the easiest way to save bandwidth.

这是在做一个浅克隆时隐含的。这使得git克隆——深度1是节省带宽的最简单方法。

And since Git 1.9.0 (February 2014), shallow clones support data transfer (push/pull), so that option is even more useful now.
See more at "Is git clone --depth 1 (shallow clone) more useful than it makes out?".

而由于Git 1.9.0(2014年2月),浅层克隆支持数据传输(push/pull),所以这个选项现在更有用了。看到更多的“是git克隆——深度1(浅层克隆)比它所做的更有用吗?”


"Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)

“取消”一个浅层克隆在“转换浅克隆到完全克隆”(git 1.8.3+)中详细说明。

# unshallow the current branch
git fetch --unshallow

# for getting back all the branches (see Peter Cordes' comment)
git config remote.origin.fetch refs/heads/*:refs/remotes/origin/*
git fetch --unshallow

As Chris comments:

克里斯的评论:

the magic line for getting missing branches to reverse --single-branch is (git v2.1.4):

将缺失分支反向的魔法线——单分支是(git v2.1.4):

git config remote.origin.fetch +refs/heads/*:refs/remotes/origin/*
git fetch --unshallow  

#2


617  

One way is to execute the following

一种方法是执行以下操作。

git clone user@git-server:project_name.git -b branch_name /your/folder

Where branch_name is the branch of your choice and "/your/folder" is the destination folder for that branch. It's true that this will bring other branches giving you the opportunity to merge back and forth. Now, starting with GIT 1.7.10, you can now do this

branch_name是您选择的分支,“/您的/文件夹”是该分支的目标文件夹。这是真的,这将会带来其他的分支给你机会来合并来来回回。现在,从GIT 1.7.10开始,现在可以这样做了。

git clone user@git-server:project_name.git -b branch_name --single-branch /your/folder

#3


100  

Using git version 1.7.3.1 (on Windows), here's what I do ($BRANCH is the name of the branch I want to checkout and $REMOTE_REPO is the URL of the remote repository I want to clone from):

使用git版本1.7.3.1(在Windows上),这里是我所做的($BRANCH是我想要签出的分支的名称,$REMOTE_REPO是我想要克隆的远程存储库的URL):

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

The advantage of this approach is that subsequent git pull (or git fetch) calls will also just download the requested branch.

这种方法的优点是,随后的git pull(或git fetch)调用也将只下载请求的分支。

#4


22  

You can try the long winded way:

你可以试着用长条的方式:

mkdir newrepo.git
cd newrepo.git
git init
git remote add origin file:///path/to/original 
git fetch origin branchiwant:refs/remotes/origin/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

什么这是:

  • Create and init empty git repository.
  • 创建并初始化空的git存储库。
  • Adds the original repository as a remote called origin.
  • 将原始存储库添加为远程调用的源。
  • Fetches only the branch you require from the remote called origin.
  • 从远程调用的原点获取所需的分支。
  • Creates and checks out a new brach that is setup to track the source branch you just cloned.
  • 创建并检查一个新的brach,用于跟踪刚刚克隆的源分支。

Hopefully that will be something like what you are after.

希望这将会是你想要的东西。

#5


11  

from git-clone man page

从git clone手册页

--single-branch is your friend during clone
remember to use with --branch <branch name> or only remote primary HEAD will be cloned (master by default)

——在克隆时,单分支是您的朋友,记住要使用-branch <分支名称> ,或仅克隆远程主头(默认为master)

always remember to do CTRL+F5 to read fresh source, not the one from cache :-)
(I didn't so didn't know about this option for long time)

记住要按CTRL+F5来读取新的源文件,而不是从缓存中读取数据:-)(很长时间以来我都不知道这个选项)

#6


10  

You can do it by using the below command:

您可以使用以下命令来完成:

git clone -b branch_name --single-branch project_url local_folder_to_clone_in

#7


2  

git clone <url> --branch <branch> --single-branch

Just put url and branch name

只需输入url和分支名称。

#8


1  

Can be done in 2 steps

可以分为两步吗?

  1. Clone the repository

    克隆存储库

    • git clone <http url>
    • git克隆< http url >
  2. Checkout the branch you want

    结帐你要的分行。

    • git checkout $BranchName
    • git checkout BranchName美元

#9


1  

After cloning was complete, I had to enter git submodule update --init --recursive to download all submodules

在克隆完成之后,我必须输入git子模块更新——init——来下载所有子模块。

#10


1  

open the cmd
cd folder_name (Enter the path where to clone the branch) Just a command

打开cmd cd folder_name(输入克隆分支的路径),只需要一个命令。

git clone url_of_projecturltoclone -b branch_name

#11


1  

for cloning branch of git you don't have public key the use this

对于git的克隆分支,您没有使用它的公钥。

git clone -b <branch> <git repo url or clone url you get from git repo>

#12


0  

Clone only one branch this is the most easy way:

只克隆一个分支这是最简单的方法:

$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git

#13


-2  

  1. Open Git bash shell.
  2. 开放的Git bash shell。
  3. Create an directory in your file system where you want to checkout.
    • $ mkdir Feature_develop_branch
    • mkdir Feature_develop_branch美元
  4. 在您想要签出的文件系统中创建一个目录。mkdir Feature_develop_branch美元
  5. change directory to Feature_develop_branch folder.
    • $ cd Feature_develop_branch
    • $ cd Feature_develop_branch
  6. 将目录更改为Feature_develop_branch文件夹。$ cd Feature_develop_branch
  7. clone the repository using external clone url.
  8. 使用外部克隆url克隆存储库。美元git克隆https://someurl.repositoryName.git
  9. After cloning, change the directory to created repositoryName.
    • $ cd /repositoryName
    • $ cd / repositoryName
  10. 在克隆之后,将目录更改为创建存储库名称。$ cd / repositoryName
  11. Check out the branch.
    • $ git checkout <Branch Name>
    • $ git checkout <分支名称> 。
  12. 查看分支。$ git checkout <分支名称> 。