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.
Note: the git1.7.10 (April 2012) actually allows you to clone only one branch:
You 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:
From git-clone man page:
--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)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.)
One way is to execute the following.
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 thisJust put in URL and branch name.
Can be done in 2 steps
Clone the repository
git clone <http url>
Checkout the branch you want
git checkout $BranchName
You can do it by using the below command: