A friend is stuc开发者_如何学Ck with an old version of Git (I think he said 1.5?), where he says the -b <branch>
option is not supported. I can't wrap my head around it, so I really hope someone could help:
What would be the equivalent of the following command, without using -b
?
git clone -b $BRANCH $REPO
EDIT: I originally asked for git checkout
- that's not what I meant. Sorry!
in older git this required two steps:
git branch $BRANCH $FROM_COMMIT
git checkout $BRANCH
notice i used $FROM_COMMIT
, $REPO
in your question looks odd and misleading – you can only create branches from commits, not from other repositories.
editing my answer, since the question was altered. reading the manpage for git clone, we can see that
-b
Instead of pointing the newly created HEAD to the branch pointed to by the cloned repository’s HEAD, point to branch instead. In a non-bare repository, this is the branch that will be checked out.
to achieve this effect with an older git version we would use:
git clone $REPO
git branch $BRANCH origin/$BRANCH
git checkout $BRANCH
this will set your local HEAD
to the newly created $BRANCH
which is pointing to origin/$BRANCH
(hopefully i'm not mistaken – i don't have a git install here to test …)
He should be able to do:
git clone -n $REPO
cd <reponame>
git checkout -b $BRANCH origin/$BRANCH
I've checked that the options are valid for v1.2.0 so this should be good if he's on at least 1.5.
For reference, clone only gained the -b
/--branch
option in v1.6.5.
That would be:
git clone -n $REPO
git checkout -b $BRANCH origin/$BRANCH
(see Charles Bailey's answer for the right answer)
If you don't fetch the default branch from the HEAD
repo, you need to fetch the branch you actually want to track, and then create your own local branch.
I would have deleted this answer, but Charles Bailey's comments are interesting:
There's no point in fetching immeditately after a clone but you might want to pass
-n
toclone
to avoid the unnecessary checkout of the remote's default branch.
Also, you need the-b
option tocheckout
.I've just checked,
-b
was added tocheckout
in commit91dcdfd3
which predatesv1.0
of git.
If your friend is using a version older than this (which seems extremely unlikely) then you need to be more specific.
精彩评论