Advertisement
Advertisement


What is the command equivalent to git clone -b on old Git versions?


Question

A friend is stuck 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!

2011/08/27
1
2
8/27/2011 8:20:47 PM

Accepted Answer

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 …)

2011/04/21
6
4/21/2011 8:43:23 AM


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 to clone to avoid the unnecessary checkout of the remote's default branch.
Also, you need the -b option to checkout.

I've just checked, -b was added to checkout in commit 91dcdfd3 which predates v1.0 of git.
If your friend is using a version older than this (which seems extremely unlikely) then you need to be more specific.

2017/05/23

Source: https://stackoverflow.com/questions/5741490
Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]