LWN.net Logo

Can "git clone" checkout a specified branch now?

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 4:23 UTC (Wed) by bbbush (subscriber, #17456)
Parent article: Git Magic

Actually what I want to do is

mkdir folder1
cd folder1 && git init
git pull <url> <branch>

but I wonder if I can use "git clone <url> -b <branch>" instead of several commands?


(Log in to post comments)

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 6:11 UTC (Wed) by dh (subscriber, #153) [Link]

Hi,

that doesn't make sense. "clone" always clones the complete repository 
including all branches. You then only decide which branch should drive 
your working tree. So, my workflow always is

git clone <some-url>
git checkout <wanted-branch>

Best regards,
Dirk

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 6:18 UTC (Wed) by bbbush (subscriber, #17456) [Link]

Only doesn't make sense for "clone" itself but for usability

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 15:05 UTC (Wed) by newren (subscriber, #5160) [Link]

I've wanted that as well, but no, I haven't seen it in git. I've used the commands you gave sometimes, though I also once put a script together with the following commands:

git clone --no-checkout REPOSITORY
git symbolic-ref HEAD refs/heads/BRANCH
git read-tree -m -u -v HEAD HEAD

Typically users aren't supposed to be exposed to symbolic-ref or read-tree; they're low-level "plumbing" commands. But it can be useful in a script if you have a reason for wrapping git as part of some other build tool or something.

Can "git clone" checkout a specified branch now?

Posted Aug 6, 2008 18:13 UTC (Wed) by iabervon (subscriber, #722) [Link]

No, but it should be pretty easy to add now. In builtin-clone, around line 491, it looks up
what HEAD points to on the remote repository, with remote_head being the "HEAD is <hash>" info
and head_points_at being the "refs/heads/master is <hash>" info (where the left part of that
is what HEAD is determined to be a reference to), if there is one. Just look up the user's
specified branch for both if the option was used instead of using locate_head() to find them.

(head_points_at is used so that you get a local branch with the same name as the remote
branch, if the remote repository doesn't have HEAD not on a branch; remote_head is used so
that you check out whatever tree it was that the remote had as HEAD, regardless of whether it
was a branch or not.)

Copyright © 2012, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds