tags:

views:

263

answers:

1

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 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.

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.

Any thoughts and suggestions are appreciated! Thanks SO!

+6  A: 

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/branchiwant
git checkout -b branchiwant --track origin/branchiwant

What this does is:

  • Create and init 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 brach that is setup to track the source branch you just cloned.

Hopefully that will be something like what you are after.

jkp
The arguments to git remote add are swapped, but I couldn't then get checkout to work. "git checkout origin/rails" gave me "error: pathspec 'origin/rails' did not match any file(s) known to git." and the alternative gave me "fatal: git checkout: updating paths is incompatible with switching branches."
rodarmor
@rodarmor: have corrected the git remote command, thanks. Can you paste the output of `git branch -r`?
jkp
@jkp, Actually `git branch -r` gives no output.
rodarmor
@rodarmor: I've corrected the example and this definitely works (tested). HTH (you can accept it once tested it you like ;))
jkp
@jkp, unfortunately, I still get the same error :[
rodarmor