I have a local Git repository in ~/local_repo
. It has a few branches:
$ git branch
* master
rails
c
c++
To clone the local repository, I do:
$ git clone ~/local_repo new_repo
Initialized empty Git repository in /home/username/new_repo/.git/
The new_repo
master branch points to the local_repo
master branch, and I can push / pull.
But I am unable to clone another branch. I want to only pull the branch I want (e.g. rails
), so that the new repository has a master
branch that pushes to and pulls from local_repo
's rails
branch, by default. How do I accomplish this, or perhaps something similar with local_repo
tracking the master local_repo
?
Best Answer
Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:
Note:
<url>
is the URL of the remote repository, and does not reference the branch cloned<folder>
is the local folder where you want to clone the repositoryYou can see it in
t5500-fetch-pack.sh
:Tobu comments that:
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?"."Undoing" a shallow clone is detailed at "Convert shallow clone to full clone" (git 1.8.3+)
As Chris comments:
With Git 2.26 (Q1 2020), "
git clone --recurse-submodules --single-branch
" now uses the same single-branch option when cloning the submodules.See commit 132f600, commit 4731957 (21 Feb 2020) by Emily Shaffer (
nasamuffin
).(Merged by Junio C Hamano --
gitster
-- in commit b22db26, 05 Mar 2020)