How do I clone a single branch in Git?

2018-12-31 05:25发布

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:

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 the 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 repository has a master branch that pushes to and pulls from the skeleton repository's rails branch, by default.

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 repositories, with the Ruby on Rails skeleton repository tracking the master skeleton repository? And any individual project cloning the Ruby on Rails skeleton repository.

14条回答
与君花间醉酒
2楼-- · 2018-12-31 05:45

For cloning a branch of Git you don't have the public key to, use this:

git clone -b <branch> <Git repository URL or clone URL you get from Git repository>
查看更多
零度萤火
3楼-- · 2018-12-31 05:46
  1. Open Git Bash shell.
  2. Create an directory in your file system where you want to checkout.
    • $ mkdir Feature_develop_branch
  3. Change directory to Feature_develop_branch folder.
    • $ cd Feature_develop_branch
  4. Clone the repository using external clone URL.
  5. After cloning, change the directory to created repositoryName.
    • $ cd /repositoryName
  6. Check out the branch.
    • $ git checkout <Branch Name>
查看更多
美炸的是我
4楼-- · 2018-12-31 05:47

Open the cmd.

cd folder_name (enter the path where to clone the branch)

Just one command:

git clone url_of_projecturltoclone -b branch_name
查看更多
呛了眼睛熬了心
5楼-- · 2018-12-31 05:55

Clone only one branch. This is the easiest way:

$ git clone -b BRANCH_NAME --single-branch git@bitbucket.org:___/PROJECTNAME.git
查看更多
怪性笑人.
6楼-- · 2018-12-31 06:00

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

查看更多
旧人旧事旧时光
7楼-- · 2018-12-31 06:02

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 an empty Git repository.
  • 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 branch that is set up to track the source branch you just cloned.

Hopefully that will be something like what you are after.

查看更多
登录 后发表回答